WordPress 条件タグ【Conditional Tags】解説

Sponsored Link

QuickTag.

添付ファイル

is_attachment()

投稿かページに添付ファイルのドキュメントが表示されている場合です。添付ファイルは、画像や投稿エディターのアップロード機能を介してアップロードされた他のファイルです。添付ファイルは、独自の’page’かテンプレートで表示する事ができます。詳細については、画像と添付ファイルを使用するを参照してください。

原文(翻訳元)When an attachment document to a post or Page is being displayed. An attachment is an image or other file uploaded through the post editor’s upload utility. Attachments can be displayed on their own ‘page’ or template. For more information, see Using Image and File Attachments.

シングルページ、シングル投稿あるいは、添付ファイル

is_singular()

次のいずれかをtrueで返す時です:is_single(), is_page(), is_attachment()

原文(翻訳元)When any of the following return true: is_single(), is_page() or is_attachment().

is_singular(‘book’)

投稿タイプのbookを投稿で表示する時にtrueです。バージョン 3.0で導入されました。

原文(翻訳元)True when viewing a post of the post type book. Introduced with Version 3.0.

is_singular(array( ‘newspaper’, ‘book’ ))

投稿タイプnewspaperかbookの投稿を表示する時trueです。バージョン 3.0で導入されました。

原文(翻訳元)True when viewing a post of the post type newspaper or book. Introduced with Version 3.0.

シンジケーション

is_feed()

サイトがシンジケーションを要求した場合です。このタグは、通常ユーザーには使用されません;それは内部時にWordPressによって使用され、プラグイン開発者に有効です。

原文(翻訳元)When the site requested is a Syndication. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.

トラックバック

is_trackback()

サイトが要求したそのトラックバックエンジンにWordPressのフックの場合です。このタグは、通常ユーザーには使用されません;それは内部時にWordPressによって使用され、プラグイン開発者に有効です。

原文(翻訳元)When the site requested is WordPress’ hook into its Trackback engine. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers.

プレビュー

is_preview()

シングル投稿が表示され、ドラフトモードで表示された場合です。

原文(翻訳元)When a single post being displayed is viewed in Draft mode.

抜粋あり

has_excerpt()

現在の投稿に抜粋がある場合です。

原文(翻訳元)When the current post has an excerpt

has_excerpt(’42’)

投稿42(ID)に抜粋がある場合です。

原文(翻訳元)When the post 42 (ID) has an excerpt.

<?php
// Get $post if you're inside a function
global $post;

if ( empty($post->post_excerpt) ) {
    // This post has no excerpt
} else {
    // This post has excerpt
}
?>

他の使い方

自動表示の抜粋を隠す必要があり、投稿の抜粋が表示のみの場合です。

原文(翻訳元)when you need hide the excerpt auto displayed and only display yours posts excerpts.

<?php if ( !has_excerpt()) {
      echo '';
} else { 
      the_excerpt();
}

テキストかコードに自動抜粋に置き換えます。

原文(翻訳元)Replace auto excerpt for a text or code.

<?php if ( !has_excerpt() ) {?>
    <!-- you text or code -->
<?php } ?>

ループ内

in_the_loop()

“ループ内”にあるかどうかを確認します。プラグイン作者の役立ち、この条件はループ内にある時にtrueとして返します。

原文(翻訳元)Check to see if you are “inside the loop”. Useful for plugin authors, this conditional returns as true when you are inside the loop.

サイドバーアクティブ

is_active_sidebar()

指定されたサイドバーがアクティブ(使用中)であるかどうかを確認します。サイドバー(名前、id、ナンバーによて識別された)が使用された場合trueを返し、それ以外の関数はfalseを返します。この条件機能は、バージョン2.8から利用可能になりました。

原文(翻訳元)Check to see if a given sidebar is active (in use). Returns true if the sidebar (identified by name, id, or number) is in use, otherwise the function returns false. This conditional function became available with Version 2.8.

作業例

ここでは、これらの条件タグを使用するための方法を示すサンプルを勧めています。

原文(翻訳元)
Here are working samples to demonstrate how to use these conditional tags.

シングル投稿

この例では、いくつかの特定シングル投稿ページを表示している時のみ表示するためにis_single()を使用するための方法を表示しています。

原文(翻訳元)This example shows how to use is_single() to display something specific only when viewing a single post page:

if (is_single()) {
   echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!';
}

その他の例では、ループに条件タグを使用する方法です。これがシングル投稿、カテゴリー、ホームとページで表示する固定のアーカイブの時、index.php内で、コンテンツあるいは抜粋を表示する選択をします。

原文(翻訳元)Other example how to use Conditional Tags into loop. choose display content or excerpt in the index.php, when this is unique archive for display single post, categories, home and page.

