用于在侧边栏中发布特定内容的 Wordpress 小部件

标签 wordpress

我正在寻找一个 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/

相关文章:

wordpress - 限制站点管理员可以添加到 WP woocommerce 的产品数量

php - Wordpress 登录页面重定向到普通用户的 Woocommerce 我的帐户页面

php - 我如何使用 wp_list_pages 将值 ="1"添加到当前 li

php - 如何将帖子从旧的自定义遗留博客迁移到新的 WordPress 网站?

php - 在 Wordpress 中向 get_the_date 添加一天

wordpress - 自定义页面与插件 Slug

Javascript 在价格中添加逗号而不删除 DIV

php mysql - 获取产品的产品缩略图

php - SSL 破坏了 Facebook 的点赞按钮

javascript - 未登录用户的“登录”值和登录用户的 'Log Out' 值