Jeffsebring (talk | contribs) m (→Has A Nav Menu Assigned) |
m (→is_tax) |
||
(102 intermediate revisions by 41 users not shown) | |||
Line 2: | Line 2: | ||
{{en|Conditional Tags}} |
{{en|Conditional Tags}} |
||
{{fr|Marqueurs_conditionnels}} |
{{fr|Marqueurs_conditionnels}} |
||
+ | {{it|Tag condizionali}} |
||
− | {{ja|Conditional Tags}} |
||
+ | {{ja|Conditional_Tags}} |
||
{{tr|Kosul Etiketleri}} |
{{tr|Kosul Etiketleri}} |
||
{{pt-br|Tags condicionais}} |
{{pt-br|Tags condicionais}} |
||
Line 13: | Line 14: | ||
Note the close relation these tags have to WordPress [[Template Hierarchy]]. |
Note the close relation these tags have to WordPress [[Template Hierarchy]]. |
||
− | '''Warning: You can only use conditional query tags after the '' |
+ | '''Warning: You can only use conditional query tags after the ''posts_selection'' [[Plugin API/Action Reference#Actions Run During a Typical Request|action hook]] in WordPress (the ''wp'' action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.''' |
+ | |||
+ | '''However: if you have a reference to the <tt>query</tt> object (for example, from within the <tt>parse_query</tt> or <tt>pre_get_posts</tt> hooks), you can use the WP_Query conditional methods (eg: <code>$query->is_search()</code>)''' |
||
== The Conditions For ... == |
== The Conditions For ... == |
||
Line 21: | Line 24: | ||
=== The Main Page === |
=== The Main Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_home|is_home()]]</tt> : When the main blog page is being displayed. This is the page which shows the time based blog content of your site, so if you've set a static Page for the Front Page (see below), then this will only be true on the Page which you set as the "Posts page" in [[Administration_Panels|Administration]] > [[Administration_Panels#Reading|Settings]] > [[Settings_Reading_SubPanel|Reading]]. |
=== The Front Page === |
=== The Front Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_front_page|is_front_page()]]</tt> : When the front of the site is displayed, whether it is posts or a [[Pages|Page]]. Returns true when the main blog page is being displayed and the '[[Administration_Panels#Reading|Settings]] > [[Settings_Reading_SubPanel|Reading]] ->Front page displays' is set to "Your latest posts", '''or''' when '[[Administration_Panels#Reading|Settings]] > [[Settings_Reading_SubPanel|Reading]] ->Front page displays' is set to "A static page" and the "Front Page" value is the current [[Pages|Page]] being displayed. |
+ | |||
+ | === The Blog Page === |
||
+ | |||
+ | ; <tt>[[Function_Reference/is_front_page|is_front_page()]]</tt> and <tt>[[Function_Reference/is_home|is_home()]]</tt> : |
||
+ | There is no conditional tag for the blog page. You have to use both <tt>is_home()</tt> and <tt>is_front_page()</tt> to detect this page, but those functions can be misused. In fact, a user can define a static page for the homepage, and another page to display the blog. This one will return <tt>true</tt> with <tt>is_home()</tt> function, even if it's not the homepage. Here is what a user can define : |
||
+ | * a default homepage (with the latest posts) |
||
+ | * a static homepage and no blog page |
||
+ | * a static homepage and a blog page |
||
+ | |||
+ | When you use <tt>is_home()</tt> and <tt>is_front_page()</tt>, you have to use them in the right order to avoid bugs and to test every user configuration: |
||
+ | |||
+ | <pre>if ( is_front_page() && is_home() ) { |
||
+ | // Default homepage |
||
+ | } elseif ( is_front_page() ) { |
||
+ | // static homepage |
||
+ | } elseif ( is_home() ) { |
||
+ | // blog page |
||
+ | } else { |
||
+ | //everything else |
||
+ | }</pre> |
||
=== The Administration Panels === |
=== The Administration Panels === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_admin|is_admin()]]</tt>: When the Dashboard or the administration panels are being displayed. |
+ | ; <tt>[[Function_Reference/is_network_admin|is_network_admin()]]</tt>: When the Network Dashboard or the Network administration panels for multisite are being displayed. |
||
+ | '''Attention''': The '''wp-login.php''' page is not an admin page. To check if this page is displayed, use <tt>[[Global_Variables#Admin_Globals|the admin global variable]]</tt> '''$pagenow'''. |
||
+ | |||
+ | === The Admin Bar === |
||
+ | |||
+ | ; <tt>[[Function_Reference/is_admin_bar_showing|is_admin_bar_showing()]]</tt> : Returns <tt>true</tt> if the admin bar will be displayed. |
||
+ | |||
+ | ''Note'' : To display or not this bar, use [[Function_Reference/show_admin_bar|show_admin_bar()]], this function should be called immediately upon ''plugins_loaded'' or placed in the theme's ''functions.php'' file. |
||
=== A Single Post Page === |
=== A Single Post Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_single|is_single()]]</tt> : When a single post of any post type (except attachment and page post types) is being displayed. |
; <tt>is_single( '17' )</tt> : When Post 17 is being displayed as a single Post. |
; <tt>is_single( '17' )</tt> : When Post 17 is being displayed as a single Post. |
||
; <tt>is_single( 'Irish Stew' )</tt> : When the Post with Title "Irish Stew" is being displayed as a single Post. |
; <tt>is_single( 'Irish Stew' )</tt> : When the Post with Title "Irish Stew" is being displayed as a single Post. |
||
Line 45: | Line 76: | ||
=== A Sticky Post === |
=== A Sticky Post === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_sticky|is_sticky()]]</tt> : Returns true if "Stick this post to the front page" check box has been checked for the current post. In this example, no post ID argument is given, so the post ID for the [[The Loop|Loop]] post is used. |
; <tt>is_sticky( '17' )</tt> : Returns true when Post 17 is considered a sticky post. |
; <tt>is_sticky( '17' )</tt> : Returns true when Post 17 is considered a sticky post. |
||
− | |||
− | === A Post Type === |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/get_post_type get_post_type()]</tt> : Not really a conditional tag, but returns the [[Function_Reference/register_post_type|registered post type]] of the current post. |
||
− | ; <tt>if ( 'book' == get_post_type() ) ...</tt> : Tests to see if the current post is of type 'book'. |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/post_type_exists post_type_exists()]</tt> : Returns true if a given post type is a [[Function_Reference/register_post_type|registered post type]]. This does not test if a post is a certain post_type. Note: This function replaces a function called ''is_post_type'' which existed briefly in 3.0 development. |
||
=== A Post Type is Hierarchical === |
=== A Post Type is Hierarchical === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_post_type_hierarchical|is_post_type_hierarchical( $post_type )]]</tt> : Returns true if this $post_type has been set with [[Function_Reference/register_post_type|hierarchical support when registered]]. |
; <tt>is_post_type_hierarchical( 'book' )</tt> : Returns true if the book post type was registered as having support for hierarchical. |
; <tt>is_post_type_hierarchical( 'book' )</tt> : Returns true if the book post type was registered as having support for hierarchical. |
||
=== A Post Type Archive === |
=== A Post Type Archive === |
||
− | + | ;<tt>[[Function_Reference/is_post_type_archive|is_post_type_archive()]]</tt> : Returns true on any post type archive. |
|
− | + | ;<tt>is_post_type_archive( $post_type )</tt> : Returns true if on a post type archive page that matches $post_type. |
|
+ | ;<tt>is_post_type_archive( array( 'foo', 'bar', 'baz' ) )</tt> : Returns true if on a post type archive page that matches either "foo", "bar", or "baz". |
||
− | |||
− | To turn on post type archives, use 'has_archive' => true, when [ |
+ | To turn on post type archives, use 'has_archive' => true, when [[Function_Reference/register_post_type|registering the post type]]. |
=== A Comments Popup === |
=== A Comments Popup === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_comments_popup|is_comments_popup()]]</tt> : When in Comments Popup window. |
=== Any Page Containing Posts === |
=== Any Page Containing Posts === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/comments_open|comments_open()]]</tt>: When comments are allowed for the current Post being processed in the [[The Loop|WordPress Loop]]. |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/pings_open|pings_open()]]</tt>: When pings are allowed for the current Post being processed in the [[The Loop|WordPress Loop]]. |
=== A PAGE Page === |
=== A PAGE Page === |
||
− | This section refers to WordPress [[Pages]], not any generic |
+ | This section refers to WordPress [[Pages]], not any generic web page from your blog, or in other words to the built in ''post_type'' 'page'. |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_page|is_page()]]</tt> : When any [[Pages|Page]] is being displayed. |
; <tt>is_page( 42 )</tt> : When [[Pages|Page]] 42 (ID) is being displayed. |
; <tt>is_page( 42 )</tt> : When [[Pages|Page]] 42 (ID) is being displayed. |
||
; <tt>is_page( 'About Me And Joe' )</tt> : When the [[Pages|Page]] with a ''post_title'' of "About Me And Joe" is being displayed. |
; <tt>is_page( 'About Me And Joe' )</tt> : When the [[Pages|Page]] with a ''post_title'' of "About Me And Joe" is being displayed. |
||
Line 82: | Line 108: | ||
; <tt>is_page( array( 42, 54, 6 ) )</tt> : Returns true when the [[Pages|Pages]] displayed is either ''post ID'' = 42, or ''post ID'' = 54, or ''post ID'' = 6. |
; <tt>is_page( array( 42, 54, 6 ) )</tt> : Returns true when the [[Pages|Pages]] displayed is either ''post ID'' = 42, or ''post ID'' = 54, or ''post ID'' = 6. |
||
+ | See also [[Function_Reference/is_page|is_page()]] for more snippets, such as <tt>is_subpage</tt>, <tt>is_tree</tt>. |
||
− | ==== Testing for paginated Pages ==== |
||
+ | ''Note'': There is no function to check if a page is a sub-page. We can get around the problem: |
||
− | You can use this code to check whether you're on the nth page in a Post or PAGE Page that has been divided into pages using the <tt><!<nowiki></nowiki>--nextpage--></tt> QuickTag. This can be useful, for example, if you wish to display meta-data only on the first page of a post divided into several pages. |
||
+ | <pre>if ( is_page() && $post->post_parent > 0 ) { |
||
− | '''Example 1''' |
||
− | + | echo "This is a child page"; |
|
+ | }</pre> |
||
− | $paged = $wp_query->get( 'paged' ); |
||
+ | === Is a Page Template === |
||
− | if ( ! $paged || $paged < 2 ) |
||
+ | |||
− | { |
||
− | // This is not a paginated page (or it's simply the first page of a paginated page/post) |
||
− | |||
− | } |
||
− | else |
||
− | { |
||
− | // This is a paginated page. |
||
− | |||
− | } |
||
− | ?> |
||
− | </pre> |
||
− | |||
− | '''Example 2''' |
||
− | <!-- Could someone please take a look at the difference between 'paged' & 'page'? I ran into some support requests, where people said it's 'page', not 'paged' --> |
||
− | |||
− | <pre><?php |
||
− | $paged = get_query_var( 'page' ) ? get_query_var( 'page' ) : false; |
||
− | if ( $paged === false ) |
||
− | { |
||
− | // This is not a paginated page (or it's simply the first page of a paginated page/post) |
||
− | |||
− | } |
||
− | else |
||
− | { |
||
− | // This is a paginated page. |
||
− | |||
− | } |
||
− | ?></pre> |
||
− | |||
− | ==== Testing for sub-Pages ==== |
||
− | |||
− | There is no <tt>is_subpage()</tt> function yet, but you can test this with a little code: |
||
− | |||
− | '''Snippet 1''' |
||
− | <pre><?php |
||
− | |||
− | global $post; // if outside the loop |
||
− | |||
− | if ( is_page() && $post->post_parent ) { |
||
− | // This is a subpage |
||
− | |||
− | } else { |
||
− | // This is not a subpage |
||
− | } |
||
− | ?></pre> |
||
− | |||
− | You can create your own is_subpage() function using the code in Snippet 2. Add it to your [http://codex.wordpress.org/Theme_Development#Theme_Functions_File functions.php] file. It tests for a parent page in the same way as Snippet 1, but will return the ID of the page parent if there is one, or <tt>false</tt> if there isn't. |
||
− | |||
− | '''Snippet 2''' |
||
− | <pre> |
||
− | function is_subpage() { |
||
− | global $post; // load details about this page |
||
− | |||
− | if ( is_page() && $post->post_parent ) { // test to see if the page has a parent |
||
− | return $post->post_parent; // return the ID of the parent post |
||
− | |||
− | } else { // there is no parent so ... |
||
− | return false; // ... the answer to the question is false |
||
− | } |
||
− | } |
||
− | </pre> |
||
− | |||
− | It is advisable to use a function like that in Snippet 2, rather than using the simple test like Snippet 1, if you plan to test for sub pages frequently. |
||
− | |||
− | To test if the parent of a page is a specific page, for instance "About" (page id ''pid'' 2 by default), we can use the tests in Snippet 3. These tests check to see if we are looking at the page in question, as well as if we are looking at any child pages. This is useful for setting variables specific to different sections of a web site, so a different banner image, or a different heading. |
||
− | |||
− | '''Snippet 3''' |
||
− | <pre> |
||
− | <?php |
||
− | |||
− | if ( is_page( 'about' ) || '2' == $post->post_parent ) { |
||
− | // the page is "About", or the parent of the page is "About" |
||
− | $bannerimg = 'about.jpg'; |
||
− | |||
− | } elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) { |
||
− | $bannerimg = 'teaching.jpg'; |
||
− | |||
− | } elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { |
||
− | $bannerimg = 'admissions.jpg'; |
||
− | |||
− | } else { |
||
− | $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page |
||
− | } |
||
− | |||
− | ?> |
||
− | </pre> |
||
− | |||
− | Snippet 4 is a function that allows you to carry out the tests above more easily. This function will return true if we are looking at the page in question (so "About") or one of its sub pages (so a page with a parent with ID "2"). |
||
− | |||
− | '''Snippet 4''' |
||
− | <pre> |
||
− | function is_tree( $pid ) { // $pid = The ID of the page we're looking for pages underneath |
||
− | global $post; // load details about this page |
||
− | |||
− | if ( is_page($pid) ) |
||
− | return true; // we're at the page or at a sub page |
||
− | |||
− | $anc = get_post_ancestors( $post->ID ); |
||
− | foreach ( $anc as $ancestor ) { |
||
− | if( is_page() && $ancestor == $pid ) { |
||
− | return true; |
||
− | } |
||
− | } |
||
− | |||
− | return false; // we arn't at the page, and the page is not an ancestor |
||
− | } |
||
− | </pre> |
||
− | |||
− | Add Snippet 4 to your [http://codex.wordpress.org/Theme_Development#Theme_Functions_File functions.php] file, and call <tt>is_tree( 'id' )</tt> to see if the current page is the page, or is a sub page of the page. In Snippet 3, <tt>is_tree( '2' )</tt> would replace "<tt>is_page( 'about' ) || '2' == $post->post_parent</tt>" inside the first <tt>if</tt> tag. |
||
− | |||
− | Note that if you have more than one level of pages the parent page is the one directly above and not the one at the very top of the hierarchy. |
||
− | |||
− | ====Is a Page Template==== |
||
Allows you to determine whether or not you are in a page template or if a specific page template is being used. |
Allows you to determine whether or not you are in a page template or if a specific page template is being used. |
||
+ | ; <tt>[[Function_Reference/is_page_template|is_page_template()]]</tt> : Is a [[Page_Templates|Page Template]] being used? |
||
+ | ; <tt>is_page_template( 'about.php' )</tt> : Is [[Page_Templates|Page Template]] 'about' being used? |
||
+ | ''Note'': if the file is in a subdirectory you must include this as well. Meaning that this should be the filepath in relation to your theme as well as the filename, for example <tt>page-templates/about.php</tt>. |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/is_page_template is_page_template()]</tt> : Is a [[Pages#Page_Templates|Page Template]] being used? |
||
− | ; <tt>is_page_template( 'about.php' )</tt> : Is [[Pages#Page_Templates|Page Template]] 'about' being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php. |
||
=== A Category Page === |
=== A Category Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_category|is_category( $category )]]</tt> : When the actual page is associated with the <tt>$category</tt> Category. |
; <tt>is_category( '9' )</tt> : When the archive page for Category 9 is being displayed. |
; <tt>is_category( '9' )</tt> : When the archive page for Category 9 is being displayed. |
||
; <tt>is_category( 'Stinky Cheeses' )</tt> : When the archive page for the Category with Name "Stinky Cheeses" is being displayed. |
; <tt>is_category( 'Stinky Cheeses' )</tt> : When the archive page for the Category with Name "Stinky Cheeses" is being displayed. |
||
; <tt>is_category( 'blue-cheese' )</tt> : When the archive page for the Category with Category Slug "blue-cheese" is being displayed. |
; <tt>is_category( 'blue-cheese' )</tt> : When the archive page for the Category with Category Slug "blue-cheese" is being displayed. |
||
; <tt>is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) )</tt> : Returns true when the category of posts being displayed is either term_ID 9, or ''slug'' "blue-cheese", or ''name'' "Stinky Cheeses". |
; <tt>is_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) )</tt> : Returns true when the category of posts being displayed is either term_ID 9, or ''slug'' "blue-cheese", or ''name'' "Stinky Cheeses". |
||
+ | ; <tt>is_category( 5 ) || cat_is_ancestor_of( 5, get_query_var( 'cat' ) )</tt> : Returns true when the category of posts being displayed is either term_id 5, or an ancestor of term_id 5 (subcategory, sub-subcategory...). |
||
− | ; <tt>in_category( '5' )</tt> : Returns true if the current post is '''in''' the specified category id. [http://codex.wordpress.org/Template_Tags/in_category read more] |
||
+ | ; <tt>in_category( '5' )</tt> : Returns true if the current post is '''in''' the specified category id ([[Template_Tags/in_category|read more]]). |
||
; <tt>in_category( array( 1,2,3 ) )</tt> : Returns true if the current post is '''in''' either category 1, 2, or 3. |
; <tt>in_category( array( 1,2,3 ) )</tt> : Returns true if the current post is '''in''' either category 1, 2, or 3. |
||
; <tt>! in_category( array( 4,5,6 ) )</tt> : Returns true if the current post is '''NOT in''' either category 4, 5, or 6. Note the '''!''' at the beginning. |
; <tt>! in_category( array( 4,5,6 ) )</tt> : Returns true if the current post is '''NOT in''' either category 4, 5, or 6. Note the '''!''' at the beginning. |
||
− | '''Note:''' Be sure to check your spelling when testing |
+ | '''Note:''' Be sure to check your spelling when testing: "is" and "in" are significantly different. |
See also <tt>[[#Any Archive Page|is_archive()]]</tt> and [[Category Templates]]. |
See also <tt>[[#Any Archive Page|is_archive()]]</tt> and [[Category Templates]]. |
||
Line 225: | Line 141: | ||
=== A Tag Page === |
=== A Tag Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_tag|is_tag()]]</tt> : When any Tag archive page is being displayed. |
; <tt>is_tag( 'mild' )</tt> : When the archive page for tag with the slug of 'mild' is being displayed. |
; <tt>is_tag( 'mild' )</tt> : When the archive page for tag with the slug of 'mild' is being displayed. |
||
; <tt>is_tag( array( 'sharp', 'mild', 'extreme' ) )</tt> : Returns true when the tag archive being displayed has a slug of either "sharp", "mild", or "extreme". |
; <tt>is_tag( array( 'sharp', 'mild', 'extreme' ) )</tt> : Returns true when the tag archive being displayed has a slug of either "sharp", "mild", or "extreme". |
||
− | ; <tt>has_tag()</tt> : When the current post has a tag. |
+ | ; <tt>[[Function_Reference/has_tag|has_tag()]]</tt> : When the current post has a tag. Prior to 2.7, must be used inside The Loop. |
; <tt>has_tag( 'mild' )</tt> : When the current post has the tag 'mild'. |
; <tt>has_tag( 'mild' )</tt> : When the current post has the tag 'mild'. |
||
; <tt>has_tag( array( 'sharp', 'mild', 'extreme' ) )</tt> : When the current post has any of the tags in the array. |
; <tt>has_tag( array( 'sharp', 'mild', 'extreme' ) )</tt> : When the current post has any of the tags in the array. |
||
Line 234: | Line 150: | ||
See also <tt>[[#Any Archive Page|is_archive()]]</tt> and [[Tag Templates]]. |
See also <tt>[[#Any Archive Page|is_archive()]]</tt> and [[Tag Templates]]. |
||
− | === A Taxonomy Page === |
+ | === A Taxonomy Page (and related) === |
+ | ==== is_tax ==== |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/is_tax is_tax()]</tt> : When any Taxonomy archive page is being displayed. |
||
+ | ; <tt>[[Function_Reference/is_tax|is_tax()]]</tt> : True for custom taxonomy archive pages, false for built-in taxonomies (category and tag archives). |
||
; <tt>is_tax( 'flavor' )</tt> : When a Taxonomy archive page for the flavor taxonomy is being displayed. |
; <tt>is_tax( 'flavor' )</tt> : When a Taxonomy archive page for the flavor taxonomy is being displayed. |
||
; <tt>is_tax( 'flavor', 'mild')</tt> : When the archive page for the flavor taxonomy with the slug of 'mild' is being displayed. |
; <tt>is_tax( 'flavor', 'mild')</tt> : When the archive page for the flavor taxonomy with the slug of 'mild' is being displayed. |
||
; <tt>is_tax( 'flavor', array( 'sharp', 'mild', 'extreme' ) )</tt> : Returns true when the flavor taxonomy archive being displayed has a slug of either "sharp", "mild", or "extreme". |
; <tt>is_tax( 'flavor', array( 'sharp', 'mild', 'extreme' ) )</tt> : Returns true when the flavor taxonomy archive being displayed has a slug of either "sharp", "mild", or "extreme". |
||
+ | ==== has_term ==== |
||
− | See also <tt>[[#Any Archive Page|is_archive()]]</tt>. |
||
+ | ; <tt>[[Function_Reference/has_term|has_term()]]</tt> : Check if the current post has any of given terms. The first parameter should be an empty string. It expects a taxonomy slug/name as a second parameter. |
||
+ | ; <tt>has_term( 'green', 'color' )</tt> : When the current post has the term 'green' from taxonomy 'color'. |
||
+ | ; <tt>has_term( array( 'green', 'orange', 'blue' ), 'color' )</tt> : When the current post has any of the terms in the array. |
||
+ | |||
+ | ==== term_exists ==== |
||
+ | ; <tt>[[Function_Reference/term_exists|term_exists( $term, $taxonomy, $parent )]]</tt> : Returns <tt>true</tt> if <tt>$term</tt> exists in any taxonomy. If <tt>$taxonomy</tt> is given, the term must exist in this one. The 3rd parameter <tt>$parent</tt> is also optional, if given, the term have to be a child of this parent, the taxonomy must be hierarchical. |
||
+ | ==== is_taxonomy_hierarchical ==== |
||
− | === A Registered Taxonomy === |
||
+ | ; <tt>[[Function_Reference/is_taxonomy_hierarchical|is_taxonomy_hierarchical( $taxonomy )]]</tt> : Returns <tt>true</tt> if the taxonomy <tt>$taxonomy</tt> is hierarchical. To declare a taxonomy hierarchical, use <tt>'hierarchical' => true</tt> when using [[Function_Reference/register_taxonomy|register_taxonomy()]]. |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/taxonomy_exists taxonomy_exists()]</tt> : When a particular taxonomy is registered via [[Function_Reference/register_taxonomy|register_taxonomy()]]. Formerly is_taxonomy(), which was deprecated in [[Version 3.0]] |
||
+ | |||
+ | ==== taxonomy_exists ==== |
||
+ | ;<tt>[[Function_Reference/taxonomy_exists|taxonomy_exists( $taxonomy )]]</tt> : Returns <tt>true</tt> if <tt>$taxonomy</tt> has been registered on this site using [[Function_Reference/register_taxonomy|register_taxonomy()]]. |
||
+ | |||
+ | See also <tt>[[#Any Archive Page|is_archive()]]</tt>. |
||
=== An Author Page === |
=== An Author Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_author|is_author()]]</tt> : When any Author page is being displayed. |
; <tt>is_author( '4' )</tt> : When the archive page for Author number (ID) 4 is being displayed. |
; <tt>is_author( '4' )</tt> : When the archive page for Author number (ID) 4 is being displayed. |
||
; <tt>is_author( 'Vivian' )</tt> : When the archive page for the Author with Nickname "Vivian" is being displayed. |
; <tt>is_author( 'Vivian' )</tt> : When the archive page for the Author with Nickname "Vivian" is being displayed. |
||
Line 259: | Line 187: | ||
=== A Date Page === |
=== A Date Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_date|is_date()]]</tt> : When ''any'' date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive). |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_year|is_year()]]</tt> : When a yearly archive is being displayed. |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_month|is_month()]]</tt> : When a monthly archive is being displayed. |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_day|is_day()]]</tt> : When a daily archive is being displayed. |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_time|is_time()]]</tt> : When an hourly, "minutely", or "secondly" archive is being displayed. |
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_new_day|is_new_day()]]</tt> : If today is a new day according to post date. Should be used inside the loop. |
− | ; <tt>[http://codex.wordpress.org/Function_Reference/is_new_day is_new_day()]</tt> : If today is a new day according to post date. Should be used inside the loop. |
||
See also <tt>[[#Any Archive Page|is_archive()]]</tt>. |
See also <tt>[[#Any Archive Page|is_archive()]]</tt>. |
||
Line 271: | Line 198: | ||
=== Any Archive Page === |
=== Any Archive Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_archive|is_archive()]]</tt> : When ''any'' type of Archive page is being displayed. Category, Tag, other Taxonomy Term, custom post type archive, Author and Date-based pages are all types of Archives. |
− | |||
=== A Search Result Page === |
=== A Search Result Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_search|is_search()]]</tt> : When a search result page archive is being displayed. |
=== A 404 Not Found Page === |
=== A 404 Not Found Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_404|is_404()]]</tt> : When a page displays after an "HTTP 404: Not Found" error occurs. |
=== A Paged Page === |
=== A Paged Page === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_paged|is_paged()]]</tt> : When the page being displayed is "paged". This refers to an archive or the main page being split up over several pages and will return true on 2nd and subsequent pages of posts. This does ''not'' refer to a Post or [[Pages|Page]] whose content has been divided into pages using the <tt><!<nowiki></nowiki>--nextpage--></tt> [[Writing Posts#Here is a run-down of the quicktag functions:|QuickTag]]. To check if a Post or Page has been divided into pages using the <tt><!<nowiki></nowiki>--nextpage--></tt> QuickTag, see [[#A_Paged_Page|A_Paged_Page]] section. |
=== An Attachment === |
=== An Attachment === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_attachment|is_attachment()]]</tt> : When an attachment document to a post or [[Pages|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. |
+ | See also [[Using Image and File Attachments]]. |
||
+ | === Attachment Is Image === |
||
− | === A Single Page, Single Post or Attachment === |
||
+ | ; <tt>[[Function_Reference/wp_attachment_is_image|wp_attachment_is_image( $post_id )]]</tt> : Returns <tt>true</tt> if the attached file to the post with <tt>ID</tt> equal to <tt>$post_id</tt> is an image. ''Mime'' formats and extensions allowed are: .jpg, .jpeg, .gif, et .png. |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/is_singular is_singular()]</tt> : When any of the following return true: <tt>is_single()</tt>, <tt>is_page()</tt> or <tt>is_attachment()</tt>. |
||
+ | |||
− | ; <tt>is_singular( 'book' )</tt> : True when viewing a post of the [[Custom Post Types]] book. |
||
+ | === A Local Attachment === |
||
− | ; <tt>is_singular( array( 'newspaper', 'book' ) )</tt> : True when viewing a post of the [[Custom Post Types]] newspaper or book. |
||
+ | |||
+ | ; <tt>[[Function_Reference/is_local_attachment|is_local_attachment( $url )]]</tt> : Returns <tt>true</tt> if the link passed in <tt>$url</tt> is a real attachment file from this site. |
||
+ | |||
+ | === A Single Page, a Single Post, an Attachment or Any Other Custom Post Type === |
||
+ | ; <tt>[[Function_Reference/is_singular|is_singular()]]</tt> : Returns <tt>true</tt> for any <tt>is_single()</tt>, <tt>is_page()</tt>, or <tt>is_attachment()</tt>. |
||
+ | ; <tt>is_singular( 'foo' )</tt> : Returns <tt>true</tt> if the <tt>post_type</tt> is "foo". |
||
+ | ; <tt>is_singular( array( 'foo', 'bar', 'baz' ) )</tt> : Returns <tt>true</tt> if the <tt>post_type</tt> is "foo", "bar", or "baz". |
||
+ | |||
+ | See also the [[Custom Post Types]] book. |
||
+ | |||
+ | === Post Type Exists === |
||
+ | ; <tt>[[Function_Reference/post_type_exists|post_type_exists( $post_type )]]</tt> : Returns <tt>true</tt> is the given <tt>$post_type</tt> has been registered on this site using [[Function_Reference/register_post_type|register_post_type()]]. |
||
+ | |||
+ | === Is Main Query === |
||
+ | ; <tt>[[Function_Reference/is_main_query|is_main_query()]]</tt>: Returns <tt>true</tt> when the current query (such as within the loop) is the "main" query. |
||
+ | |||
+ | '''Example with the filter hook the_content''' |
||
+ | <pre> |
||
+ | add_action( 'the_content', 'baw_add_social_buttons' ); |
||
+ | function baw_add_social_buttons( $content ) { |
||
+ | if ( ! is_admin() && is_main_query() ) { |
||
+ | return $content . function_from_a_social_plugin(); |
||
+ | } |
||
+ | return $content; |
||
+ | } |
||
+ | </pre> |
||
+ | If, when WordPress tries to display the content of each post in the Loop or in a single post page, we are in the main query, and not admin side, we add some social buttons (for example). |
||
+ | |||
+ | '''Example with the action hook pre_get_posts''' |
||
+ | <pre> |
||
+ | add_action( 'pre_get_posts', 'baw_modify_query_exclude_category' ); |
||
+ | function baw_modify_query_exclude_category( $query ) { |
||
+ | if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) { |
||
+ | $query->set( 'cat', '-5' ); |
||
+ | } |
||
+ | } |
||
+ | </pre> |
||
+ | With <tt>pre_get_posts</tt>, this is not possible to call directly <tt>is_main_query</tt>, we should use <tt>$query</tt> given as a parameter. |
||
+ | |||
+ | === A New Day === |
||
+ | |||
+ | ; <tt>[[Function_Reference/is_new_day|is_new_day()]]</tt> : Returns <tt>true</tt> if today is a new day. |
||
=== A Syndication === |
=== A Syndication === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_feed|is_feed()]]</tt> : When the site requested is a [[Introduction_to_Blogging#Syndication|Syndication]]. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers. |
=== A Trackback === |
=== A Trackback === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_trackback|is_trackback()]]</tt> : 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. |
=== A Preview === |
=== A Preview === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_preview|is_preview()]]</tt> : When a single post being displayed is viewed in Draft mode. |
=== Has An Excerpt === |
=== Has An Excerpt === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/has_excerpt|has_excerpt()]]</tt> : When the current post has an excerpt |
; <tt>has_excerpt( 42 )</tt> : When the post 42 (ID) has an excerpt. |
; <tt>has_excerpt( 42 )</tt> : When the post 42 (ID) has an excerpt. |
||
+ | === Has A Nav Menu Assigned === |
||
− | <pre><?php |
||
+ | ; <tt>[[Function_Reference/has_nav_menu|has_nav_menu()]]</tt> : Returns <tt>true</tt> when a registered nav menu location has a menu assigned. |
||
− | // Get $post if you're inside a function |
||
− | global $post; |
||
+ | See also [[Function_Reference/register_nav_menu|register_nav_menu()]]. |
||
− | if ( empty( $post->post_excerpt ) ) { |
||
− | // This post has no excerpt |
||
− | } else { |
||
− | // This post has excerpt |
||
− | } |
||
− | ?></pre> |
||
+ | === Inside The Loop === |
||
+ | ; <tt>[[Function_Reference/in_the_loop|in_the_loop()]]</tt> : 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 Dynamic SideBar === |
||
− | <b>Other Use</b> |
||
+ | ; <tt>[[Function_Reference/is_dynamic_sidebar|is_dynamic_sidebar()]]</tt> : Returns <tt>true</tt> if the theme supports dynamic sidebars. |
||
+ | === Is Sidebar Active === |
||
− | When you need to hide the auto displayed excerpt and only display your post's excerpts. |
||
+ | ; <tt>[[Function_Reference/is_active_sidebar|is_active_sidebar()]]</tt> : Check to see if a given sidebar is active (in use). Returns ''true'' if the sidebar (identified by name or id) is in use. |
||
+ | ''Note'': To display a sidebar's content, use [[Function_Reference/dynamic_sidebar|dynamic_sidebar( $sidebar )]]. |
||
− | <pre> |
||
− | <?php if ( ! has_excerpt() ) { |
||
− | echo ''; |
||
− | } else { |
||
− | the_excerpt(); |
||
− | } |
||
− | </pre> |
||
+ | === Is Widget Active === |
||
− | Replace auto excerpt for a text or code. |
||
+ | ; <tt>[[Function_Reference/is_active_widget|is_active_widget( $widget_callback, $widget_id )]]</tt> : Returns <tt>true</tt> if the widget with callback <tt>$widget_callback</tt> or it's <tt>ID</tt> is <tt>$widget_id</tt>will be displayed on front-end. |
||
− | <pre> |
||
− | <?php if ( ! has_excerpt() ) {?> |
||
− | <!-- you text or code --> |
||
− | <?php } ?> |
||
− | </pre> |
||
+ | ''Note'' : To be effective this function has to run after widgets have initialized, at action 'init' or later, see [[Plugin_API/Action_Reference|Action Reference]]. |
||
− | === Has A Nav Menu Assigned === |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/has_nav_menu has_nav_menu()]</tt> : Whether a registered nav menu location has a menu assigned |
||
− | Returns: assigned(true) or not(false) |
||
− | === |
+ | === Is Blog Installed === |
+ | ; <tt>[[Function_Reference/is_blog_installed|is_blog_installed()]]</tt> : Returns <tt>true</tt> if the current is properly installed. |
||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/in_the_loop in_the_loop()]</tt> : Check to see if you are "inside the loop". Useful for plugin authors, this conditional returns as true when you are inside the loop. |
||
+ | ''Note'': The cache will be checked first. If you have a cache plugin, which saves the cache values, then this will work. If you use the default WordPress cache, and the database goes away, then you might have problems. |
||
− | === Is Sidebar Active === |
||
+ | |||
− | ; <tt>[http://codex.wordpress.org/Function_Reference/is_active_sidebar is_active_sidebar()]</tt> : 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. |
||
+ | === Right To Left Reading === |
||
+ | ; <tt>[[Function_Reference/is_rtl|is_rtl()]]</tt> : Returns <tt>true</tt> if the current locale est read from right to left (RTL). |
||
+ | |||
+ | '''Example''' |
||
+ | <pre> |
||
+ | if ( is_rtl() ) { |
||
+ | wp_enqueue_style( 'style-rtl', plugins_url( '/css/style-rtl.css', __FILE__ ) ); |
||
+ | wp_enqueue_script( 'script-rtl', plugins_url( '/js/script-rtl.js', __FILE__ ) ); |
||
+ | } |
||
+ | </pre> |
||
=== Part of a Network (Multisite) === |
=== Part of a Network (Multisite) === |
||
− | ; <tt>[ |
+ | ; <tt>[[Function_Reference/is_multisite|is_multisite()]]</tt> : Check to see whether the current site is in a WordPress MultiSite install. |
+ | |||
+ | === Main Site (Multisite) === |
||
+ | ; <tt>[[Function_Reference/is_main_site|is_main_site()]]</tt> : Determine if a site is the main site in a network. |
||
=== Admin of a Network (Multisite) === |
=== Admin of a Network (Multisite) === |
||
; <tt>[[Function_Reference/is_super_admin|is_super_admin()]]</tt> : Determine if user is a network (super) admin. |
; <tt>[[Function_Reference/is_super_admin|is_super_admin()]]</tt> : Determine if user is a network (super) admin. |
||
+ | |||
+ | === Is User Logged in === |
||
+ | ; <tt>[[Function_Reference/is_user_logged_in|is_user_logged_in()]]</tt> : Returns <tt>true</tt> if any user is currently logged-in, any roles. |
||
+ | |||
+ | === Email Exists === |
||
+ | ; <tt>[[Function_Reference/email_exists|email_exists( $email )]]</tt> : Check whether or not the given email address <tt>$email</tt> has already been registered to a username, and returns that user's <tt>ID</tt> or <tt>false</tt> if does not exists. |
||
+ | |||
+ | === Username Exists === |
||
+ | ; <tt>[[Function_Reference/username_exists|username_exists( $username )]]</tt> : Check whether or not the given username <tt>$username</tt> has already been registered to a username, and returns that user's <tt>ID</tt> or <tt>false</tt> if does not exists. |
||
=== An Active Plugin === |
=== An Active Plugin === |
||
− | ; <tt>[[Function_Reference/is_plugin_active|is_plugin_active()]]</tt> : Checks if a plugin is activated. |
+ | ; <tt>[[Function_Reference/is_plugin_active|is_plugin_active( $path )]]</tt> : Checks if a plugin is activated. |
+ | ; <tt>is_plugin_active( 'akismet/akismet.php' )</tt> : Checks if ''Akismet'' is activated. |
||
+ | ; <tt>[[Function_Reference/is_plugin_inactive|is_plugin_inactive( $path )]]</tt> : Checks if a plugin is deactivated. Same as <tt>! is_plugin_active( $path )</tt>. |
||
+ | ; <tt>[[Function_Reference/is_plugin_active_for_network|is_plugin_active_for_network( $path )]]</tt> : Same thing for a network activation on a multisite installation. |
||
+ | ; <tt>[[Function_Reference/is_plugin_page|is_plugin_page()]]</tt> : Returns <tt>true</tt> if the loaded page, admin side is a plugin's one. '''This function is deprecated depuis since WordPress 3.1, with no known alternative.''' |
||
+ | |||
+ | === A Child Theme === |
||
+ | ; <tt>[[Function Reference/is_child_theme|is_child_theme()]]</tt> : Check whether a child theme is in use. |
||
=== Theme supports a feature === |
=== Theme supports a feature === |
||
; <tt>[[Function Reference/current_theme_supports|current_theme_supports()]]</tt> : Check if various theme features exist. |
; <tt>[[Function Reference/current_theme_supports|current_theme_supports()]]</tt> : Check if various theme features exist. |
||
+ | ; <tt>current_theme_support( 'post-thumbnails' )</tt> : Returns <tt>true</tt> if the current theme supports [[Post_Thumbnails|featured images]]. To add a theme support functionality, use [[Function_Reference/add_theme_support|add_theme_support()]]. |
||
+ | |||
+ | === Has Post Thumbnail === |
||
+ | ; <tt>[[Function_Reference/has_post_thumbnail|has_post_thumbnail( $post_id )]]</tt> : Returns <tt>true</tt> if the post with <tt>ID</tt> equal to <tt>$post_id</tt> contains a featured image. Theme should support Featured Images, see above. |
||
+ | |||
+ | === Script Is In use === |
||
+ | ; <tt>[[Function_Reference/wp_script_is|wp_script_is( $handle, $list )]]</tt> : Returns <tt>true</tt> if the script with ''handle'' is <tt>$handle</tt> has been 'registered', 'enqueue/queue', 'done', ou 'to_do' depending on <tt>$list</tt>. |
||
+ | |||
+ | '''Example''' |
||
+ | <pre> |
||
+ | $handle = 'fluidVids.js'; |
||
+ | $list = 'enqueued'; |
||
+ | if ( wp_script_is( $handle, $list ) ) { |
||
+ | return; |
||
+ | } else { |
||
+ | wp_register_script( 'fluidVids.js', plugin_dir_url(__FILE__).'js/fluidvids.min.js'); |
||
+ | wp_enqueue_script( 'fluidVids.js' ); |
||
+ | } |
||
+ | </pre> |
||
+ | This would check if the script named 'fluidVids.js' is enqueued. If it is not enqueued, the files are then registered and enqueued. |
||
+ | |||
+ | === Is Previewed in the Customizer === |
||
+ | ; <tt>[[Function_Reference/is_customize_preview|is_customize_preview()]]</tt> : Returns <tt>true</tt> if the site is being previewed in the [[Theme_Customization_API|Customizer]], <tt>false</tt> otherwise. |
||
==Working Examples== |
==Working Examples== |
||
Line 371: | Line 378: | ||
This example shows how to use <tt>is_single()</tt> to display something specific only when viewing a single post page: |
This example shows how to use <tt>is_single()</tt> to display something specific only when viewing a single post page: |
||
+ | |||
<pre> |
<pre> |
||
if ( is_single() ) { |
if ( is_single() ) { |
||
+ | |||
− | echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!'; |
||
+ | echo 'This post\'s title is ' . get_the_title(); |
||
+ | |||
} |
} |
||
</pre> |
</pre> |
||
+ | Add this custom function to your child themes functions.php file and modify the conditional tag to suit your needs. |
||
+ | <pre> |
||
+ | add_action( 'loop_start', 'add_to_single_posts' ); |
||
+ | function add_to_single_posts() { |
||
+ | if ( is_single('post') ) { |
||
+ | echo'<div class="your-class">Your Text or HTML</div>'; |
||
+ | } |
||
+ | } |
||
+ | </pre> |
||
+ | You could also use: |
||
− | 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. |
||
+ | |||
+ | <pre> |
||
+ | add_action( 'loop_start', 'wpsites_add_to_single_posts' ); |
||
+ | function wpsites_add_to_single_posts() { |
||
+ | if ( is_single() ) { |
||
+ | echo'<div class="your-class">Your Text or HTML</div>'; |
||
+ | } |
||
+ | } |
||
+ | </pre> |
||
+ | |||
+ | Another example of how to use Conditional Tags in the Loop. Choose to display content or excerpt in index.php when this is a display single post or the home page. |
||
<pre> |
<pre> |
||
if ( is_home() || is_single() ) { |
if ( is_home() || is_single() ) { |
||
+ | |||
the_content(); |
the_content(); |
||
+ | |||
} |
} |
||
else { |
else { |
||
+ | |||
the_excerpt(); |
the_excerpt(); |
||
+ | |||
} |
} |
||
</pre> |
</pre> |
||
− | When you need display a code or element, in a place that is |
+ | When you need display a code or element, in a place that is NOT the home page. |
<pre> |
<pre> |
||
Line 396: | Line 430: | ||
Insert your markup ... |
Insert your markup ... |
||
− | <?php }?> |
+ | <?php } ?> |
+ | </pre> |
||
+ | |||
+ | ===Check for Multiple Conditionals=== |
||
+ | |||
+ | You can use [http://www.w3schools.com/php/php_operators.asp PHP operators] to evaluate multiple conditionals in a single if statement. |
||
+ | |||
+ | This is handy if you need to check whether combinations of conditionals evaluate to true or false. |
||
+ | |||
+ | <pre> |
||
+ | // Check to see if 2 conditionals are met |
||
+ | |||
+ | |||
+ | if ( is_single() || is_page() ) ) { |
||
+ | |||
+ | // If it's a single post or a single page, do something special |
||
+ | |||
+ | } |
||
+ | |||
+ | if ( is_archive() && ! is_category( 'nachos' ) ) { |
||
+ | |||
+ | // If it's an archive page for any category EXCEPT nachos, do something special |
||
+ | |||
+ | } |
||
+ | </pre> |
||
+ | |||
+ | <pre> |
||
+ | // Check to see if 3 conditionals are met |
||
+ | |||
+ | |||
+ | if ( $query->is_main_query() && is_post_type_archive( 'products' ) && ! is_admin() ) { |
||
+ | |||
+ | // If it's the main query on a custom post type archive for Products |
||
+ | // And if we're not in the WordPress admin, then do something special |
||
+ | |||
+ | } |
||
+ | |||
+ | if ( is_post_type_archive( 'movies' ) || is_tax( 'genre' ) || is_tax( 'actor' ) ) { |
||
+ | |||
+ | // If it's a custom post type archive for Movies |
||
+ | // Or it's a taxonomy archive for Genre |
||
+ | // Or it's a taxonomy archive for Actor, do something special |
||
+ | |||
+ | } |
||
</pre> |
</pre> |
||
Line 408: | Line 485: | ||
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> |
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> |
||
<h2 id="post-<?php the_ID(); ?>"> |
<h2 id="post-<?php the_ID(); ?>"> |
||
− | <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php |
+ | <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"> |
<?php the_title(); ?></a></h2> |
<?php the_title(); ?></a></h2> |
||
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> |
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> |
||
Line 444: | Line 521: | ||
// we're on the home page, so let's show a list of all top-level categories |
// we're on the home page, so let's show a list of all top-level categories |
||
echo "<ul>"; |
echo "<ul>"; |
||
− | + | wp_list_categories( 'optionall=0&sort_column=name&list=1&children=0' ); |
|
echo "</ul>"; |
echo "</ul>"; |
||
} elseif ( is_category() ) { |
} elseif ( is_category() ) { |
||
// we're looking at a single category view, so let's show _all_ the categories |
// we're looking at a single category view, so let's show _all_ the categories |
||
echo "<ul>"; |
echo "<ul>"; |
||
− | + | wp_list_categories( 'optionall=1&sort_column=name&list=1&children=1&hierarchical=1' ); |
|
echo "</ul>"; |
echo "</ul>"; |
||
} elseif ( is_single() ) { |
} elseif ( is_single() ) { |
||
Line 459: | Line 536: | ||
echo "<p>This is my about page!</p>"; |
echo "<p>This is my about page!</p>"; |
||
} elseif ( is_page( 'Colophon' ) ) { |
} elseif ( is_page( 'Colophon' ) ) { |
||
− | echo "<p>This is my colophon page, running on WordPress " . bloginfo( ' |
+ | echo "<p>This is my colophon page, running on WordPress, " . bloginfo( 'name' ) . "</p>"; |
} else { |
} else { |
||
// catch-all for other pages |
// catch-all for other pages |
||
Line 466: | Line 543: | ||
} else { |
} else { |
||
// catch-all for everything else (archives, searches, 404s, etc) |
// catch-all for everything else (archives, searches, 404s, etc) |
||
− | echo "<p> |
+ | echo "<p>That's all.</p>"; |
} // That's all, folks! |
} // That's all, folks! |
||
?> |
?> |
||
− | <form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> |
+ | <form id="searchform" method="get" action="<?php echo esc_url( $_SERVER['PHP_SELF'] ); ?>"> |
<div> |
<div> |
||
<input type="text" name="s" id="s" size="15" /> |
<input type="text" name="s" id="s" size="15" /> |
||
Line 507: | Line 584: | ||
{| class="widefat" |
{| class="widefat" |
||
|- style="background:#464646; color:#d7d7d7;" |
|- style="background:#464646; color:#d7d7d7;" |
||
− | ! ''' |
+ | ! '''Alphabetical List''' |
|- |
|- |
||
| |
| |
||
* [[#Any_Page_Containing_Posts|comments_open]] |
* [[#Any_Page_Containing_Posts|comments_open]] |
||
* [[#A_Tag_Page|has_tag]] |
* [[#A_Tag_Page|has_tag]] |
||
+ | * [[#A_Taxonomy_Page|has_term]] |
||
* [[#A_Category_Page|in_category]] |
* [[#A_Category_Page|in_category]] |
||
* [[#A_404_Not_Found_Page|is_404]] |
* [[#A_404_Not_Found_Page|is_404]] |
||
Line 519: | Line 597: | ||
* [[#An_Author_Page|is_author]] |
* [[#An_Author_Page|is_author]] |
||
* [[#A_Category_Page|is_category]] |
* [[#A_Category_Page|is_category]] |
||
+ | * [[#A_Child_Theme|is_child_theme]] |
||
* [[#A_Comments_Popup|is_comments_popup]] |
* [[#A_Comments_Popup|is_comments_popup]] |
||
+ | * [[#Is_Previewed_in_the_Customizer|is_customize_preview]] |
||
* [[#A_Date_Page|is_date]] |
* [[#A_Date_Page|is_date]] |
||
* [[#A_Date_Page|is_day]] |
* [[#A_Date_Page|is_day]] |
||
Line 528: | Line 608: | ||
* [[#A Multi-author Site|is_multi_author]] |
* [[#A Multi-author Site|is_multi_author]] |
||
* [[#Part_of_a_network|is_multisite]] |
* [[#Part_of_a_network|is_multisite]] |
||
+ | * [[#Main_Site_.28Multisite.29|is_main_site]] |
||
* [[#A_PAGE_Page|is_page]] |
* [[#A_PAGE_Page|is_page]] |
||
* [[#Is_a_Page_Template|is_page_template]] |
* [[#Is_a_Page_Template|is_page_template]] |
||
* [[#A_Paged_Page|is_paged]] |
* [[#A_Paged_Page|is_paged]] |
||
* [[#A_Preview|is_preview]] |
* [[#A_Preview|is_preview]] |
||
+ | * [[#An_RTL_Language|is_rtl]] |
||
* [[#A_Search_Result_Page|is_search]] |
* [[#A_Search_Result_Page|is_search]] |
||
* [[#A_Single_Post_Page|is_single]] |
* [[#A_Single_Post_Page|is_single]] |
||
Line 554: | Line 636: | ||
[[Category:Advanced Topics]] |
[[Category:Advanced Topics]] |
||
[[Category:Design and Layout]] |
[[Category:Design and Layout]] |
||
+ | |||
+ | == External Resources == |
||
+ | |||
+ | * [http://www.carriedils.com/wordpress-conditional-tags/ What the Weatherman Can Teach You About WordPress Conditional Tags] by Carrie Dils |
||
+ | * Go further with conditional tags: [https://github.com/Screenfeed/SF-Meta-Archives SF Meta Archives (github)] by Greg Viguier |
Languages: English • Français • Italiano • 日本語 Türkçe • Português do Brasil • 中文(简体) • (Add your language)
The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what conditions that page matches. For example, you might want to display a snippet of text above the series of posts, but only on the main page of your blog. With the is_home() Conditional Tag, that task is made easy.
Note the close relation these tags have to WordPress Template Hierarchy.
Warning: You can only use conditional query tags after the posts_selection action hook in WordPress (the wp action hook is the first one through which you can use these conditionals). For themes, this means the conditional tag will never work properly if you are using it in the body of functions.php, i.e. outside of a function.
However: if you have a reference to the query object (for example, from within the parse_query or pre_get_posts hooks), you can use the WP_Query conditional methods (eg: $query->is_search()
)
All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. The conditions under which various tags output TRUE is listed below. Those tags which can accept parameters are so noted.
There is no conditional tag for the blog page. You have to use both is_home() and is_front_page() to detect this page, but those functions can be misused. In fact, a user can define a static page for the homepage, and another page to display the blog. This one will return true with is_home() function, even if it's not the homepage. Here is what a user can define :
When you use is_home() and is_front_page(), you have to use them in the right order to avoid bugs and to test every user configuration:
if ( is_front_page() && is_home() ) { // Default homepage } elseif ( is_front_page() ) { // static homepage } elseif ( is_home() ) { // blog page } else { //everything else }
Attention: The wp-login.php page is not an admin page. To check if this page is displayed, use the admin global variable $pagenow.
Note : To display or not this bar, use show_admin_bar(), this function should be called immediately upon plugins_loaded or placed in the theme's functions.php file.
Note: This function does not distinguish between the post ID, post title, or post name. A post named "17" would be displayed if a post ID of 17 was requested. Presumably the same holds for a post with the slug "17".
To turn on post type archives, use 'has_archive' => true, when registering the post type.
This section refers to WordPress Pages, not any generic web page from your blog, or in other words to the built in post_type 'page'.
See also is_page() for more snippets, such as is_subpage, is_tree.
Note: There is no function to check if a page is a sub-page. We can get around the problem:
if ( is_page() && $post->post_parent > 0 ) { echo "This is a child page"; }
Allows you to determine whether or not you are in a page template or if a specific page template is being used.
Note: if the file is in a subdirectory you must include this as well. Meaning that this should be the filepath in relation to your theme as well as the filename, for example page-templates/about.php.
Note: Be sure to check your spelling when testing: "is" and "in" are significantly different.
See also is_archive() and Category Templates.
See also is_archive() and Tag Templates.
See also is_archive().
See also is_archive() and Author Templates.
See also is_archive().
See also Using Image and File Attachments.
See also the Custom Post Types book.
Example with the filter hook the_content
add_action( 'the_content', 'baw_add_social_buttons' ); function baw_add_social_buttons( $content ) { if ( ! is_admin() && is_main_query() ) { return $content . function_from_a_social_plugin(); } return $content; }
If, when WordPress tries to display the content of each post in the Loop or in a single post page, we are in the main query, and not admin side, we add some social buttons (for example).
Example with the action hook pre_get_posts
add_action( 'pre_get_posts', 'baw_modify_query_exclude_category' ); function baw_modify_query_exclude_category( $query ) { if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) ) { $query->set( 'cat', '-5' ); } }
With pre_get_posts, this is not possible to call directly is_main_query, we should use $query given as a parameter.
See also register_nav_menu().
Note: To display a sidebar's content, use dynamic_sidebar( $sidebar ).
Note : To be effective this function has to run after widgets have initialized, at action 'init' or later, see Action Reference.
Note: The cache will be checked first. If you have a cache plugin, which saves the cache values, then this will work. If you use the default WordPress cache, and the database goes away, then you might have problems.
Example
if ( is_rtl() ) { wp_enqueue_style( 'style-rtl', plugins_url( '/css/style-rtl.css', __FILE__ ) ); wp_enqueue_script( 'script-rtl', plugins_url( '/js/script-rtl.js', __FILE__ ) ); }
Example
$handle = 'fluidVids.js'; $list = 'enqueued'; if ( wp_script_is( $handle, $list ) ) { return; } else { wp_register_script( 'fluidVids.js', plugin_dir_url(__FILE__).'js/fluidvids.min.js'); wp_enqueue_script( 'fluidVids.js' ); }
This would check if the script named 'fluidVids.js' is enqueued. If it is not enqueued, the files are then registered and enqueued.
Here are working samples to demonstrate how to use these conditional tags.
This example shows how to use is_single() to display something specific only when viewing a single post page:
if ( is_single() ) { echo 'This post\'s title is ' . get_the_title(); }
Add this custom function to your child themes functions.php file and modify the conditional tag to suit your needs.
add_action( 'loop_start', 'add_to_single_posts' ); function add_to_single_posts() { if ( is_single('post') ) { echo'<div class="your-class">Your Text or HTML</div>'; } }
You could also use:
add_action( 'loop_start', 'wpsites_add_to_single_posts' ); function wpsites_add_to_single_posts() { if ( is_single() ) { echo'<div class="your-class">Your Text or HTML</div>'; } }
Another example of how to use Conditional Tags in the Loop. Choose to display content or excerpt in index.php when this is a display single post or the home 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 markup ... <?php } ?>
You can use PHP operators to evaluate multiple conditionals in a single if statement.
This is handy if you need to check whether combinations of conditionals evaluate to true or false.
// Check to see if 2 conditionals are met if ( is_single() || is_page() ) ) { // If it's a single post or a single page, do something special } if ( is_archive() && ! is_category( 'nachos' ) ) { // If it's an archive page for any category EXCEPT nachos, do something special }
// Check to see if 3 conditionals are met if ( $query->is_main_query() && is_post_type_archive( 'products' ) && ! is_admin() ) { // If it's the main query on a custom post type archive for Products // And if we're not in the WordPress admin, then do something special } if ( is_post_type_archive( 'movies' ) || is_tax( 'genre' ) || is_tax( 'actor' ) ) { // If it's a custom post type archive for Movies // Or it's a taxonomy archive for Genre // Or it's a taxonomy archive for Actor, do something special }
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_attribute(); ?>"> <?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_categories( '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_categories( '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( 'name' ) . "</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>That's all.</p>"; } // That's all, folks! ?> <form id="searchform" method="get" action="<?php echo esc_url( $_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 -->
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.
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!'; } ?>