blog.knym.net | I'm searching something to change my life.

wordpressのtheme内で、トップページかどうかの判別

下記でややこしいことしてるんだけど、、、
is_front_page()でトップページかチェックできる。


$wp_queryを呼び出すタイミングが遅いとエントリの一覧表示後などには、
表示されているページと違う内容が代入されているので、
wp_headを呼ぶタイミングで表示されているページのIDを取っておくようにした。


んー、説明がややこしいけど、要は、
以下のプログラムをtheme内のfunctions.phpに仕込んでおくと
トップページにだけ表示するコンテンツ内容が操作できて便利ですよという話。


/wp-content/themes/{THEME_NAME}/functions.php

php; first-line: 0;">
$topId = 5;//トップページとして指定したページID
$currentPostId;//表示されるページID
//wp_headが呼ばれるタイミングで、現在表示されているページを判別
add_action('wp_head', setCurrentPostId);
function setCurrentPostId(){
    global $wp_query;
    global $currentPostId;
    $currentPostId =  getParentPageId($wp_query->post);
}
//if(isTop()){ ... というふうにthemeファイル内で使える。
function isTop(){
    global $topId;
    global $currentPostId;
    return ($currentPostId==$topId)? 1:0;
}


Reference


Function Reference/add action « WordPress Codex

Related Posts

Write a Comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Essentials

Meta

Pages

Categories