0
votes

I have created some categories for my Wordpress pages, some of which have subcategories and some of those have subcategories of their own. What I want is to display in each category page(template created with category.php file) all pages(!not posts, my categories are for pages!!!) that are of that parent category but also of its subcategories no matter the tree depth. That means when I navigate to a subcategory the same happens - pages of that category and subcategories but not of the previous parent category. It is important to note that parent categories might have pages that are of that category only and not of a subcategory so they should not display in child category pages!. This is the code I am using:

<?php
/**
*Template Name: Overview
*
*/
get_header();
the_post();?>


$post = get_post();
            if ( $post ) {
                $categories = get_the_category( $post->ID );
            }
            $separator = '&nbsp;&nbsp;&nbsp;   /&nbsp;&nbsp;&nbsp;   ';
            $output = '';
            if ( ! empty( $categories ) ) {
                foreach( array_reverse($categories) as $category ) {
                    $output .= '<a style="font-size: 12px;font-weight: 100;letter-spacing: 0.3px;" href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
                }
                //echo trim( $output, $separator );
            }


$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'post_type' => 'page',
            'post_status' => 'publish',
            'meta_key' => '_wp_page_template',
            'meta_value' => 'post.php',
            'category_name' => esc_html( $category->name ),
            'posts_per_page' => 12,
            'paged' => $paged,
        );
        $arr_posts = new WP_Query( $args );
        if ( $arr_posts->have_posts() ) :

            while ( $arr_posts->have_posts() ) :
                $arr_posts->the_post();

                ?>

This code works to a point but behaves in a very erratic way, not always correctly. To be specific it displays the pages correctly only if the last page created was of the parent category. If the last page created was of a subcategory, it is as if the other pages do not exist and it just displays this last page on its own. Also I want to note that I want there to be pagination with 12 pages per category page.

1

1 Answers

0
votes

Thanks to another post I figured it out. https://stackoverflow.com/a/7738746/11460305

$currentCategory = single_cat_title("", false);
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
        $args = array(
            'post_type' => 'page',
            'post_status' => 'publish',
            'meta_key' => '_wp_page_template',
            'meta_value' => 'post.php',
            'category_name' => $currentCategory,
            'posts_per_page' => 12,
            'paged' => $paged,
        );
        $arr_posts = new WP_Query( $args );