【问题标题】:WordPress excerpt adding social share button textWordPress 摘录添加社交分享按钮文本
【发布时间】:2017-02-13 00:58:47
【问题描述】:

我在我的 WordPress 主题上添加了一个分享按钮,然后用the_content 将它附加到页面顶部,它运行良好,只是当我检查我的主页时,我看到社交按钮上的文本,例如Facebook、Google Plus、Twitter 和其他的都出现了(虽然是纯文本)。

但是,我制作了另一段代码,将其附加在完美运行的内容之后。我的意思是摘录没有添加社交图标文本,毕竟限制是 30 个字字符。

当摘录被附加到顶部时,我想删除这些按钮。我的意思是在内容之前。

这是我的意思的截图:

这是我的代码,如果需要修改。

/* social share icons */

function crunchify_social_sharing_buttons($content) {
global $post;
if(is_singular() || is_home()){

    // Get current page URL 
    $crunchifyURL = urlencode(get_permalink());

    // Get current page title
    $crunchifyTitle = str_replace( ' ', '%20', get_the_title());

    // Get Post Thumbnail for pinterest
    $crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

    // Construct sharing URL without using any script
    $twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&url='.$crunchifyURL.'&via=Crunchify';
    $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
    $googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
    $bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&text='.$crunchifyTitle;
    $linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$crunchifyURL.'&title='.$crunchifyTitle;
    $whatsappURL = 'whatsapp://send?text='.$crunchifyTitle . ' ' . $crunchifyURL;

    // Based on popular demand added Pinterest too
    $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&media='.$crunchifyThumbnail[0].'&description='.$crunchifyTitle;

    // Add sharing button at the end of page/page content
    $variable .= '<!-- Crunchify.com social sharing. Get your copy here: http://crunchify.me/1VIxAsz -->';
    $variable .= '<div class="crunchify-social">';
    $variable .= '<a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank"><i class="fa fa-twitter"></i> Twitter</a>';
    $variable .= '<a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank"><i class="fa fa-facebook"></i>Facebook</a>';
    $variable .= '<a class="crunchify-link crunchify-whatsapp" href="'.$whatsappURL.'" target="_blank"><i class="fa fa-whatsapp"></i>WhatsApp</a>';
    $variable .= '<a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank"><i class="fa fa-google-plus"></i>Google+</a>';
    $variable .= '<a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank"><i class="fa fa-spinner"></i>Buffer</a>';
    $variable .= '<a class="crunchify-link crunchify-linkedin" href="'.$linkedInURL.'" target="_blank"><i class="fa fa-linkedin" aria-hidden="true"></i>LinkedIn</a>';
    $variable .= '<a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" data-pin-custom="true" target="_blank"><i class="fa fa-pinterest-p"></i>Pin It</a>';
    $variable .= '</div>';

    return $variable.$content;
}else{
    // if not a post/page then don't include sharing button
    return $variable.$content;
}
};
add_filter( 'the_content', 'crunchify_social_sharing_buttons');

