首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Wordpress中的任意页面上显示特定父级的子页

在Wordpress中的任意页面上显示特定父级的子页
EN

Stack Overflow用户
提问于 2018-03-08 21:57:40
回答 1查看 1.1K关注 0票数 1

我的网站是建立与许多家长和孩子。

我目前正在使用以下函数:https://gist.github.com/mprahweb/3c94d9c65b32ec9e3b2908305efd77d5

然而,它并没有实现我想要的。我只能在父母下面使用。

我希望能够使用一个短代码在任何页面(主要是主页)上列出特定父级的子页。

我的梦想是,我可以有这样的一个短代码:

wpb_childpages_xxx,其中xxx等于父页面

这个是可能的吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-09 06:45:57

以下是修改后的代码:

代码语言:javascript
运行
复制
function wpb_list_child_pages( $atts = array() ) {
    $atts = shortcode_atts( array(
        'id'   => 0,
        'slug' => '',
    ), $atts );

    if ( $atts['slug'] ) {
        global $wpdb;

        $q = $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_name = %s", $atts['slug'] );
        $post_id = $wpdb->get_var( $q );

        if ( ! $post_id ) {
            return '';
        }
    } else {
        $post_id = absint( $atts['id'] );
    }

    $post = get_post( $post_id ); // WP_Post on success.
    $childpages = '';

    if ( $post && is_post_type_hierarchical( $post->post_type ) ) {
        $childpages = wp_list_pages( array(
            'child_of' => $post->ID,
            'title_li' => '',
            'echo'     => 0,
        ) );
    }

    if ( $childpages ) {
        $childpages = '<ul>' . $childpages . '</ul>';
    }

    return $childpages;
}
add_shortcode( 'wpb_childpages', 'wpb_list_child_pages' );

使用短代码-使用idslug,但是如果您指定了一个slug,则id将被忽略:

  • 若要显示当前页[wpb_childpages /]的子页,请执行以下操作
  • 显示任何页的子页
代码语言:javascript
运行
复制
1. With the post ID: `[wpb_childpages id="{POST_ID} /]`

,例如[wpb_childpages id="123" /]

2.使用后弹:[wpb_childpages slug="{SLUG} /]

例如[wpb_childpages slug="home" /]

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49183161

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档