ちょっとしたデータ整理用のエントリを作っておきたいことがあります。たとえば書籍の一覧をつくっておきたいのですが、これは通常のエントリとは別のものなので、トップページへはエントリとして表示したくありません。そんなとき、WordPressではカテゴリやタグを指定できますから、これをうまくつかうことによりコントロールすることができます。
本ブログでは、Twitterの記録もエントリとして残しているのですが、これもトップページへ表示したくないエントリです。また、広告エントリである書籍エントリのうち一部のものは純粋に書籍一覧しかないので、表示したくありません。
ということで、次のようなコードをindex.phpのテンプレートに記載しています。広告書籍エントリについては、カテゴリをBookとして、タイトルと投稿タグに同一文字列を指定することで判別できるようにしています。
<?php //--- custom
$cat=get_the_category(); if (strcmp($cat[0]->cat_name, 'Twitter')==0) continue;
$title = get_the_title();
$tags = get_the_tags();
if (count($cat)==1 && strcmp($cat[0]->cat_name, 'Book')==0) {
$count = 0; $tagName='';
if ($tags) {
foreach($tags as $tag) {
$count++;
if (1 == $count) {
//echo $tag->name;
$tagName = $tag->name;
}
}
}
if (strcmp($title, $tagName)==0 && $count==1) continue;
//echo "<div>" . $title . ", " . $tags . ", " . count($tags) . ", " . $tagName . "</div>";
}
?>
$cat=get_the_category(); if (strcmp($cat[0]->cat_name, 'Twitter')==0) continue;
$title = get_the_title();
$tags = get_the_tags();
if (count($cat)==1 && strcmp($cat[0]->cat_name, 'Book')==0) {
$count = 0; $tagName='';
if ($tags) {
foreach($tags as $tag) {
$count++;
if (1 == $count) {
//echo $tag->name;
$tagName = $tag->name;
}
}
}
if (strcmp($title, $tagName)==0 && $count==1) continue;
//echo "<div>" . $title . ", " . $tags . ", " . count($tags) . ", " . $tagName . "</div>";
}
?>
ここまで作ってみて気がついたのですが、広告書籍エントリは実は固定ページとして実装した方が良かったのかもしれません。
関連書籍: