我正在寻找一个 wordpress 插件,它可以让我在侧边栏中添加一段特定于博文的段落。创建帖子时,我需要能够添加该文本。那里有这样的东西吗?我在搜索中一直没有成功。 谢谢
最佳答案
这可以使用自定义字段、文本小部件和短代码轻松解决。
这段代码放在主题的 functions.php
中,或者最好放在 custom plugin 中。 .
1) 为文本小部件启用短代码
add_filter( 'widget_text', 'do_shortcode' );
2)定义简码,阅读评论了解详情
add_shortcode( 'mytext', 'so_13735174_custom_text_widget' );
function so_13735174_custom_text_widget( $atts, $content = null )
{
global $post;
// $post contains lots of info
// Using $post->ID many more can be retrieved
// Here, we are getting the Custom Field named "text"
$html = get_post_meta( $post->ID, 'text', true );
// Custom Field not set or empty, print nothing
if( !isset( $html ) || '' == $html )
return '';
// Print Custom Field
return $html;
}
3) 在所需的侧边栏中添加文本小部件。
将标题留空并将简码放在内容中:[mytext]
。
4) 现在,每个带有名为 text
的自定义字段的页面或帖子都将在小部件中打印其值。
5) $html
可以很花哨,可以使用多个自定义字段。
关于用于在侧边栏中发布特定内容的 Wordpress 小部件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13735174/