2013-03-10 18 views
0

我很新的PHP。请原谅,如果这是一个愚蠢的问题。 我已通过php将社交分享图标添加到我的博客。在主索引页面中,我添加了facebook,twitter和google plus共享图标,并在单个帖子中添加了更多图标。是否可以从一个PHP文件中调用不同的代码段?

我如何实施?

我创建了两个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> 
+0

是什么阻止你从创建,要么出回声的功能? – Daedalus 2013-03-10 06:20:21

+0

@ Daedalus:正如我在我的文章中提到的,我对PHP很新。我通过查看主题设计代码了解了上述代码。实际上,我对php一无所知。我在尝试学习。 – Ian 2013-03-10 06:24:00

回答

2

这其实是一个很简单的事情,我建议你在manual时读了你有机会。只要创建一个函数,将返回的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(); 
?> 
+0

对我来说这仍然很复杂......但我一定会尝试。 – Ian 2013-03-10 06:58:59

+0

@Ian我有一个轻微的错字,现在修好了。另外,如果这解决了您的问题,请考虑接受此答案,方法是单击旁边的勾选标记。 – Daedalus 2013-03-10 07:05:07

+0

感谢Daedalus它为我工作... – Ian 2013-03-10 07:25:44

相关问题