List current category/subcategory subcategories and posts

Asked

Viewed 691 times

2

In my file category.php I have a layout where I have a side menu where the subcategorias of categoria current and a <section></section> where the current posts should be displayed categoria and subcategoria, to generate the menu with 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 the section i made:

<?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’t get her to section always take the posts of the current page being her categoria or subcategoria, I can only catch the categorial current using get_the_category()[0]->term_id, and don’t know how to pick up the posts of the current subcategoria, how can I make posts is displayed according to the current categoria or subcategoria?

1 answer

0

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

This line is unnecessary and is breaking the template, 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 then the part cat=etc 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' );
    } 
}
  • By the way: https://naousequeryposts.wordpress.com/

  • Thanks Ricardo, when I try to assemble the menu I would like to always pick the category pai (main) because when I access the subcategories of the way I am writing I receive a array empty, I’m trying to use get_the_category() but I always get one back array emptiness as well.

  • get_the_category serves to search for categories associated with a specific post. If you want to know the category of the template, use single_term_title() to take the title or get_queried_object() to get the current object of the query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.