我的函数中有一个函数。为作者框编译作者信息的php。函数结束时return $content;
然后我将其添加到如下内容的末尾
// Add our function to the post content filter
add_action( \'the_content\', \'author_info_box\' );
// Allow HTML
remove_filter(\'pre_user_description\', \'wp_filter_kses\');
问题是,我需要在评论的正上方打印出来,而不是在内容之后打印出来。我还有一些其他代码,我正在将其放入一个页面模板中,我想显示在作者框上方。通过将作者框附加到内容,我无法将其他代码插入模板。
Edit: The full function code:function author_info_box( $content ) {
global $post;
// Detect if it is a single post with a post author
if ( is_single() && isset( $post->post_author ) ) {
// Get author\'s display name
$display_name = get_the_author_meta( \'display_name\', $post->post_author );
// Get author\'s website URL
$user_website = get_the_author_meta(\'url\', $post->post_author);
// Pass all this info to post content
$content = $content . \'<footer class="author_bio_section" >\' . $author_details . \'</footer>\';
}
return $content;
}
// Add our function to the post content filter
add_action( \'comment_post\', \'author_info_box\' );
// Allow HTML in author bio section
remove_filter(\'pre_user_description\', \'wp_filter_kses\');
最合适的回答,由SO网友:stims 整理而成
您可以将函数更改为echo,如下所示:
echo apply_filters(\'the_content\', $content);
然后在您的模板中,就在注释模板之前,调用您的作者框函数,如下所示:
author_info_box();
此外,您还需要删除
add_action(\'the_content\', \'author_info_box\');
完整功能示例:
function author_info_box() {
global $post;
// Detect if it is a single post with a post author
if ( isset( $post->post_author ) ) {
// Get author\'s display name
$display_name = get_the_author_meta( \'display_name\', $post->post_author );
// Get author\'s website URL
$user_website = get_the_author_meta(\'url\', $post->post_author);
// Pass all this info to post content
$content = \'<footer class="author_bio_section" >\' . $author_details . \'</footer>\';
echo apply_filters(\'the_content\', $content);
}
}
注:
apply_filters
迭代所有注册到设置的过滤器的函数(在本例中,
the_content
). 完成最后一个功能后,它将返回“过滤”内容,因此需要echo将其打印出来。