在 Wordpress 的任何页面上显示特定父级的子页面 [英] Show Child-pages of Specific Parent on any page in Wordpress

查看:26
本文介绍了在 Wordpress 的任何页面上显示特定父级的子页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站是由很多家长和孩子建立的.

My website is build with alot of parents and childs.

我目前正在使用以下功能:https://gist.github.com/mprahweb/3c94d9c65b3020ec

I am currently using the follow function: https://gist.github.com/mprahweb/3c94d9c65b32ec9e3b2908305efd77d5

但是它没有达到我想要的.我只能在父级下使用.

However it doesnt achieve what i want. I can only use it under the parent.

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

I want to be able to use a shortcode to list a child-pages for a specfic parent on any page (mainly the homepage)

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

The dream would be that i could have a shortcode like this:

[wpb_childpages_xxx] 其中 xxx 等于父页面

[wpb_childpages_xxx] where xxx equals the parent page

这可能吗?

推荐答案

修改后的代码如下:

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,但如果指定 slugid 将被忽略:

Using the Shortcode — use either id or slug, but if you specify a slug, the id will be ignored:

  • 显示当前页面的子页面:
    [wpb_childpages/]

显示任何页面的子页面:

  1. 使用帖子 ID:[wpb_childpages id="{POST_ID}/]
    例如.[wpb_childpages id="123"/]

使用 post slug:[wpb_childpages slug="{SLUG}/]
例如.[wpb_childpages slug="home"/]

With the post slug: [wpb_childpages slug="{SLUG} /]
E.g. [wpb_childpages slug="home" /]

这篇关于在 Wordpress 的任何页面上显示特定父级的子页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