【问题标题】:How to put socail share button in wordpress post under the post excerpt?如何在帖子摘录下的wordpress帖子中放置社交分享按钮?
【发布时间】:2015-12-26 03:27:42
【问题描述】:

请告诉我是否有任何插件可以将社交分享按钮放在 wordpress 帖子摘录下?请查看此图像以了解我想要什么。谢谢

【问题讨论】:

  • 您是否尝试过任何研究或代码方面的尝试来实现这一目标?

标签: wordpress button plugins social


【解决方案1】:

您可以使用名为 Share Buttons 的 WordPress 插件来实现此目的。安装后,激活并选择要添加的图标。将以下内容添加到带有摘录的页面(在调用摘录之后)。

<?php if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>

代码类似于:

<?php the_excerpt(); ?>
<?php if ( function_exists( 'ADDTOANY_SHARE_SAVE_KIT' ) ) { ADDTOANY_SHARE_SAVE_KIT(); } ?>

可能会有所不同,具体取决于您完成摘录的方式。

【讨论】:

    【解决方案2】:

    这是另一种使用泛型的方法

    首先,在您的主题中下载并安装genericon pack。将genericon样式表加入队列

    function enqueue_genericon_style() {
          wp_enqueue_style( 'genericons', get_template_directory_uri() . '/genericons/genericons.css', array(), '3.0.2' );
    }
    
    add_action( 'wp_enqueue_scripts', 'enqueue_genericon_style' );
    

    将以下内容添加到您的functions.php。这个函数通过添加分享 url 和泛型来完成所有艰苦的工作

    function pietergoosen_social_share_buttons() {
        $services = array (
            'facebook' => array (
                'url'  => 'https://www.facebook.com/sharer/sharer.php?u=%1$s',
                'text' => esc_attr(__('Share on Facebook.', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-facebook"></span>'
            ),
            'twitter' => array (
                'url'  => 'http://twitter.com/home/?status=%1$s%%20-%%20%2$s',
                'text' => esc_attr(__('Tweet this post!', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-twitter"></span>'
            ),
            'googleplus' => array (
                'url'  => 'https://plus.google.com/share?url=%1$s',
                'text' => esc_attr(__('Google+1.', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-googleplus"></span>'
            ),
            'linkedin' => array (
                'url'  => 'https://www.linkedin.com/shareArticle?mini=true&url=%1$s&amp;title=%2$s&amp;summary=%3$s&amp;source=%4$s',
                'text' => esc_attr(__('linkedin.', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-linkedin"></span>'
            ),
            'reddit' => array (
                'url'  => 'http://reddit.com/submit?url=%1$s&amp;title=%2$s',
                'text' => esc_attr(__('Reddit.', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-reddit"></span>'
            ),
            'stumbleupon' => array (
                'url'  => 'http://www.stumbleupon.com/submit?url=%1$s&amp;title=%2$s',
                'text' => esc_attr(__('StumbleUpon.', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-stumbleupon"></span>'
            ),
            'digg' => array (
                'url'  => 'http://digg.com/submit?phase=2&amp;url=%1$s&amp;title=%2$s',
                'text' => esc_attr(__('Digg this post!', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-digg"></span>'
            ),
            'gmail' => array (
                'url'  => 'https://mail.google.com/mail/?view=cm&amp;fs=1&amp;to&amp;su=%1$s&amp;t=%2$s',
                'text' => esc_attr(__('Share with Gmail', 'pietergoosen' )),
                'icon' => '<span class="genericon genericon-mail"></span>'
            )
        );
    
        $title    = the_title_attribute( array ( 'echo' => FALSE ) );
        $url      = urlencode( get_permalink() );
        $summary  = the_excerpt();
        $source   = '';
    
        print '<h4>' . __( 'Share this post with others', 'pietergoosen' ) . '</h4>';
    
        echo '<div class="socialgenericons service">';
    
            foreach ( $services as $name  => $service )
            {
                $href = sprintf( $service['url'], $url, urlencode( $title ), urlencode( $summary ), urlencode( $source ) );
                $genericon = $service['icon'];
    
                printf(
                    '<a href="%1$s" title="%2$s" alt="%2$s">%3$s</a>',
                    $href,
                    $service['text'],
                    $genericon
                );
            }
    
        echo '</div>';  
    }
    

    现在在您需要显示按钮的 content.php 中添加 &lt;?php pietergoosen_social_share_buttons(); ?&gt;

    要在单击链接时打开弹出窗口,请在您的 enqueue_genericon_style() 函数中添加以下内容。

    wp_enqueue_script( 'pietergoosen-socialshare', get_template_directory_uri() . '/js/socialshare.popup.js', array( 'jquery' ), '' , true );
    

    如果您没有,现在在您的主题中创建一个 js 文件夹。在 js 文件夹中创建一个名为 socialshare.popup.js 的文件。现在在该文件中添加以下内容

    jQuery(document).ready(function($) {
    
        jQuery('.socialgenericons.service a').live('click', function(){
    
            newwindow=window.open($(this).attr('href'),'','height=450,width=700');
    
            if (window.focus) {newwindow.focus()}
    
            return false;
    
        });
    
    });
    

    这应该可以解决问题。您的按钮将如下所示。您只需要相应地设置泛型的样式

    编辑

    我使用自定义摘录,因此我将其更改为 the_excerpt() 以达到此答案的目的,否则您将收到致命错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-18
      • 2018-01-26
      • 2018-06-30
      相关资源
      最近更新 更多