admin管理员组

文章数量:1421054

I have category menu, if opening category.php in left side only display post of current parent category, and in right hand side show child category post.

If I have two category. A and B. A is parent of B b is child.

In left Side want to show all post of Only A, Right side Want to Show post of Only.

Both action Should be perform on same template.

<?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(); ?>

I have category menu, if opening category.php in left side only display post of current parent category, and in right hand side show child category post.

If I have two category. A and B. A is parent of B b is child.

In left Side want to show all post of Only A, Right side Want to Show post of Only.

Both action Should be perform on same template.

<?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(); ?>
Share Improve this question edited Jul 17, 2019 at 13:41 nmr 4,5672 gold badges17 silver badges25 bronze badges asked Jul 16, 2019 at 5:15 AdityaAditya 32 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

To display posts only from current category, without posts from sub-categories, use pre_get_posts action hook to set 'include_children' => FALSE in tax_query parameter.

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;
}

Posts from only the parent category you can get with the function get_posts(), in which also include_children should be set to 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 );
}

Update

Display child category posts.

category.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

本文标签: