使用shortcodes 在内容区域内添加php功能。或ob_start() 和ob_get_clean() 捕获输出并附加到$content 你回来了。
The
"the_content" 过滤器用于在从数据库检索到帖子内容之后,以及在将其打印到屏幕之前,过滤帖子内容。
add_filter( \'the_content\', \'my_the_content_filter\', 20 );
function my_the_content_filter($content) {
$post = $GLOBALS[\'post\'];
ob_start();
switch($post->post_type) {
case \'property\':
if(class_exists(\'MRP_Multi_Rating_API\')) {
MRP_Multi_Rating_API::display_rating_result(array(
\'rating_item_ids\' => 2,
\'show_count\' => false,
\'result_type\' => \'value_rt\',
\'no_rating_results_text\' => \'Not Rated\',
));
MRP_Multi_Rating_API::display_rating_result(array(
\'rating_item_ids\' => 5,
\'show_count\' => false,
\'result_type\' => \'overall_rt\',
\'no_rating_results_text\' => \'Not Rated\',
));
}
break;
}
$extra = ob_get_clean();
if( ! empty($extra)) {
return "<div>" . $content . $extra . "</div>";
}
return $content;
}
如果您只想在内容中插入内容,请使用
str_replace 在$post上。
$post = get_post(37);
// Update post 37
$my_post = array(
\'ID\' => $post->ID,
\'post_content\' => str_replace(\'round hole\', \'square peg\' . \' round hole\', $post->post_content),
);
// Update the post into the database
wp_update_post($my_post);