【问题标题】:Wordpress, iterate child pages on templateWordpress,在模板上迭代子页面
【发布时间】:2017-07-21 17:47:46
【问题描述】:

我有这个模板来显示所有的新闻页面,它们都是 news_all 页面的子页面。 在 news_all 页面的模板里面有如下代码。

部分模板get-news-template.php:

<?php $the_query = new WP_Query(array(
                      'post_type'      => 'page',
                      'post_parent' => 143
                   ));
    while ($the_query->have_posts())
    {
        $the_query->the_post();
        echo get_the_title();
    }
    wp_reset_postdata();
?>

其中 143 是 news_all 页面的 id。 由于某种原因,这不起作用,我是否以错误的方式使用它?请帮忙。

我的最终目标是迭代并将所有新闻页面显示为侧边栏中的列表。

编辑:添加了 post_type,但仍然没有,此页面至少有两个子页面,它们应该得到回显,对吗?

【问题讨论】:

    标签: php wordpress templates


    【解决方案1】:

    Wordpress有page和post,需要page时必须在query中指定!

    $the_query = new WP_Query(array(
                      'post_type'      => 'page',
                      'post_parent' => 143
                   ));
    

    【讨论】:

    • 您确定您的页面是 id 为 143 的页面的父页面吗?您也可以尝试将您的 news_all 设置为 id 为 143 的页面模板并编写查询: $current_page_id = get_the_ID(); $the_query = new WP_Query(array( 'post_type' => 'page', 'post_parent' => $current_page_id ));
    • News all id = 143,我希望显示该页面的子页面,此模板适用于父页面,它应该迭代并显示所有子页面。
    • 嗯,我什么也没做,只是在吃东西,与此同时,这个脚本开始工作了。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多