每页的社交分享插件

时间:2016-08-18 10:04:50

标签: php wordpress hook

我最近一直在开发一个社交分享插件,它坚持在页面的右侧(固定的CSS)。

我一直在使用钩子' the_content'显示侧边栏,但我确定大家都知道,这会显示帖子上的侧边栏,但不显示类别页面/博客供稿。

我尝试过使用' the_excerpt'在博客供稿/类别页面上显示侧边栏,但我觉得有一种更简单的方法可以达到相同的结果。

这是我使用插件的目标的一个很好的例子:https://wordpress.org/plugins/custom-share-buttons-with-floating-sidebar/

这是插件的前端显示部分(我想象的问题出在哪里):

function add_social_share_icons($content) {

    $html = "<ul id='social-sidebar'>";

    global $post;

    $url = get_permalink($post->ID);
    $url = esc_url($url);

    if(get_option("social-share-facebook") == 1)
    {
        $html = $html . "<li><a class='fa fa-facebook' target='_blank' href='http://www.facebook.com/sharer.php?u=" . $url . "'><span>Facebook</span></a></li>";
    }

    if(get_option("social-share-twitter") == 1)
    {
        $html = $html . "<li><a class='fa fa-twitter' target='_blank' href='https://twitter.com/share?url=" . $url . "'><span>Twitter</a></li>";
    }

    if(get_option("social-share-linkedin") == 1)
    {
        $html = $html . "<li><a class='fa fa-linkedin linkedin' target='_blank' href='http://www.linkedin.com/shareArticle?url=" . $url . "'><span>LinkedIn</span></a></li>";
    }

    if(get_option("social-share-google") == 1)
    {
        $html = $html . "<li><a class='fa fa-google gplus' target='_blank' href='https://plus.google.com/share?url=" . $url . "'><span>Google+</span></a></li>";
    }

    if(get_option("social-share-mail") == 1)
    {
        $html = $html . "<li><a class='fa fa-envelope github' href='mailto:?body=" . $url . "'><span>Email</span></a></li>";
    }

    $html = $html . "</ul>";

    return $content =  $html . $content;

}
add_filter("the_content", "add_social_share_icons");

非常感谢任何帮助。

编辑:我尝试过:add_action("wp_footer", "add_social_share_icons");但是没有运气,我还检查了页脚文件,认为wp_footer()可能不存在,但肯定包括在那里。

1 个答案:

答案 0 :(得分:0)

如果目标是分享指向“当前页面”的链接,您可以随时将其挂钩到wp_footer()

相关问题