Skip to content

Instantly share code, notes, and snippets.

@snoise
Last active January 4, 2016 00:29
Show Gist options
  • Save snoise/8541813 to your computer and use it in GitHub Desktop.
Save snoise/8541813 to your computer and use it in GitHub Desktop.
「特定のカテゴリ」の記事の末尾に文字列・画像を追加wordpressプラグイン
<?php
/*
Plugin Name: paka3 特定カテゴリ記事に画像・文字列を追加
Plugin URI: http://www.paka3.com/wpplugin
Description: すべての記事に「最新の記事」のリンクを3件入れる
Author: Shoji ENDO
Version: 0.1
Author URI:http://www.paka3.com/
*/
function paka3_CatAddContents($contentData) {
if(is_single()){
//特定のカテゴリID
$catIDs = array(3,12); //例cat_ID=3と12
//現在のカテゴリIDを取得
$nowCatID =array();
foreach(get_the_category() as $cat){
array_push($nowCatID,$cat->cat_ID);
//echo $cat->cat_ID."*";
}
//検索処理
foreach($catIDs as $catID){
if(array_search($catID,$nowCatID)!==false){
//カテゴリIDが一つでもあった場合の処理
$str = "例cat_IDが3と12カテゴリでしか表示されないよ<br />";
$str .= "<img src='http://www.paka3.net/wp-content/uploads/2014/01/wpicon1.png' alt='1日1プラグイン'>";
return $contentData.$str; //離脱
}
}
}
//条件にあわない場合はそのまま返す
return $contentData;
}
add_filter('the_content','paka3_CatAddContents');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment