WP_Query 无法在页面模板上运行

问题描述 投票:0回答:1

我有一个带有 ACF 字段的页面模板,可在页面上显示特定类别。

代码显示所有类别的所有帖子,但我只想显示自定义字段中选择的类别。

希望你能帮助我。谢谢

<?php /* Template Name: Category Page 2*/ ?>
<?php get_header(); ?>


<div class="container">
  <div class="row">
    <div class="col-md-12">
      <?php
      $terms = get_field('select_cat');
      if ($terms) : $page_cat = array(); ?>
        <div class="mt-3">
            <span class="filter_title">Filter :</span>
            <select class="filter_dropdown">
                <option value="">All</option>
                <?php foreach ($terms as $term) : ?>
                    <option value="<?php echo esc_url(get_term_link($term)); ?>" data-thumbnail="<?php echo get_field('flag_image' , esc_html($term->taxonomy . '_' . $term->term_id)); ?>"><?php echo esc_html($term->name); ?></option>
                <?php endforeach; ?>
            </select>
        </div>
      <?php endif; ?>
    </div>
  </div>
</div>

<?php //print_r($page_cat); 
?>
<?php
$posts = new WP_Query([
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 9,
  'cat' => $page_cat,
  'orderby' => 'date',
  'order' => 'DESC',
  'paged' => 1,
]);
?>

<div class="container">
    <div class="row">
        <div class="col-md-8">
            <?php if ($posts->have_posts()) : ?>
              <div class="row post-list">
                <?php
                $i = 1;
                while ($posts->have_posts()) : $posts->the_post(); ?>
                    <?php 
                        $author_id = get_the_author_meta('ID');
                        $profilepicture = get_user_meta( $author_id, 'profilepicture', 'true' );
                        $display_name = get_the_author_meta( 'display_name', $author_id );
                    ?>
                    <div class="catpage_post_col col-md-<?php if ($i == 1) {echo "12";}else{echo "6";} ?>">
                        <div>
                            <a href="<?php echo get_permalink(); ?>"><img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id($post->ID) ); ?>" class="img-fluid catpage_post_feature_img<?php if ($i == 1) {echo '-01';}?>"></a>
                        </div>
                        <div>
                            <span class="post_author_info">
                                <img src="<?php echo $profilepicture; ?>" class="post_author_img">
                                <span class="single_post_author_name">
                                    <a href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>"><?php the_author(); ?></a>
                                </span>
                            </span>
                            <span class="post_date_info">
                                <?php echo get_the_date(); ?>
                            </span>
                            <?php if ($i == 1) { ?>
                                <span class="post_read_info">
                                    <?php echo do_shortcode('[rt_reading_time postfix="MINS READ" postfix_singular="minute"]') ?>
                                </span>
                            <?php } ?>
                        </div>
                        <h4 class="catpage_post_title"><a href="<?php echo get_permalink(); ?>" class="catpage_post_title_link"><?php echo get_the_title(); ?></a></h4>
                        <p class="catpage_post_excerpt">
                          <?php
                                $excerpt = get_the_excerpt(); 
                                $excerpt = substr( $excerpt, 0, 150 );
                                $result = substr( $excerpt, 0, strrpos( $excerpt, ' ' ) );
                                echo $result; 
                            ?>
                         </p>
                        <a href="<?php echo get_permalink(); ?>" class="catpage_post_link">CONTINUE READING</a>
                    </div>
                <?php $i++; endwhile;
                ?>
              </div>
            <?php endif; ?>
            <?php wp_reset_postdata(); ?>

            <div class="vc_btn3-container  with-icon without_col_change vc_btn3-center text-center mb-5 load-more-div">
                <a class="vc_general vc_btn3 vc_btn3-size-md vc_btn3-shape-round vc_btn3-style-classic vc_btn3-color-success d-inline-block load-more" href="#">Load more</a>
            </div>

        </div>
        <div class="col-md-4 post-sidebar">
            <?php dynamic_sidebar( 'sidebar01' ); ?>
        </div>
    </div>
</div>


<?php get_footer(); ?>

<script type="text/javascript">
  let currentPage = 1;
  $('.load-more').on('click', function(event) {
    event.preventDefault();
    currentPage++; // Do currentPage + 1, because we want to load the next page
    $.ajax({
      type: 'POST',
      url: '/wp-admin/admin-ajax.php',
      dataType: 'json',
      data: {
        action: 'weichie_load_more',
        paged: currentPage,
        page_cat: <?php echo json_encode($page_cat); ?>,
      },
      success: function(res) {
        $('.post-list').append(res.html);
        if (currentPage >= res.max) {
          $('.load-more-div').hide();
        }

      }
    });
  });
</script>

<script>
$(document).ready(function() {
    $(".filter_dropdown").change(function(){
        window.location = $('.filter_dropdown').val();
    });
})
</script>

代码显示所有类别的所有帖子,但我只想显示自定义字段中选择的类别。

php wordpress templates
1个回答
0
投票

假设您要在“select_cat”ACF 字段中添加分类多重选择。

请先收集term_ids。

$term_ids = wp_list_pluck(get_field('select_cat'), 'term_id');

现在,请更新您的 wp 查询如下,

$posts = new WP_Query([
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 9,
  'tax_query' => array(
    array(
      'taxonomy' => 'category', // Make sure you are using correct taxonomy name as needed
      'field' => 'term_id',
      'terms' => $term_ids
    )
  ),
  'orderby' => 'date',
  'order' => 'DESC',
  'paged' => 1,
]);

可能,您想要基于ajax的下拉过滤器,加载更多。 您可以在 ajax 回调函数中使用与下拉选择和分页计数器相同的逻辑。

© www.soinside.com 2019 - 2024. All rights reserved.