admin管理员组

文章数量:1336632

I need to post articles and pages present in two specific taxonomy in a slider. I tried to merge the $args and $args2 but the slider disappears from the page. Could anyone tell me how to write the code correctly? Thanks!

This is the code:

    <?php

        $slider_counter = 0;

  $args = array(
    'post_type' => 'post',
    'category_name' => 'slider'
  );

$args2 =  array(
'post_type' => 'page',
'tax_query' => array(
    array(
        'taxonomy' => 'my_taxonomy',
        'field' => 'slug',
        'terms' => 'slider'
    )
)
  );


  $merged_query_args = array_merge( $args, $args2 );

        $query_slider = new WP_Query ( $merged_query_args );


        while ( $query_slider->have_posts() ) :
            $query_slider->the_post(); ?>

        <?php $slider_counter++ ;?>

            <?php $slider_image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post ->ID ), 'slider_img' );?>

            <div class="swiper-slide <?php if ($slider_counter ==1) { echo 'active'; } ?>">
                <a href="<?php the_permalink(); ?>"><img src="<?php echo $slider_image_attributes[0]; ?>" class="d-block w-100" alt="..."></a>
                <div class="carousel-caption d-none d-md-block">
                    <div class="dispaly-3"><a href="<?php the_permalink(); ?>" class="slider-a"><?php the_title(); ?></a></div>
                </div>
            </div>

        <?php endwhile;

        wp_reset_query();
        wp_reset_postdata();  ?>

I need to post articles and pages present in two specific taxonomy in a slider. I tried to merge the $args and $args2 but the slider disappears from the page. Could anyone tell me how to write the code correctly? Thanks!

This is the code:

    <?php

        $slider_counter = 0;

  $args = array(
    'post_type' => 'post',
    'category_name' => 'slider'
  );

$args2 =  array(
'post_type' => 'page',
'tax_query' => array(
    array(
        'taxonomy' => 'my_taxonomy',
        'field' => 'slug',
        'terms' => 'slider'
    )
)
  );


  $merged_query_args = array_merge( $args, $args2 );

        $query_slider = new WP_Query ( $merged_query_args );


        while ( $query_slider->have_posts() ) :
            $query_slider->the_post(); ?>

        <?php $slider_counter++ ;?>

            <?php $slider_image_attributes = wp_get_attachment_image_src( get_post_thumbnail_id( $post ->ID ), 'slider_img' );?>

            <div class="swiper-slide <?php if ($slider_counter ==1) { echo 'active'; } ?>">
                <a href="<?php the_permalink(); ?>"><img src="<?php echo $slider_image_attributes[0]; ?>" class="d-block w-100" alt="..."></a>
                <div class="carousel-caption d-none d-md-block">
                    <div class="dispaly-3"><a href="<?php the_permalink(); ?>" class="slider-a"><?php the_title(); ?></a></div>
                </div>
            </div>

        <?php endwhile;

        wp_reset_query();
        wp_reset_postdata();  ?>
Share Improve this question asked May 20, 2020 at 14:29 KnivesKnives 132 bronze badges 2
  • Why are you using a custom taxonomy for pages? It may be a necessity in your specific case but I'm just wondering if it'd be easier to simply use the existing category taxonomy. You could query both post_types using an array(). So post_type => array( 'post', 'page' ),. If you have to keep the two separate taxonomies then your best bet would be to run two queries and just collect the returned IDs into a new array, then run a foreach to output the posts and pages whose IDs are in that new array. – Tony Djukic Commented May 20, 2020 at 17:32
  • Thanks for the answer, yes my need lies in the fact that both pages and posts can be inserted in the slider. – Knives Commented May 21, 2020 at 9:29
Add a comment  | 

1 Answer 1

Reset to default 0

You could try merging the two arg arrays:

$args = array(
    'post_type' => array('post','page'),
    'tax_query' => array(
       'relation' => 'OR',
       array(
         'taxonomy' => 'category',
         'field' => 'slug',
         'terms' => 'slider'
      ),
      array(
         'taxonomy' => 'my_taxonomy',
         'field' => 'slug',
         'terms' => 'slider'
      ),
    ),
  );

本文标签: postsHow to insert 2 args into 1 WpQuery for a slideshow