我不知道它是否是最佳实践,但当我遇到类似问题时,我使用了\\u内容挂钩来检查帖子类型,看看它是否是我的自定义类型,如果是,我返回了我想要的内容。例如:
add_filter(\'the_content\',\'events_conetnt_display\');
function events_conetnt_display($content){
global $post;
if (!$post_type == "events"){
return $content;
}else{
remove_filter( \'the_content\', \'events_conetnt_display\' );
$events_meta = get_post_custom($post->ID);
$new_content = \'<div class="event_container">\';
$new_content .= \'<div class="event_title">\'.get_the_title($post->ID).\'</div>\';
$new_content .= \'<div class="event_description">\'.apply_filters(\'the_content\',get_the_content()).\'</div>\';
$new_content .= \'<div class="event_start_date">\'.$events_meta[\'start_date\'].\'</div>\';
$new_content .= \'<div class="event_end_date">\'.$events_meta[\'start_end\'].\'</div>\';
//...
//whatever
//...
add_filter(\'the_content\',\'events_conetnt_display\');
return $new_content;
}
}
我的插件有一个选项,让用户决定是使用什么内容挂钩,还是有一个自定义模板,比如:
if (get_option(\'use_content_template\')){
add_filter(\'the_content\',\'events_conetnt_display\');
}