categories - Parent categorychild category posts

admin2025-06-02  1

Not sure if this is the best approach - so happy to change - but I've set up a number of categories with child categories. Then on my landing page I list the parent categories - I'm using ACF to select the taxonomies I want -

        <ul>

        <?php foreach( $terms as $term ): ?>

        <h2><?php echo $term->name; ?></h2>
        <p><?php echo $term->description; ?></p>


        <a href="<?php echo get_term_link( $term ); ?>">View all '<?php echo $term->name; ?>' posts</a>

        <?php endforeach; ?>


        </ul>

        <?php endif; ?>

Each link above takes me to categories.php where I list the child categories:

        <?php 

                $term = get_queried_object();

                $children = get_terms( $term->taxonomy, array(
                    'parent'    => $term->term_id,
                    'hide_empty' => false
                ) );

                if ( $children ) { 

                foreach( $children as $subcat )
                {

                echo '<li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li>';
                }


                }


        ?>

Problem is the link attached to the child categories stays on the category.php page and not the child category post.

How do I direct the child category link to the actual post?

Heres how I though it should flow:

  1. Landing page (containing list of parent categories)
  2. Category.php (containing child categories)
  3. Child post

Not sure if this is the best approach - so happy to change - but I've set up a number of categories with child categories. Then on my landing page I list the parent categories - I'm using ACF to select the taxonomies I want -

        <ul>

        <?php foreach( $terms as $term ): ?>

        <h2><?php echo $term->name; ?></h2>
        <p><?php echo $term->description; ?></p>


        <a href="<?php echo get_term_link( $term ); ?>">View all '<?php echo $term->name; ?>' posts</a>

        <?php endforeach; ?>


        </ul>

        <?php endif; ?>

Each link above takes me to categories.php where I list the child categories:

        <?php 

                $term = get_queried_object();

                $children = get_terms( $term->taxonomy, array(
                    'parent'    => $term->term_id,
                    'hide_empty' => false
                ) );

                if ( $children ) { 

                foreach( $children as $subcat )
                {

                echo '<li><a href="' . esc_url(get_term_link($subcat, $subcat->taxonomy)) . '">' . $subcat->name . '</a></li>';
                }


                }


        ?>

Problem is the link attached to the child categories stays on the category.php page and not the child category post.

How do I direct the child category link to the actual post?

Heres how I though it should flow:

  1. Landing page (containing list of parent categories)
  2. Category.php (containing child categories)
  3. Child post
Share Improve this question edited Mar 15, 2019 at 3:57 Paul asked Mar 15, 2019 at 3:31 PaulPaul 115 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

OK solved!

I have a post called "Cardio" with a child category name "Cardio".

I created a category-cardio.php (cardio is the child category) but the page doesn't display the post "cardio" same name as the category.

<?php
/**
 * Cardio category template
 *
 * @package clf
 */

get_header(member);

?>
    <div id="primary" class="content-area">
        <main id="main" class="site-main">

<h1>category-cardio.php</h1>

    <?php $args = array(
        'categroy_name' => 'cardio',    
    ) ?>

    <?php $the_query = new WP_Query( $args ); ?>

    <?php if ($the_query->have_posts()) : ?>

    <?php while ($the_query->have_posts()) : $the_query->the_post();

                echo the_content();

            endwhile;

        endif;
        ?>


        </main><!-- #main -->
    </div><!-- #primary -->

<?php
get_sidebar();
get_footer();

It's normal behaviour. ctaegory.php is the template meant to display posts of a category (parent as well as child-categories), but you are using it to display child-categories only. Posts are not shown because there is no code to display posts.

You can get a list of posts by appending a WP loop to the category.php file.

// After your current code for displaying child categories
// Main loop
if( have_posts() ){
    while ( have_posts() ){
       the_post();
      //    Display post title, contents etc here

    }
 }

I hope this helps.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748804328a313857.html

最新回复(0)