0

私はphpにかなり慣れていません。これがばかげた質問である場合は、ご容赦ください。PHP 経由でブログにソーシャル共有アイコンを追加しました。メインのインデックス ページに facebook、twitter、google に加えて共有アイコンを追加し、単一の投稿にいくつかのアイコンを追加しました。

どのように実装しましたか?

social_main_index.php と social_single_post.php の 2 つの 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 に答える