我的一个客户希望能够创建小块内容(称之为“pods”),并在整个网站的各个页面中(重新)使用它们。到目前为止,这是通过创建自定义帖子类型[图1]并将其中生成的页面通过快捷码[图2]调用到“容器”页面[图3]中来实现的。
问题是,搜索只会返回那些“pod”中的点击,而不会返回“container”页面。考虑到内容不是在“容器”页面中构建的,这是有道理的,但我更喜欢只在“容器”页面中显示搜索结果。我一直在寻找解决方案,认为我的问题不可能是新的,但没有用。这是可能的还是我找错了方向?
图1-自定义立柱类型
function create_content_pod_post_type() {
register_post_type( \'content_pod\',
array(
\'labels\' => array(
\'name\' => __( \'Content Pods\' ),
\'singular_name\' => __( \'Content Pod\' ),
\'menu_name\' => __( \'Content Pods\' ),
\'name_admin_bar\' => __( \'Pod\' ),
),
\'public\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => true,
\'has_archive\' => true,
\'menu_position\' => 20,
\'capability_type\' => \'page\'
)
);
}
add_action( \'init\', \'create_content_pod_post_type\' );
图2-显示“pod”内容的快捷码功能function get_pod_content( $atts ) {
$pod_id = $atts[ \'id\' ];
$args = array( \'page_id\' => $pod_id, \'post_type\' => \'content_pod\' );
$custom_query = new WP_Query( $args );
if($custom_query->have_posts()) {
while( $custom_query->have_posts() ) {
$custom_query->the_post();
echo \'<div class="content-pod">\' . get_the_content() . \'</div>\';
}
}
wp_reset_postdata();
}
add_shortcode( \'pod\', \'get_pod_content\' );
图3-“container”页面中的shortcode宏示例[pod id="135"]