如何在类别页面中显示该父类别及其子类别的所有页面

时间:2019-09-18 14:05:27

标签: php wordpress categories custom-wordpress-pages

我为我的Wordpress页面创建了一些类别,其中一些具有子类别,而有些具有其自己的子类别。我想要的是显示在每个类别页面(使用category.php文件创建的模板)中该父类别的所有页面(!不是帖子,我的类别是针对页面的!!! )但无论树的深度如何,其子类别也是如此。这意味着,当我导航到子类别时,也会发生相同的情况-该类别和子类别的页面,而不是先前父类别的页面。重要的是要注意,父类别可能只有该类别的页面而不是子类别的页面,因此它们不应显示在子类别页面中! 这是我正在使用的代码:

<?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();

                ?>

这段代码可以工作到一定程度,但是表现得很不稳定,并非总是正确的。 具体来说,只有最后创建的页面属于父类别时,它才能正确显示页面。 如果最后创建的页面属于一个子类别,就好像其他页面不存在一样,它仅显示最后一个页面。 我还要指出,我希望每个类别页面有12页的分页。

1 个答案:

答案 0 :(得分:0)

我想出了另一篇文章。 https://stackoverflow.com/a/7738746/11460305

<Container>
   <Row>
     <Col md={4}>md=4</Col>
     <Col md={{ span: 4, offset: 4 }}>{md={{ span: 4, offset: 4 }}} 
     </Col>
    </Row>.....
相关问题