子页面和自定义分类

时间:2014-06-02 作者:user3700046

我为这件事紧张了一段时间。我已经知道如何使用自定义分类法在循环中调用页面。我还了解了如何在循环中调用父页的子页。然而,我想做的是调用子页面,然后过滤掉属于自定义分类法的子页面,以便只显示这些子页面。

提前感谢您的帮助,

我可以提供更多需要的信息,

子页面代码:

<?php

$child_pages = $wpdb->get_results(
    "SELECT * FROM $wpdb->posts WHERE post_parent = $post->ID AND post_type = \'page\' ORDER BY menu_order"
);

if ( $child_pages ) :
    foreach ( $child_pages as $pageChild ) :
        setup_postdata( $pageChild );

        ?>

        <div class="comparison-post-block">
            <div class="wrapper">
                <div class="top-5-image">
                    <?php echo get_the_post_thumbnail( $pageChild->ID, \'thumbnail\' ) ?>
                </div>
            </div>
            <div class="comparison-top5-text">
                <div class="wrapper">
                    <div class="top-5-title">
                        <p class="comparisontitle">
                            <a class="comparisontitle" href="<?php echo get_permalink( $pageChild->ID ) ?>" rel="bookmark" title="<?php echo $pageChild->post_title ?>">
                                <?php echo $pageChild->post_title ?>
                             </a>
                        </p>
                    </div>
                </div>
                <div class="wrapper">
                    <div class="comparison-content">
                        <div class="comparisonexcerpttext">
                            <?php echo get_the_excerpt() ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    <?php endforeach ?>
<?php endif ?>
自定义分类代码:

<?php 

if ( is_page() ) :
    global $post; 

    $args = array( 
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'top5\',
                \'field\' => \'name\',
                \'terms\' => $post->post_title
            ),
        ),
    );

    $My_Query = new WP_Query( $args );

    if ( $My_Query->have_posts() ) :
        while ( $My_Query->have_posts() ) :
            $My_Query->the_post();
            ?>

            <div class="comparison-stories">
                <div class="comparison-post-block">
                    <div class="wrapper">
                        <div class="top-5-image">
                            <?php the_post_thumbnail( \'medium\' ) ?>
                        </div>
                    </div>
                    <div class="comparison-top5-text">
                        <div class="wrapper">
                            <div class="top-5-title">
                                <p class="comparisontitle">
                                    <a class="comparisontitle" href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
                                        <?php the_title(); ?>
                                    </a>
                                </p>
                            </div>
                        </div>  
                        <div class="wrapper">
                            <div class="comparison-content">
                                <div class="comparisonexcerpttext">
                                    <?php the_excerpt() ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

        <?php endwhile ?>

    <?php wp_reset_postdata() ?>
    <?php else : ?>

    <?php endif ?>
<?php endif ?>
据我所知,我已将所有代码复制到这里,

很抱歉,这是我的第一篇帖子,所以我不太确定您想要的代码是什么,所以我想结合这两段代码!

再次感谢!

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

您可以获取所有孩子的ID并设置post__in 税务查询的参数:

$child_ids = $wpdb->get_col(
    "SELECT ID FROM $wpdb->posts WHERE post_parent = $post->ID AND post_type = \'page\' ORDER BY menu_order"
);

$args = array(
    \'post__in\' => $child_ids, // Only retrieve taxonomy posts that are children of this page
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'top5\',
            \'field\' => \'name\',
            \'terms\' => $post->post_title
        ),
    ),
);
或者,如果您只想要那些不是页面子级的帖子,您可以使用参数\'post_parent__not_in\' => array( $post->ID ).

结束

相关推荐

How to list Custom Taxonomy

我有一个WordPress分类法,我想知道是否有这样的分类法: <ul id=\"portfolioFilter\"> <li class=\"filter\" data-filter=\"all\">All</li> <li class=\"filter\" data-filter=\"categ