【问题讨论】:

    标签: wordpress wordpress-theming


    【解决方案1】:

    您是否在主题中使用插件或函数来创建摘录?使用 WP Core 函数自动创建的摘录取自 the_content,去除所有 HTML 标签和短代码,默认长度为 55 个字。

    如果你想明确排除这个块,你可以在你的主题的functions.php中添加一个小函数或一个特定于站点的插件,它(只要它不与其他函数命名相同)可以与其他the_excerpt 的过滤器:

    function excerpt_strip_content_share_buttons( $excerpt ) {
        $regex = '(<div class="crunchify-social">)((\s)*(<a\s.*a>))+(\s*<\/div>){1}'; \\ specifically for that block of your 'a' tags, does not affect other divs
        $excerpt = preg_replace($regex,'', $excerpt);
        return $excerpt;
    }
    add_filter( 'the_excerpt', 'excerpt_strip_content_share_buttons', 0);
    

    【讨论】:

    • 你好,谢谢你的回复我真的很感激,我放置了你提到的上面的代码,它确实删除了
      但它也产生了一个错误@987654324 @
    【解决方案2】:

    我想出了一个不同的方式你,希望现在有一个标记为已回答按钮我会点击它,我发现为什么它的显示是因为它在the-content 并且摘录试图剥离图像、div 类、简码、html 标签等等,只留下纯文本。

    所以我做的是

    1. 我创建了一个新代码,添加了一个短代码并用acardio-social 调用它,然后在标题之后调用它,这次不在&lt;?php the-content(); ?&gt; 内,为了使它更好,它只是一个短代码甚至不是一个 div 类,因此只要它带有一个函数,它就会完全从 &lt;?php the-excerpt()?&gt; 中排除,我认为这种方法是我能做的最好的。

    2. 我添加了一个函数来获取标题,我也尝试了这样使它能够在while ('have_posts()') 条件下运行,因此它会在 5 个帖子的列表中生成不同的共享 URL。

    这里是代码,如果它可以帮助任何人。

    /* social share icons */
    
    /**
    * Shortcode function ish
    */
    add_shortcode( 'acardio-social', 'nerd_social_share_plugin_shortcode' );
    function nerd_social_share_plugin_shortcode( $attr ) {
    global $plugin_code;
       // Get current page URL 
    $crunchifyURL = urlencode(get_permalink());
    
    // Get current page title
    $crunchifyTitle = str_replace( ' ', '%20', get_the_title());
    
    // Get Post Thumbnail for pinterest
    $crunchifyThumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
    
    // Construct sharing URL without using any script
    $twitterURL = 'https://twitter.com/intent/tweet?text='.$crunchifyTitle.'&amp;url='.$crunchifyURL.'&amp;via=Crunchify';
    $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u='.$crunchifyURL;
    $googleURL = 'https://plus.google.com/share?url='.$crunchifyURL;
    $bufferURL = 'https://bufferapp.com/add?url='.$crunchifyURL.'&amp;text='.$crunchifyTitle;
    $linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url='.$crunchifyURL.'&amp;title='.$crunchifyTitle;
    $whatsappURL = 'whatsapp://send?text='.$crunchifyTitle . ' ' . $crunchifyURL;
    
    // Based on popular demand added Pinterest too
    $pinterestURL = 'https://pinterest.com/pin/create/button/?url='.$crunchifyURL.'&amp;media='.$crunchifyThumbnail[0].'&amp;description='.$crunchifyTitle;
    
    echo '<div class="crunchify-social">';
    
    echo '<div class="acardio-post-social-icons">';
    
    echo '
        <!--Facebook-->
        <a class="crunchify-link crunchify-facebook" href="'.$facebookURL.'" target="_blank"><i class="fa fa-facebook"></i>Facebook</a>
    
        <!-- twiiter -->
    
        <a class="crunchify-link crunchify-twitter" href="'. $twitterURL .'" target="_blank"><i class="fa fa-twitter"></i> Twitter</a>
    
    
    
        <!--Google Plus-->
       <a class="crunchify-link crunchify-googleplus" href="'.$googleURL.'" target="_blank"><i class="fa fa-google-plus"></i>Google+</a>
    
        <!-- whatsapp -->
        <a class="crunchify-link crunchify-whatsapp" href="'.$whatsappURL.'" target="_blank"><i class="fa fa-whatsapp"></i>WhatsApp</a>
    
    
        <!--Reddit-->
        <a  class="crunchify-link crunchify-googleplus" target="_blank" href="http://reddit.com/submit?url='. get_permalink() .'&amp;title='. the_title('', '', FALSE) .'" rel="nofollow"><i class="fa fa-reddit"></i>Reddit</a>
    
        <!--buffer-->
        <a class="crunchify-link crunchify-buffer" href="'.$bufferURL.'" target="_blank"><i class="fa fa-spinner"></i>Buffer</a>
    
        <!--LinkedIn-->
        <a class="crunchify-link crunchify-linkedin" href="'.$linkedInURL.'" target="_blank"><i class="fa fa-linkedin" aria-hidden="true"></i>LinkedIn</a>
    
        <!--Pinterst-->
        <a class="crunchify-link crunchify-pinterest" href="'.$pinterestURL.'" data-pin-custom="true" target="_blank"><i class="fa fa-pinterest-p"></i>Pin It</a>
    
        ' .
        "
    </div>
    </div>";
    }
    

    请注意,它可以修改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 2014-02-26
      • 2012-01-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多