0

I have custom taxonomy "oferta" and custom post type "produkt".

An "oferta" is needed to create categories and subcategories for posts in "produkt".

I have a lot of categories and subcategories and I need to show pages according to the scheme:

  • First I see main categories only
  • When I choose one I see subcategories which belongs only to that category I choosed.
  • Next I can choose another subcategory and so on.
  • And on the last page I see list of posts which belongs to that particular subcategory only.

In addition, I need to hide subcategories that do not have posts.

In taxonomy-oferta.php I have code:

<section id="primary" class="site-content">
    <div id="content" role="main">

        <header class="entry-header">
            <h1 class="entry-title"><?php single_cat_title(); ?></h1>
        </header>

        <?php
        $term = get_queried_object();
        $term_id = $term->term_id;
        $taxonomy_name = $term->taxonomy;

        $termchildren = get_term_children( $term_id, $taxonomy_name );

        echo '<ul>';
        foreach ( $termchildren as $child ) {
            $term = get_term_by( 'id', $child, $taxonomy_name );
            echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name . '</a></li>';
        }
        echo '</ul>'; ?>




    <?php if ( have_posts() ) : ?>

        <?php
        while ( have_posts() ) : the_post();

            get_template_part( 'content', 'oferta' );

        endwhile;

            twentytwelve_content_nav( 'nav-below' );
        ?>

    <?php else : ?>
        <?php get_template_part( 'content', 'none' ); ?>
    <?php endif; ?>

    </div>
</section>

Effect is that I see everyting at once:

  • Main categories
  • Subcategories
  • And below are all posts

When I click on subcategory I see all the sub-subcategories of that one and below all the posts from sub- and sub-subcategories.

Can anyone help me with this code to get the effect I described at the beginning?

1

You just need to tweak the logic so that if ( have_posts() ) : is only run if get_term_children() is empty, which it will be on the lowest level:

$term = get_queried_object();
$term_id = $term->term_id;
$taxonomy_name = $term->taxonomy;

$termchildren = get_term_children( $term_id, $taxonomy_name );

if ( ! empty( $termchildren ) ) {
    echo '<ul>';

    foreach ( $termchildren as $child ) {
        $term = get_term_by( 'id', $child, $taxonomy_name );
        echo '<li><a href="' . get_term_link( $term, $taxonomy_name ) . '">' . $term->name . '</a></li>';
    }

    echo '</ul>';
} elseif ( have_posts() ) {
    while ( have_posts() ) : the_post();
        get_template_part( 'content', 'oferta' );
    endwhile;

    twentytwelve_content_nav( 'nav-below' );
} else {
    get_template_part( 'content', 'none' ); 
}

In that example I added if ( ! empty( $termchildren ) ) and changed if ( have_posts() ) to elseif ( have_posts() ).

  • 1
    Thanks Jacob! Almost there :-) Now posts are displayed where they should be - in the last subcategories. But I have one more puzzle here. When I display the first page, which is the list of main categories - I can see sub-categories there as well. Subcategories should be visible only after selecting one of the main categories. Could you please advise? – Bociakrodyl Mar 21 '18 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.