在WP循环中显示特定类别

时间:2016-04-09 11:23:38

标签: php wordpress loops wp-list-categories

在我的WordPress网站上,我创建了一个页面,其中显示了该类别的9个帖子"访谈"。然后,如果您想查看该类别的更多帖子,可以点击一个按钮(" Charger加上d'访谈")进行点击。

循环中存在问题,因为当我点击它时,它会显示来自所有其他类别的帖子。 我该怎么做才能只显示"采访" ?

<section class="blocs" id="home_article_derniers">
    <div class="iso-container">
    <?php 

    $post_home_query = new WP_Query(array(
        'post_type' => 'post',
        'category_name' => 'interview',
        'posts_per_page' => 9,
        'orderby' => 'date',
        'order' => 'DESC',
        'post_status' => 'publish',
        'paged' => 2
    ));

    if($post_home_query->have_posts()){
        while ( $post_home_query->have_posts() ) {
            $post_home_query->the_post();
            get_template_part('loop-post', 'actus');
        }
        wp_reset_postdata();
      } ?>
    </div>
</section>

    <div class="separator">
     <a class="btn btn-default more-article" data-paged="1" data-post-not-in="<?php echo implode(',', $array_post_not_in) ?>">Charger plus d'interviews &raquo;</a>

    ?>
    </div>

这是循环的代码:

<?php $color = wami_get_first_category_color(get_the_ID()); ?>

  <article data-id="<?php the_ID(); ?>" class="post post-actu iso-item bloc-infinity <?php echo (isset($phone) ? $phone : '' ) ?> ">
   <a href="<?php the_permalink(); ?>">
    <div class="post-header">

        <?php 
            if(has_post_thumbnail()):
                the_post_thumbnail('actu-hp-small');
            endif;
        ?>

        <div class="overlay">
            <div class="img"></div>
             <div class="background" style="background-color:<?php echo $color; ?>">
             </div>
           </div>
         <div class="post-header-title">
            <?php wami_the_first_category(get_the_ID(), false, $color, true); ?>
         </div>
       </div>

      <div class="post-body">
        <?php
            the_title( '<h2>', '</h2>' );
            /*the_excerpt();*/
        ?>
      </div>
  </a>
 </article>

这是功能

function wami_load_more_posts(){
if ( isset($_REQUEST) ) {
    $paged = $_REQUEST['paged'];
    $post_not_in = explode(',', $_REQUEST['post_not_in']);

    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 9,
        'orderby' => 'date',
        'order' => 'DESC',
        'post__not_in' => $post_not_in,
        'post_status' => 'publish',
        'paged' => $paged
    );
    if(isset($_REQUEST['category_in'])){
        $args['category__in'] = $_REQUEST['category_in'];
    }

    $homeblog_query = new WP_Query($args);

    if( $homeblog_query->have_posts() ):
        while($homeblog_query->have_posts()):
            $homeblog_query->the_post();
            get_template_part('loop-post', 'actus');
        endwhile;
    endif;
    wp_reset_postdata();
    }
die();
 }
add_action('wp_ajax_wami_load_more_posts', 'wami_load_more_posts');
add_action('wp_ajax_nopriv_wami_load_more_posts', 'wami_load_more_posts');

0 个答案:

没有答案