我的网站是建立与许多家长和孩子。
我目前正在使用以下函数:https://gist.github.com/mprahweb/3c94d9c65b32ec9e3b2908305efd77d5
然而,它并没有实现我想要的。我只能在父母下面使用。
我希望能够使用一个短代码在任何页面(主要是主页)上列出特定父级的子页。
我的梦想是,我可以有这样的一个短代码:
wpb_childpages_xxx,其中xxx等于父页面
这个是可能的吗?
发布于 2018-03-09 06:45:57
以下是修改后的代码:
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. With the post ID: `[wpb_childpages id="{POST_ID} /]`,例如[wpb_childpages id="123" /]
2.使用后弹:[wpb_childpages slug="{SLUG} /]
例如[wpb_childpages slug="home" /]
https://stackoverflow.com/questions/49183161
复制相似问题