0

I have a list of categories page, and i want if i clicked on the category it will take me to list of post that using the category.
I know this confusing, but i give example sitemap

  • Parent Category 1
    • Child Category
    • List of Post
  • Parent Category 2
    • Child Category A
    • Sub Child Category A
      • List of Post
    • Child Category B
  • Parent Category 3

So Parent will have 1 page
Child Category will have 1 page Sub Child Category will have 1 page And list of Post will have 1 page also.

here's my recent function.php code

public static function get_categories_list() {
        ob_start();

        $ids = get_queried_object()->term_id;
        $categories = get_terms( 'category', array(
            'order'    => 'ASC',
            'hide_empty' => true,
            'parent'         => $ids,
        ));
        $args = array(
            'post_type'     => 'product',
            'post_status'   => 'publish',
            'tax_query'     => array(
                array(
                    'taxonomy'  => 'category',
                    'field'         => 'term_taxonomy_id',
                    'terms'         => $ids,
                ),
            ),
        );
        $_posts = new WP_Query( $args );

        if ( $_posts->have_posts() ) {
            $_posts->the_post();
            $post_id = get_the_ID();
            ?><div class="col">
            <a href="<?php echo the_permalink(); ?>" style="background-image: url(<?php echo $image[0]; ?>);">
                <div class="content">
                    <h3><?php echo get_the_title(); ?></h3>
                </div>
            </a>
        </div><?php
        }
        $index = 1;
        foreach($categories as $category) {
            $image = get_field('featured_image', $category);

            if($category->parent == $ids) {
                if($index % 2){ ?>
                    <div class="row">
                            <div id="product-cat <?php echo $category->slug ?>">
                                    <div class="two-col product-cat-image">
                                            <img src="<?php echo $image ?>">
                                    </div>
                                    <div class="two-col product-cat-details">
                                            <?php
                                                    echo '<h4>'. $category->name .'</h4>';
                                                    echo '<p>'. $category->description .'</p>';
                                                    echo '<a class="product-cat-button" href="' . get_term_link( $category ) . '">See Products</a>';
                                            ?>
                                    </div>
                            </div>
                    </div>
                <?php } else {?>
                        <div class="row">
                                <div id="product-cat <?php echo $category->slug ?>">
                                        <div class="two-col product-cat-details text-left">
                                                <?php
                                                        echo '<h4>'. $category->name .'</h4>';
                                                        echo '<p>'. $category->description .'</p>';
                                                        echo '<a class="product-cat-button" href="' . get_term_link( $category ) . '">See Products</a>';
                                                ?>
                                        </div>
                                        <div class="two-col product-cat-image">
                                                <img src="<?php echo $image ?>">
                                        </div>
                                </div>
                        </div>
                <?php }
                    $index++;
                }
            }
        return ob_get_clean();
    }

This one working, but not like what i expected. The list of post also showing on category list page. preview I just want list of product will be showing after i clicked on sub category button (which is single page for list of post)

Your Answer

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

Browse other questions tagged or ask your own question.