简体   繁体   English

WP-如何按页面父类别呼叫帖子

[英]WP - How to call posts by page parent category

I'm setting up a blog listings template for a site with multiple parent pages, and I need this page template to be used across the site, various pages will require there own blog listings page. 我正在为具有多个父页面的网站设置博客列表模板,并且我需要在整个站点中使用此页面模板,各个页面都需要有自己的博客列表页面。 Calling relevant posts depending on the Parent category. 根据父类别呼叫相关职位。

ie: 即:

Food (Site Homepage) 食品(网站首页)

  • Food Blog (Listings page for all Food related posts) 食品博客(所有与食品相关的帖子的列表页面)

Fruit (Parent page) 水果(父页面)

  • Fruit Blog (Listings page for all Fruit related posts) 水果博客(所有与水果相关的帖子的列表页面)

Veg (Parent page) 蔬菜(父页面)

  • Veg Blog (Listings page for all Veg related posts) 蔬菜博客(所有蔬菜相关帖子的列表页面)

My problem is that the posts per correct parent category aren't being called. 我的问题是每个正确的父类别的帖子都没有被调用。 I'm getting all posts. 我正在收到所有帖子。

I've set my code up like this: Many thanks in advance. 我已经将代码设置如下:预先感谢。

<div id="bloglistings">

            <?php wp_reset_query(); ?>

            <?php


            $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
            $current_cat = intval( get_query_var('cat') );

            $args = array(
            'post_type'         => 'post',
            'paged'             => $paged,
            'posts_per_page'    => 6,
            'order'             => 'DESC',
            'orderby'           => 'ID',
            );

            $wp_query = new WP_Query( $args );

            if ( $wp_query->have_posts() ) : ?>

            <div class="row">

            <?php $count=0; ?>    
            <?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>

            <div class="span6">
            <div class="media feature one">
            <a class="pull-left" href="<?php the_permalink(); ?>">
            <?php echo get_the_post_thumbnail($page->ID, 'thumbnail', array('class' => 'img-circle')); ?>
            <img class="hoverimage" src="<?php echo get_template_directory_uri(); ?>/img/icon-read-bloglistings.png" alt="">
            </a>
            <div class="media-body">
            <h2><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h2>
            <p class="date"><?php the_time('F j, Y'); ?></p>
            <p><?php
            $my_excerpt = get_the_excerpt();
            if ( $my_excerpt != '' ) {
            // Some string manipulation performed
            }
            echo $my_excerpt; // Outputs the processed value to the page
            ?>
            </p>
            </div>
            </div>
            </div>

            <?php $count++; ?>

            <?php if ($count==2 ||$wp_query->found_posts==0) : 

            echo '</div><div class="row">';

            ?>

            <?php $count=0; ?>

            <?php endif; ?>
            <?php endwhile; ?>

            <?php else : ?>                
            <h2>Sorry but there are no posts.</h3>
            <?php endif; ?>

            <!-- PAGINATION -->

            <div class="pagination">
            <ul>
            <li>
            <?php
            $big = 999999999; // need an unlikely integer

            echo paginate_links( array(
            'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big
            ) ) ),
            'format' => '?paged=%#%',
            'current' => max( 1, get_query_var('paged') ),
            'total' => $wp_query->max_num_pages,
            ) );
            ?>
            </li>
            </ul>
            </div>

            <?php wp_reset_query(); ?>


            </div><!-- /.row -->

            </div><!--/bloglistings-->

Try adding the category to the query. 尝试将类别添加到查询中。

Replace this: 替换为:

 $args = array(
        'post_type'         => 'post',
        'paged'             => $paged,
        'posts_per_page'    => 6,
        'order'             => 'DESC',
        'orderby'           => 'ID',
        );

with

 $args = array(
        'post_type'         => 'post',
        'category__in'      => array( $current_cat ),// category was missing here
        'paged'             => $paged,
        'posts_per_page'    => 6,
        'order'             => 'DESC',
        'orderby'           => 'ID',
        );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM