【问题标题】:Wordpress short code to display all child page by parents IDWordpress 短代码按父母 ID 显示所有子页面
【发布时间】:2018-08-24 03:57:15
【问题描述】:

我正在编写一个通过父 ID 循环并显示所有子页面的短代码,但我不太确定如何制作循环并使其更具自定义性。

这是我的代码: add_shortcode('home-page-listing', 'get_list');

function get_list( $atts ) {
    ob_start();
    $atts = shortcode_atts( array(
            'ids' => ''
    ), $atts );
    if($atts['ids']!='')
    {
        $id_array = explode(',',$atts['ids']);
        $homePages = new WP_Query( array(
                'post_type' => 'page',
                'post__in'=>$id_array,
                'order' => 'ASC',
                'orderby' => 'post__in',
                'posts_per_page' => -1
        ) );
        if ($homePages->have_posts()){?>
            <div class="">

                <?php while ( $homePages->have_posts() ) : $homePages->the_post(); ?>
                     //here's html template code
                <?php endwhile;
                wp_reset_postdata(); ?>
            </div>
        }
    }
}

现在我可以使用[home-page-listing id=1,2,3,4]显示所有选择页面ID,但我想这样:

[home-page-listing parentID=4]

遍历所有子页面并显示到字体,而不是检查所有要显示的页面ID。

谢谢!

【问题讨论】:

    标签: php wordpress loops shortcode


    【解决方案1】:

    使用 post_parent 的参数很简单 WP_Query。请查看以下示例

    $homePages = new WP_Query( array(
        'post_type' => 'page',
        'post_parent'=>$parentID,
        'order' => 'ASC',
        'orderby' => 'parent',
        'posts_per_page' => -1
    ) );
    

    WP_Query的更多信息click here

    【讨论】:

    • 非常感谢@Ankur Bhadania 的帮助,我会在这几天尝试添加它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-26
    • 1970-01-01
    相关资源
    最近更新 更多