显示当前上级类别的帖子,右侧显示下级类别帖子

时间:2019-07-16 作者:Aditya

我有分类菜单,如果打开分类。左侧的php仅显示当前父类别的帖子,右侧的php显示子类别帖子。

如果我有两个类别。A是B的父母,B是孩子。

在左侧只想显示所有帖子的一个,右侧只想显示帖子的一个。

这两个操作应在同一模板上执行。

<?php
/**
/* Template Name: Second-page-temp */
get_header();
?>

<div class="container main-in-section">
    <div class="row">
        <div class="col-sm-8">
            <div class="row in-sec-left-bansec">


                <?php
                // Display optional category description
                if ( category_description() ): ?>

                <div class="in-sec-banner">

                    <?php echo category_description(); ?>
                </div>
                <?php endif; ?>

            </div>
            <div class="row in-main-sec-post">
                <h4>Latest Post</h4>

                <?php
                // The Loop
                while ( have_posts() ): the_post();
                ?>


                <div class="col-sm-4">

                    <a href="<?php the_permalink();?>">
                        <div class="in-section-post-title">
                            <div class="bolg-img">
                                <?php the_post_thumbnail(); ?>
                            </div>
                            <h5>
                                <?php echo substr(get_the_title(), 0,18); ?> ...</h5>
                            <p>
                                <?php echo substr(get_the_content(), 0,70); ?> ...</p>

                            <h6> <img src="<?php bloginfo(\'template_url\'); ?>/images/date-icon.jpg"> <?php the_time(\'l, F jS, Y\') ?> <span> <img src="<?php bloginfo(\'template_url\'); ?>/images/view-icon.jpg"> <?php the_field(\'review\'); ?> </span></h6>
                        </div>
                    </a>

                </div>


                <?php endwhile; wp_reset_query(); ?>


            </div>
        </div>


        <div class="col-sm-4 right-section">
            <div class="col-sm-12 brand-review-global">

                <h3> Brands Review  </h3> // here I want to show post of B


                <?php query_posts(\'cat=12\'); while ( have_posts() ) : the_post(); $i++;?>
                <div class="brand-review">
                    <a href="<?php the_permalink();?>">
                        <?php the_post_thumbnail(); ?>
                    </a>
                    <h4> <a href="<?php the_permalink();?>"> <?php echo substr(get_the_title(), 0,29); ?>...</a></h4>
                    <p>
                        <?php //echo substr(get_the_content(), 0,80); ?>
                    </p>
                    <!--<a class="readmore-review" href="<?php //the_permalink();?>">Read Review...</a>-->
                </div>
                <?php endwhile; wp_reset_query(); ?>


            </div>
        </div>

    </div>

    <?php get_footer(); ?>

1 个回复
最合适的回答,由SO网友:nmr 整理而成

要仅显示当前类别中的帖子,而不显示子类别中的帖子,请使用pre_get_posts 要设置的动作挂钩\'include_children\' => FALSE 在里面tax_query 参数

add_action( \'pre_get_posts\', \'se342950_no_child_term_posts\' );

function se342950_no_child_term_posts( $query )
{
    if ( is_admin() || ! $query->is_main_query() || ! $query->is_category() )
        return;

    $tax_obj = $query->get_queried_object();
    $q_tax = [
        \'taxonomy\' => $tax_obj->taxonomy,
        \'terms\'    => $tax_obj->term_id,
        \'include_children\' => FALSE,
    ];
    $query->query_vars[\'tax_query\'][] = $q_tax;
}

您只能通过该功能从父类别获取帖子get_posts(), 其中也包括include_children 应设置为FALSE.

// in category.php "get_queried_object()" returns WP_Term object
$tax_obj = get_queried_object();
$posts_from_parent = [];
if ( $tax_obj->parent != 0 )
{
    $args = [
        \'post_type\' => \'post\',
        \'tax_query\' => [
            [
                \'taxonomy\' => $tax_obj->taxonomy,
                \'terms\'    => $tax_obj->parent,
                \'include_children\' => FALSE,
            ]
        ]
    ];
    $posts_from_parent = get_posts( $args );
}
更新显示子类别帖子。

类别。php

?>
<div class="col-sm-12 brand-review-global">
   <h3> Brands Review  </h3>  <!-- here I want to show post of B -->
<?php
global $post;

//
// get child category
$tax_obj = get_queried_object();
$children = get_terms([
    \'taxonomy\' => $tax_obj->taxonomy,
    \'parent\' => $tax_obj->term_id,
    \'fields\' => \'ids\',
]);
if ( is_array($children) && count($children) )
{
    $child_cat_id = array_shift( $children );
    //
    // get posts from child category
    $args = [
        \'post_type\' => \'post\',
        \'tax_query\' => [
            [
                \'taxonomy\' => $tax_obj->taxonomy,
                \'terms\'    => $child_cat_id,
                \'include_children\' => FALSE,
            ]
        ]
    ];
    $posts_from_child = get_posts( $args );

    //
    // display
    foreach( $posts_from_child as $post)
    {
        setup_postdata( $post );

        ?>
            <div class="brand-review">
                <a href="<?php the_permalink();?>">
                    <?php the_post_thumbnail(); ?>
                </a>
                <!-- other things: the_title(), the_excerpt(), ... -->
            </div>
        <?php

    }
    wp_reset_postdata();

}
else
{
    echo \'No data to display\';
}
?>
</div>
<?php