查询下级和上级职称

时间:2019-01-31 作者:baalstorm

我正在查询页面ID的子页面。如何回显显示“父标题”的父页面?使用<?php echo get_the_title( $ID ); ?> 不是选项。

<?php

$args = array(
    \'post_type\'      => \'page\', //write slug of post type
    \'posts_per_page\' => -1,
    \'post_parent\'    => \'2\', //place here id of your parent page
    \'order\'          => \'ASC\',
    \'orderby\'        => \'menu_order\'
 );


$childrens = new WP_Query( $args );

if ( $childrens->have_posts() ) : ?>


    <div class="content">
        <h2 class="big grid_3">PARENT TITLE</h2>
        <div class="grid_6 right">
        <?php while ( $childrens->have_posts() ) : $childrens->the_post(); ?>

        <div class="grid_2 left" id="teaser" >
            <p class="subtitle"></p>
            <h3><?php the_title(); ?></h3>
            <?php the_excerpt(); ?>
            <a class="cta_light" href="<?php the_permalink(); ?>">Mehr erfahren</a>

        </div>

    </div>
    </div>
<?php
        endwhile; 
endif; 
wp_reset_query(); 
?>
谢谢!

1 个回复
SO网友:Krzysiek Dróżdż

我不完全确定我是否正确地理解了你的问题,因为“这不是一个可选的部分”,但是。。。

您将获得ID为2的页面的子页面,因此看起来您希望显示该页面的标题。如果是这样,那么此代码将解决您的问题:

<?php
    $args = array(
        \'post_type\'      => \'page\', //write slug of post type
        \'posts_per_page\' => -1,
        \'post_parent\'    => \'2\', //place here id of your parent page
        \'order\'          => \'ASC\',
        \'orderby\'        => \'menu_order\'
    );

    $childrens = new WP_Query( $args );

    if ( $childrens->have_posts() ) :
?>
    <div class="content">
        <h2 class="big grid_3"><?php echo get_the_title(2); ?></h2>
        <div class="grid_6 right">
            <?php while ( $childrens->have_posts() ) : $childrens->the_post(); ?>
            <div class="grid_2 left" id="teaser" >
                <p class="subtitle"></p>
                <h3><?php the_title(); ?></h3>
                <?php the_excerpt(); ?>
                <a class="cta_light" href="<?php the_permalink(); ?>">Mehr erfahren</a>
            </div>
            <?php endwhile; // <- I had to move that, it was in wrong place so you were closing more divs than you were opening in that loop ?>
        </div>
    </div><!-- .content -->
<?php
    endif; 
    // wp_reset_query();  <- there is no point in resetting query - you haven\'t changed it
    wp_reset_postdata(); // on the other hand you should reset postdata
?>
另外,看看我的评论——我不得不对你的代码做一些更改,因为它生成了错误的HTML输出。

相关推荐

Calculations in functions.php

我为自己创建了一个发票主题,并试图在自定义列中获取发票总额。我已经尝试调整模板页面中使用的代码,以便在函数中显示这一点。php文件,但它不工作。我的乘法操作数出现了一个操作数错误,我不知道为什么。这是我的代码(如有任何帮助,将不胜感激)。if( $column_name == \'invoice_total\' ) { $hours = 0; // This allows counting of each sub_field //* Set repeater va