在WordPress的任何页面上显示特定父级的子页面

我的网站由很多父母和孩子组成。

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

但是它没有实现我想要的。我只能在父项下使用它。

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

梦想是我可以拥有这样的简码:

[wpb_childpages_xxx]其中xxx等于父页面

这可能吗?


#1


这是修改后的代码:

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' );

使用简码-使用id或slug, 但是如果你指定了Slug, 则ID将被忽略:

要显示当前页面的子页面:

[wpb_childpages /]

要显示任何页面的子页面:

  1. 使用帖子ID:[wpb_childpages id =” {POST_ID} /], 例如[wpb_childpages id =” 123″ /]
  2. 使用帖子slug:[wpb_childpages slug =” {SLUG} /]例如[wpb_childpages slug =” home” /]

来源:

https://www.srcmini02.com/65814.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_40768.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?