List repeating share links

时间:2013-05-05 作者:Pieter Goosen

我想在我的帖子中添加社交共享按钮,但我不想在我的所有内容文件中添加那么多代码。我想创建一个函数,然后将函数名添加到我的模板文件中,如下所示:

function pietergoosen_sosiale_netwerk_deel_knoppies() {

<strong>Deel die pos met ander</strong><p </p>
        <a href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;amp;t=<?php the_title(); ?>" title="Share on Facebook.">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" /></a>
        <a href="http://twitter.com/home/?status=<?php the_title(); ?> : <?php the_permalink(); ?>" title="Tweet this!">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/twitter.png" alt="Tweet this!" /></a>
        <a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=<?php the_permalink();?>&amp;amp;t=<?php the_title(); ?>" title="Google+1.">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/google.png" alt="Google+1" id="Google+1" /></a>
        <a href="http://www.stumbleupon.com/submit?url=<?php the_permalink(); ?>&amp;amp;title=<?php the_title(); ?>" title="StumbleUpon.">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/stumbleupon.png" alt="StumbleUpon" /></a>
        <a href="http://digg.com/submit?phase=2&amp;amp;url=<?php the_permalink(); ?>&amp;amp;title=<?php the_title(); ?>" title="Digg this!">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/digg.png" alt="Digg This!" /></a>               
        <a href="http://del.icio.us/post?url=<?php the_permalink(); ?>&amp;amp;title=<?php the_title(); ?>" title="Bookmark on Delicious.">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/delicious.png" alt="Boekmerk op Delicious" /></a>
        <a href="https://mail.google.com/mail/?view=cm&fs=1&to&su=<?php the_permalink();?>&amp;amp;t=<?php the_title(); ?>" title="epos.">
        <img src="<?php bloginfo(\'stylesheet_directory\'); ?>/images/gmail.png" alt="epos" id="epos dit" /></a> }
这不管用。我应该如何正确地执行它?

2 个回复
最合适的回答,由SO网友:fuxia 整理而成

您的代码有几个问题:

如果要输出纯HTML,必须关闭PHP上下文:function foo(){ ?><strong><?php }<a href 不止一次是一个bugget_the_title() 在属性中。使用the_title_attribute( array ( \'echo\' => FALSE ) ) 相反,您也可以在HTML输出中获得意外的标记bloginfo(\'stylesheet_directory\'). 使用get_stylesheet_directory_uri() 在里面child themes onlyget_template_directory_uri() 在所有其他情况下&.https://delicious.com/post?. 多年来一直如此生成的代码可能如下所示:

function pietergoosen_sosiale_netwerk_deel_knoppies()
{
    $services = array (
        \'facebook\' => array (
            \'url\'  => \'http://www.facebook.com/sharer.php?u=%1$s&amp;t=%2$s\',
            \'text\' => \'Share on Facebook.\'
        ),
        \'twitter\' => array (
            \'url\'  => \'http://twitter.com/home/?status=%1$s%%20-%%20%2$s\',
            \'text\' => \'Tweet this!\'
        ),
        \'google\' => array (
            \'url\'  => \'http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=%2$s&amp;t=%2$s\',
            \'text\' => \'Google+1.\'
        ),
        \'stumbleupon\' => array (
            \'url\'  => \'http://www.stumbleupon.com/submit?url=%1$s&amp;title=%2$s\',
            \'text\' => \'StumbleUpon.\'
        ),
        \'digg\' => array (
            \'url\'  => \'http://digg.com/submit?phase=2&amp;url=%1$s&amp;title=%2$s\',
            \'text\' => \'Digg this!\'
        ),
        \'delicious\' => array (
            \'url\'  => \'https://delicious.com/post?url=%1$s&amp;title=%2$s\',
            \'text\' => \'Bookmark on Delicious.\'
        ),
        \'gmail\' => array (
            \'url\'  => \'https://mail.google.com/mail/?view=cm&amp;fs=1&amp;to&amp;su=%1$s&amp;t=%2$s\',
            \'text\' => \'Share per Gmail.\'
        )
    );
    $img_base = get_template_directory_uri() . \'/images/%s.png\';
    $title    = the_title_attribute( array ( \'echo\' => FALSE ) );
    $url      = urlencode( get_permalink() );

    print \'<h4>Deel die pos met ander</h4>\';

    foreach ( $services as $name  => $service )
    {
        $href = sprintf( $service[\'url\'], $url, urlencode( $title ) );
        $src  = sprintf( $img_base, $name );

        printf(
            \'<a href="%1$s" title="%2$s"><img src="%3$s" alt="%2$s" /></a>\',
            $href,
            esc_attr( $service[\'text\'] ),
            $src
        );
    }
}
现在可以在模板中调用此函数:

pietergoosen_sosiale_netwerk_deel_knoppies();

SO网友:OriginalEXE

首先,您从未关闭过php标记。

其次,不要每次都为每个社交图标调用函数。而是将值保存在变量中,以加快过程。

应该是这样的:

<?php function my_social_buttons() {

    $title = esc_attr( get_the_title() );
    $permalink = esc_attr( get_the_permalink() );
    $directory = get_template_directory_uri();

    ?>
    <strong>Deel die pos met ander</strong>
    <a href="http://www.facebook.com/sharer.php?u=<?php echo $permalink;?>&amp;amp;t=<?php echo $title; ?>" title="Share on Facebook.">
    <img src="<?php echo $directory; ?>/images/facebook.png" alt="Share on Facebook" id="sharethis-last" /></a>
    <a href="http://twitter.com/home/?status=<?php echo $title; ?> : <?php echo $permalink; ?>" title="Tweet this!">
    <img src="<?php echo $directory; ?>/images/twitter.png" alt="Tweet this!" /></a>
    <a href="http://www.google.com/bookmarks/mark?op=edit&bkmk=<?php echo $permalink;?>&amp;amp;t=<?php echo $title; ?>" title="Google+1.">
    <img src="<?php echo $directory; ?>/images/google.png" alt="Google+1" id="Google+1" /></a>
    <a href="http://www.stumbleupon.com/submit?url=<?php echo $permalink; ?>&amp;amp;title=<?php echo $title; ?>" title="StumbleUpon.">
    <img src="<?php echo $directory; ?>/images/stumbleupon.png" alt="StumbleUpon" /></a>
    <a href="http://digg.com/submit?phase=2&amp;amp;url=<?php echo $permalink; ?>&amp;amp;title=<?php echo $title; ?>" title="Digg this!">
    <img src="<?php echo $directory; ?>/images/digg.png" alt="Digg This!" /></a>               
    <a href="http://del.icio.us/post?url=<?php echo $permalink; ?>&amp;amp;title=<?php echo $title; ?>" title="Bookmark on Delicious.">
    <img src="<?php echo $directory; ?>/images/delicious.png" alt="Boekmerk op Delicious" /></a>
    <a href="https://mail.google.com/mail/?view=cm&fs=1&to&su=<?php echo $permalink;?>&amp;amp;t=<?php echo $title; ?>" title="epos.">
    <img src="<?php echo $directory; ?>/images/gmail.png" alt="epos" id="epos dit" /></a> 

    <?php

}

结束

相关推荐

如何在Functions.php中使用PHP手动修复WordPress库代码?

Wordpress为内置的gallery功能输出了一些非常糟糕的代码,这已经被谈论了很多次了。这是负责库输出的核心代码(在/wp-includes/media.php中):function gallery_shortcode($attr) { global $post; static $instance = 0; $instance++; // Allow plugins/themes to override the de