0

我对php很陌生。如果这是一个愚蠢的问题,请原谅。我已经通过 php 在我的博客中添加了社交分享图标。在主索引页面中,我添加了 facebook、twitter 和 google 以及共享图标,在单个帖子中,我添加了更多图标。

我是如何实施的?

我创建了两个 php 文件 social_main_index.php 和 social_single_post.php 然后在主索引模板中添加了代码<? php include("social_main_index.php") ?>

在单个帖子模板中,我添加了代码<? php include("social_single_post.php") ?>

我的问题

我可以创建一个包含这两个代码的单个 php 文件并专门调用它们中的任何一个吗?

我在 social_main_index.php 中的代码

<?php include_once("custom_css.php") ?>
<div class="social-single">
<!--Facebook Share-->
<div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(get_permalink()); ?>&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
<!--Google Plus Share-->
<div id="plusonebutton"><g:plusone href="<?php echo rawurlencode(get_permalink()); ?> "size="medium"></g:plusone></div>
<!--Twitter Share-->
<div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink() ?>" data-counturl="<?php the_permalink() ?>" data-text="<?php the_title(); ?>" data-via="nucleationin" data-related="nucleationin"></a></div>
</div>
</div>

我在 social_single_post.php 中的代码

<?php include_once("custom_css.php") ?>
<div class="social-single">
<!--Facebook Share-->
<div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href=<?php echo rawurlencode(get_permalink()); ?>&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
<!--Google Plus Share-->
<div id="plusonebutton"><g:plusone href="<?php echo rawurlencode(get_permalink()); ?> "size="medium"></g:plusone></div>
<!--Twitter Share-->
<div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="<?php the_permalink() ?>" data-counturl="<?php the_permalink() ?>" data-text="<?php the_title(); ?>" data-via="nucleationin" data-related="nucleationin"></a></div></div>
<!--Linkedin Share-->
<div id="linkedinshare"><script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="in/share" data-url="<?php the_permalink() ?>" data-counter="right"></script></div>
<!--Stumble Share-->
<div id="stumblebutton"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1"></script></div>
</div>
4

1 回答 1

2

这实际上是一件非常简单的事情,我建议您在有机会时阅读手册。只需创建一个返回任一 html 的函数:

function print_buttons($button_set) {
    if ($button_set == "one") { //if the local variable for the function is the string "one", then paste the data below where the function is called
        return '<div class="social-single">
            <!--Facebook Share-->
            <div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href='.rawurlencode(get_permalink()).'&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
            <!--Google Plus Share-->
            <div id="plusonebutton"><g:plusone href="'.rawurlencode(get_permalink()).'"size="medium"></g:plusone></div>
            <!--Twitter Share-->
            <div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="'.the_permalink().'" data-counturl="'.the_permalink().'" data-text="'.the_title().'" data-via="nucleationin" data-related="nucleationin"></a></div>
            </div>
            </div>';
    }
    else { //if the local variable is anything else, do the same thing, but with the below data instead.
        return '<?php include_once("custom_css.php") ?>
            <div class="social-single">
            <!--Facebook Share-->
            <div id="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href='.rawurlencode(get_permalink()).'&layout=button_count&show_faces=false&width=100&action=like&font=verdana&colorscheme=light&height=21" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:21px;" allowTransparency="true"></iframe></div>
            <!--Google Plus Share-->
            <div id="plusonebutton"><g:plusone href="'.rawurlencode(get_permalink()).'"size="medium"></g:plusone></div>
            <!--Twitter Share-->
            <div id="twitterbutton"><script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script><div> <a href="http://twitter.com/share" class="twitter-share-button" data-url="'.the_permalink().'" data-counturl="'.the_permalink().'" data-text="'.the_title().'" data-via="nucleationin" data-related="nucleationin"></a></div></div>
            <!--Linkedin Share-->
            <div id="linkedinshare"><script type="text/javascript" src="http://platform.linkedin.com/in.js"></script><script type="in/share" data-url="'.the_permalink().'" data-counter="right"></script></div>
            <!--Stumble Share-->
            <div id="stumblebutton"><script src="http://www.stumbleupon.com/hostedbadge.php?s=1"></script></div>
            </div>';
    }
}

然后使用它...

echo print_buttons("one"); //this will print the first group
echo print_buttons("anything else"); //this will print the second

请注意,这是在黑暗中拍摄的,因为我不知道您回显的功能是什么,否则返回/做了什么。如果我这样做了,我可以肯定地说这对你有用。然而,我没有,因此以上。

要使用此功能,请确保它包含在正在使用它的页面中,就像上面的 css 通过include(). 例子:

some_functions.php:

<?php
function the_function_above() { }
?>

进而...

The_page_above.php:

<?php
include("some_functions.php");

echo the_function_above();
?>
于 2013-03-10T06:40:00.750 回答