List subcategories and posts of current category / subcategory

1

In my file category.php I have a layout where I have a side menu where the subcategorias of the categoria current and a <section></section> should be displayed where the posts of the current categoria and subcategoria , to generate the menu with the subcategorias of categoria current I did:

$categories =  get_categories('child_of='.get_the_category()[0]->term_id);

<nav class="panel">
    <?php foreach($categories as $category): ?>
        <a href="<?= get_category_link($category->term_id); ?>" class="panel-block">
            <?= $category->name; ?>
            <span class="icon"><i class="fa fa-angle-right fa-fw"></i></span>
        </a>                
    <?php endforeach; ?>
</nav>

In section I did:

<?php query_posts('posts_per_page=-1&cat='.get_the_category()[0]->term_id); ?>
    <?php if (have_posts()): while(have_posts()): the_post(); ?>
        <article class="column is-half">
            <div class="price-label">
                <div class="price-title">
                    <h1 class="title is-6">0001. <?php the_title(); ?></h1>
                    <h2 class="subtitle is-6">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis.</h2>
                </div>
                <div class="price-tag has-text-right">
                    <p><span class="size">P</span> <small>R$</small> 21.90</p>
                    <p><span class="size">M</span> <small>R$</small> 25.90</p>
                    <p><span class="size">G</span> <small>R$</small> 35.90</p>
                </div>
            </div>
        </article>
    <?php endwhile; endif; ?>

I can not get section to always get the posts of the current page being categoria or subcategoria , I can only get the categorial current using get_the_category()[0]->term_id , and I do not know how to get the% posts from current subcategoria , how can I make posts to be displayed according to current categoria or subcategoria ?

    
asked by anonymous 09.07.2017 / 23:19

1 answer

0

You are rewriting the query within section , so it will always show the posts in the first category.

This line is unnecessary and is breaking the template, you can delete entire:

<?php query_posts('posts_per_page=-1&cat='.get_the_category()[0]->term_id); ?>

When the template is loaded, WP already knows which category / subcategory you want so the cat=etc part is unnecessary. To pass the parameter posts_per_page use the hook pre_get_posts in your functions.php

add_action( 'pre_get_posts', 'retira_paginacao' );
function retira_paginacao( $query ) {
    if( is_admin() ) {
        return;
    }

    if ( is_category() ) {
        $query->set( 'posts_per_page', '-1' );
    } 
}
    
10.07.2017 / 13:46