if (is_home() || is_single()) {
   the_content();
}
else {
   the_excerpt();
}

ホームページではない場所に、コードまたは要素を表示する必要がある場合です。

原文(翻訳元)When you need display a code or element, in a place that is not the home page.

<?php if (!is_home()) {?>

 Insert your code...

<?php }?>

日付ベースの違い

誰かが日付順のサイトを参照するならば、異なる色を使用して別の年に投稿を区別してみましょう:

原文(翻訳元)If someone browses our site by date, let’s distinguish posts in different years by using different colors:

<?php
// this starts The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>

<?php
// are we showing a date-based archive?
if (is_date())
{
     if (date('Y') != get_the_date('Y'))
     {
          // this post was written in a previous year
          // so let's style the content using the "oldentry" class
          echo '<div class="oldentry">';
     } else {
          echo '<div class="entry">';
     }
} else {
     echo '<div class="entry">';
}
the_content('Read the rest of this entry »'); 
?>
</div>

サイドバーのコンテンツの変数

この例では、現在表示している読者のページに基づくサイドバーにさまざまなコンテンツを表示する事ができます。

原文(翻訳元)This example will display different content in your sidebar based on what page the reader is currently viewing.

<!-- begin sidebar -->
<div id="sidebar">
<?php
// let's generate info appropriate to the page being displayed
if (is_home()) {
        // we're on the home page, so let's show a list of all top-level categories
        echo "<ul>";
        wp_list_cats('optionall=0&sort_column=name&list=1&children=0');
        echo "</ul>";
} elseif (is_category()) {
        // we're looking at a single category view, so let's show _all_ the categories
         echo "<ul>";
        wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1');
        echo "</ul>";
} elseif (is_single()) {
        // we're looking at a single page, so let's not show anything in the sidebar
} elseif (is_page()) {
        // we're looking at a static page.  Which one?
        if (is_page('About')) {
             // our about page.
             echo "<p>This is my about page!</p>";
        } elseif (is_page('Colophon')) {
             echo "<p>This is my colophon page, running on WordPress " . bloginfo('version') . "</p>";
        } else {
              // catch-all for other pages
              echo "<p>Vote for Pedro!</p>";
        }
} else {
        // catch-all for everything else (archives, searches, 404s, etc)
        echo "<p>Pedro offers you his protection.</p>";
} // That's all, folks!
?>
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<input type="text" name="s" id="s" size="15" />
<input type="submit" value="<?php _e('Search'); ?>" />
</div>
</form>

</div>
<!-- end sidebar -->

404ページの参考

フレンドリーメッセージを書くセクション内で、PHP条件関数のsset()を使用する例を持つ、エラー404ページの記事を作成します。

原文(翻訳元)The Creating an Error 404 Page article has an example of using the PHP conditional function isset() in the Writing Friendly Messages section.

動的メニューの強調表示

動的メニューのハイライト資料では、メニュー内の現在のページでハイライトを有効にする条件タグを使用する方法を説明しています。

原文(翻訳元)The Dynamic Menu Highlighting article discusses how to use the conditional tags to enable highlighting of the current page in a menu.

テーマのfooter.phpファイル内で

時には、クエリーは、特定の条件タグを破損させる可能性がある、例えばsidebar.phpなど他のテンプレートタグに実行されます。例えば、header.php内の条件タグは正常に動作しますが、テーマのfooter.php内では動作しません。トリックは、フッター内の条件テストの前にwp_reset_queryを追加します。

原文(翻訳元)At times queries performed in other templates such as sidebar.php may corrupt certain conditional tags. For instance, in header.php a conditional tag works properly but doesn’t work in a theme’s footer.php. The trick is to put wp_reset_query before the conditional test in the footer. For example:

<?php
wp_reset_query();
if (is_page('2') ) {
echo 'this is page 2!';
} 
?>

条件タグINDEX

アルファベット順のリスト

ファンクションリファレンス

Conditional Tags: comments_open(), is_local_attachment(), is plugin active(), is_404(), is_admin(), is_archive(), is_attachment(), is_author(), is_category(), is_comments_popup(), is_date(), is_day(), is_feed(), is_front_page(), is_home(), is_month(), is_new_day(), is_page(), is_page_template(), is_paged(), is_plugin_page(), is_preview(), is_search(), is_single(), is_singular(), is_sticky(), is_tag(), is_tax(), is_taxonomy_hierarchical(), is_time(), is_trackback(), is_year(), pings_open(), has_excerpt(), has_post_image(), has_tag(), in_category(), in_the_loop(), is_active_sidebar(), is_active_widget(), is_blog_installed(), is_dynamic_sidebar(), is_user_logged_in(), post_type_exists(), taxonomy_exists(), term_exists()

Sponsored Link