blog.knym.net | I'm searching something to change my life.
WordPressで各カテゴリの最新の記事をとって、それを最新順に並べ替えて表示
もっと賢い方法がある気がするけど、とりあえず以下の感じで対応。
<?php
$count = 0;
function getCategoryLatestPosts($ids){
//$bloggerCatIDs = '46,50,52,54,13,45,14,11,12';
$idsArr = split(",",$ids);
$posts = array();
//カテゴリ別に最新のpostを取得
foreach($idsArr as $catId){
$p =query_posts("showposts=1&cat={$catId}");
array_push($posts,$p[0]);
}
//配列の空要素を削除
function delNull($var){
if ($var == '') return FALSE;
return TRUE;
}
$posts…
WP plugin Calendarの日本語入力でラー
WordPress › Calendar « WordPress Plugins
をつかったとき、Event Titleで日本語入力すると下記のエラーが出る。
The event title must be between 1 and 30 characters in length and contain no punctuation. Spaces are allowed but the title must not start
…
WordPressでget_post_ancestors()を使った親IDの取得
表示されているページの、一番親となるページのIDを取得する関数を作った。
/wp-content/themes/{TEMPLATE_NAME}/functions.php
に下記のコードを追加
function get_root_post_id($id){
$arr = get_post_ancestors($id);
return array_pop($arr);
}
んで、
/wp-content/themes/{TEMPLATE_NAME}/page.php
なんかで、下記のような感じでその関数を呼び出す。
$root_ID= get_root_post_id($current_page_ID);
カテゴリページのカテゴリIDをcookieに保存
こんなことする人めったにいないと思うけど、
ま、WordPressでcookieを使ってごにょごにょしたいときにでも、参考にしてみてください。
get_headerにadd_action()するようにしないで、
たとえばwp_headにadd_action()するとsetcookie()の行がエラーで怒られる。
かといってinitにadd_action()するとis_category()の値が取れないという…
hookするタイミングの順番は
Plugin API/Action Reference « WordPress Codex
にある。
add_action('get_header', setCatCookie);
function setCatCookie(){
//60分間カテゴリーページのIDを保持
if(is_category()){
$value = get_query_var('cat');
$timeout = time() +60*60;#現在の時刻 + 60分
setcookie("cat",$value,$timeout,'/');
}
global $cat_ID;
$cat_ID = $_COOKIE['cat'];
}</p>
<p>
get_query_var('cat')はarchiveページのカテゴリIDをとれるから便利。
WordPressのGEOコード系plugin
WordPressでカテゴリIDの取得
sidebar.php内でややこしいメニューの階層処理をしてる中で使った。
if(is_category()){ $cat_ID=get_query_var('cat'); $cat=get_category($cat_ID); if($cat->parent) $cat_IDs[] = $cat->parent; $cat_IDs[]=$cat_ID; }else{ foreach(get_the_category() as $cat) { $cat_IDs[]=$cat->cat_ID; } }
WordPressの投稿記事で使われているサムネイル画像などを取得する関数 the_image
アーカイブページなどのループ内でサムネイル画像を表示したいときなどに重宝する関数the_image()を作った。
/wp-content/themes/{YOUR THEME}/functions.phpに下記のthe_image関数を追加しておく。
$typeでサムネイル画像や他の画像サイズを指定。
$numでアップロードした画像のどれを表示するか指定。
/* * $type='thumbnail'|'medium'|'large'|'full'; * $num is the number of image order. */ function the_image($type='thumbnail',$num=0){ global $post; $post_ID = $post->ID; $files = get_children("post_parent={$post_ID}&post_type=attachment&post_mime_type=image"); if…
WordPressでカテゴリのリンクから/category/を取る
http://blog.url.com/----escape_autolink_uri:213753098a21d0a5f8fed1ed79ca5673----/hogehoge/から、
/category/をとって、http://blog.url.com/hogehoge/としたいときの方法。
wp_list_pages()の代わり
$posts = get_posts("numberposts=5&offset=1&category={$cat_id}");
foreach($posts as $post) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endforeach;
?>
wp_list_categoriesの代わり
たとえば、wp_list_categories('title_li=&child_of=3&depth=1')の代わりとなる処理は
$categories = get_categories("child_of=3");
foreach($categories as $cat){
$cat_link = get_category_link($cat->cat_ID);
echo "<li><a href='{$cat_link}'>{$cat->cat_name}</a></li>";
}