(→The Main Page: link to Pages Alternate Methods for Setting the Front Page) |
m (→is_tax) |
||
(265 intermediate revisions by 79 users not shown) | |||
Line 1: | Line 1: | ||
+ | {{Languages| |
||
+ | {{en|Conditional Tags}} |
||
+ | {{fr|Marqueurs_conditionnels}} |
||
+ | {{it|Tag condizionali}} |
||
+ | {{ja|Conditional_Tags}} |
||
+ | {{tr|Kosul Etiketleri}} |
||
+ | {{pt-br|Tags condicionais}} |
||
+ | {{zh-cn|条件标签}} |
||
+ | }} |
||
== Introduction == |
== Introduction == |
||
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 <tt>[[#The Main Page|is_home()]]</tt> Conditional Tag, that task is made easy. |
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 <tt>[[#The Main Page|is_home()]]</tt> Conditional Tag, that task is made easy. |
||
− | Note the close relation these tags have to WordPress |
+ | Note the close relation these tags have to WordPress [[Template Hierarchy]]. |
+ | '''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.''' |
||
− | ==The Conditions For...== |
||
+ | |||
+ | '''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 ... == |
||
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. |
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. |
||
Line 11: | Line 24: | ||
=== The Main Page === |
=== The Main Page === |
||
+ | ; <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]]. |
||
− | ; <tt>is_home()</tt> : When the main blog page is being displayed. (Wordpress 2.1 handles this function differently than prior versions. See [http://codex.wordpress.org/Pages#Alternate_Methods_for_Setting_the_Front_Page Alternate Methods for Setting the Front Page].) |
||
− | |||
− | '''Note:''' If you select a static Page as your frontpage (see below), this tag will be applied to your "posts page". |
||
=== The Front Page === |
=== The Front Page === |
||
− | ; <tt>is_front_page()</tt> : When |
+ | ; <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>is_admin()</tt>: When the Dashboard or the administration panels are being displayed. |
+ | ; <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> |
+ | ; <tt>[[Function_Reference/is_admin_bar_showing|is_admin_bar_showing()]]</tt> : Returns <tt>true</tt> if the admin bar will be displayed. |
− | ; <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('beef-stew')</tt> : When the Post with Post Slug "beef-stew" is being displayed as a single Post. |
||
− | ; <tt>is_single(array(17,'beef-stew','Irish Stew'))</tt> : Returns true when the single post being displayed is either post ID 17, or the ''post_name'' is "beef-stew", or the ''post_title'' is "Irish Stew". Note: the array ability was added at [[Version 2.5]]. |
||
+ | ''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 Sticky Post === |
||
− | ; <tt>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. Note: this tag was added at [[Version 2.7]]. |
||
− | ; <tt>is_sticky('17')</tt> : Returns true when Post 17 is considered a sticky post. |
||
− | === |
+ | === A Single Post Page === |
+ | ; <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>comments_open()</tt>: When comments are allowed for the current Post being processed in the [[The Loop|WordPress Loop]]. |
||
− | ; <tt> |
+ | ; <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( 'beef-stew' )</tt> : When the Post with Post Slug "beef-stew" is being displayed as a single Post. |
||
+ | ; <tt>is_single( array( 17, 'beef-stew', 'Irish Stew' ) )</tt> : Returns true when the single post being displayed is either post ID 17, or the ''post_name'' is "beef-stew", or the ''post_title'' is "Irish Stew". |
||
+ | ; <tt>is_single( array( 17, 19, 1, 11 ) )</tt> : Returns true when the single post being displayed is either ''post ID = '' 17, ''post ID = '' 19, ''post ID = '' 1 or ''post ID = '' 11. |
||
+ | ; <tt>is_single( array( 'beef-stew', 'pea-soup', 'chili' ) )</tt> : Returns true when the single post being displayed is either the ''post_name'' "beef-stew", ''post_name'' "pea-soup" or ''post_name'' "chili". |
||
+ | ; <tt>is_single( array( 'Beef Stew', 'Pea Soup', 'Chili' ) )</tt> : Returns true when the single post being displayed is either the ''post_title'' is "Beef Stew", ''post_title'' is "Pea Soup" or ''post_title'' is "Chili". |
||
+ | 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". |
||
− | === A PAGE Page === |
||
+ | === A Sticky Post === |
||
− | This section refers to WordPress [[Pages]], not any generic webpage from your blog. |
||
+ | ; <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. |
||
+ | === A Post Type is Hierarchical === |
||
− | ; <tt>is_page()</tt> : When any [[Pages|Page]] is being displayed. |
||
+ | ; <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_page('42')</tt> : When [[Pages|Page]] 42 (ID) is being displayed. |
||
+ | ; <tt>is_post_type_hierarchical( 'book' )</tt> : Returns true if the book post type was registered as having support for hierarchical. |
||
− | ; <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')</tt> : When the [[Pages|Page]] with a ''post_name'' (slug) of "about-me" is being displayed. |
||
− | ; <tt>is_page(array(42,'about-me','About Me And Joe'))</tt> : Returns true when the [[Pages|Pages]] displayed is either post ID 42, or ''post_name'' "about-me", or ''post_title'' "About Me And Joe". Note: the array ability was added at [[Version 2.5]]. |
||
− | + | === 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 [[Function_Reference/register_post_type|registering the post type]]. |
||
+ | === A Comments Popup === |
||
− | There is no <tt>is_subpage()</tt> function yet, but you can test this with a little code: |
||
+ | ; <tt>[[Function_Reference/is_comments_popup|is_comments_popup()]]</tt> : When in Comments Popup window. |
||
+ | === Any Page Containing Posts === |
||
− | <pre><?php |
||
− | // Get $post if you're inside a function |
||
− | global $post; |
||
+ | ; <tt>[[Function_Reference/comments_open|comments_open()]]</tt>: When comments are allowed for the current Post being processed in the [[The Loop|WordPress Loop]]. |
||
− | if (is_page() && $post->post_parent ) { |
||
+ | ; <tt>[[Function_Reference/pings_open|pings_open()]]</tt>: When pings are allowed for the current Post being processed in the [[The Loop|WordPress Loop]]. |
||
− | // This is a subpage |
||
− | } else { |
||
− | // This is not a subpage |
||
− | } |
||
− | ?></pre> |
||
+ | === A PAGE Page === |
||
− | If you need to test whether this is a particular page OR a child of that page (e.g. to present a different banner on different sections of a page-based site), get the parent page IDs from the back-end, then use code like this: |
||
+ | 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'. |
||
− | <pre> |
||
− | <?php |
||
+ | ; <tt>[[Function_Reference/is_page|is_page()]]</tt> : When any [[Pages|Page]] is being displayed. |
||
− | if (is_page(about) || $post->post_parent=="2") { |
||
+ | ; <tt>is_page( 42 )</tt> : When [[Pages|Page]] 42 (ID) is being displayed. |
||
− | $bannerimg="home.jpg"; |
||
+ | ; <tt>is_page( 'About Me And Joe' )</tt> : When the [[Pages|Page]] with a ''post_title'' of "About Me And Joe" is being displayed. |
||
− | } elseif (is_page(learning) || $post->post_parent=="56") { |
||
+ | ; <tt>is_page( 'about-me' )</tt> : When the [[Pages|Page]] with a ''post_name'' (slug) of "about-me" is being displayed. |
||
− | $bannerimg="teaching.jpg"; |
||
+ | ; <tt>is_page( array( 42, 'about-me', 'About Me And Joe' ) )</tt> : Returns true when the [[Pages|Pages]] displayed is either ''post ID'' = 42, or ''post_name'' is "about-me", or ''post_title'' is "About Me And Joe". |
||
− | } elseif (is_page(admissions) || $post->post_parent=="15") { |
||
+ | ; <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. |
||
− | $bannerimg="admissions.jpg"; |
||
− | } else { |
||
− | $bannerimg="home.jpg" ; // Fall-through |
||
− | } |
||
+ | See also [[Function_Reference/is_page|is_page()]] for more snippets, such as <tt>is_subpage</tt>, <tt>is_tree</tt>. |
||
− | ?> |
||
− | </pre> |
||
+ | ''Note'': There is no function to check if a page is a sub-page. We can get around the problem: |
||
− | ====Is a Page Template==== |
||
− | Beginning with [[Version 2.5]] this allows you to determine whether or not you are in a page template or if a specific page template is being used. |
||
+ | <pre>if ( is_page() && $post->post_parent > 0 ) { |
||
− | ; <tt>is_page_template()</tt> : Is a [[Pages#Page_Templates|Page Template]] being used? |
||
+ | echo "This is a child page"; |
||
− | ; <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. |
||
+ | }</pre> |
||
+ | |||
+ | === 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. |
||
+ | ; <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>. |
||
=== A Category Page === |
=== A Category Page === |
||
− | ; <tt>is_category()</tt> : When |
+ | ; <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]]). |
||
− | '''Note:''' Be sure to check your spelling when testing, "is" and "in" are a big difference. |
||
+ | ; <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. |
||
+ | '''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 103: | Line 141: | ||
=== A Tag Page === |
=== A Tag Page === |
||
− | ; <tt>is_tag()</tt> : When any Tag archive page is being displayed. |
+ | ; <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. |
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 (and related) === |
||
+ | |||
+ | ==== is_tax ==== |
||
+ | ; <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', '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". |
||
+ | |||
+ | ==== has_term ==== |
||
+ | ; <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 ==== |
||
+ | ; <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()]]. |
||
+ | |||
+ | ==== 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>[[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( 'Vivian' )</tt> : When the archive page for the Author with Nickname "Vivian" is being displayed. |
||
+ | ; <tt>is_author( 'john-jones' )</tt> : When the archive page for the Author with Nicename "john-jones" is being displayed. |
||
+ | ; <tt>is_author( array( 4, 'john-jones', 'Vivian' ) )</tt> : When the archive page for the author is either user ID 4, or ''user_nicename'' "john-jones", or ''nickname'' "Vivian". |
||
− | ; <tt>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('Vivian')</tt> : When the archive page for the Author with Nickname "Vivian" is being displayed. |
||
− | ; <tt>is_author('john-jones')</tt> : When the archive page for the Author with Nicename "john-jones" is being displayed. |
||
− | ; <tt>is_author(array(4,'john-jones','Vivian'))</tt> : When the archive page for the author is either user ID 4, or ''user_nicename'' "john-jones", or ''nickname'' "Vivian". Note: the array ability was added at [[Version 2.5]]. |
||
See also <tt>[[#Any Archive Page|is_archive()]]</tt> and [[Author Templates]]. |
See also <tt>[[#Any Archive Page|is_archive()]]</tt> and [[Author Templates]]. |
||
+ | |||
+ | === A Multi-author Site === |
||
+ | ; <tt>is_multi_author( )</tt> : When more than one author has published posts for a site. Available with [[Version 3.2]]. |
||
=== A Date Page === |
=== A Date Page === |
||
+ | ; <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>[[Function_Reference/is_year|is_year()]]</tt> : When a yearly archive is being displayed. |
||
+ | ; <tt>[[Function_Reference/is_month|is_month()]]</tt> : When a monthly archive is being displayed. |
||
+ | ; <tt>[[Function_Reference/is_day|is_day()]]</tt> : When a daily archive is being displayed. |
||
+ | ; <tt>[[Function_Reference/is_time|is_time()]]</tt> : When an hourly, "minutely", or "secondly" archive is being displayed. |
||
+ | ; <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>is_date()</tt> : When ''any'' date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive). |
||
− | ; <tt>is_year()</tt> : When a yearly archive is being displayed. |
||
− | ; <tt>is_month()</tt> : When a monthly archive is being displayed. |
||
− | ; <tt>is_day()</tt> : When a daily archive is being displayed. |
||
− | ; <tt>is_time()</tt> : When an hourly, "minutely", or "secondly" archive is being displayed. |
||
See also <tt>[[#Any Archive Page|is_archive()]]</tt>. |
See also <tt>[[#Any Archive Page|is_archive()]]</tt>. |
||
=== Any Archive Page === |
=== Any Archive Page === |
||
− | ; <tt>is_archive()</tt> : When ''any'' type of Archive page is being displayed. Category, Tag, Author and Date |
+ | ; <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>is_search()</tt> : When a search result page archive is being displayed. |
+ | ; <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>is_404()</tt> : When a page displays after an "HTTP 404: Not Found" error occurs. |
+ | ; <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>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. 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]]. |
+ | ; <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>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. |
+ | ; <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 === |
||
+ | ; <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. |
||
+ | |||
+ | === A Local Attachment === |
||
+ | |||
+ | ; <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>is_feed()</tt> : When the site requested is a [[Introduction_to_Blogging#Syndication|Syndication]]. |
+ | ; <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>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. |
+ | ; <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>is_preview()</tt> : When a single post being displayed is viewed in Draft mode. |
+ | ; <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>[[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. |
||
+ | === Has A Nav Menu Assigned === |
||
− | ; <tt>!empty($post->post_excerpt)</tt> : There is no built-in way to check if a post has an excerpt, but you can use the above code in the same way. Remove the '!' to emulate 'has_no_excerpt'. |
||
+ | ; <tt>[[Function_Reference/has_nav_menu|has_nav_menu()]]</tt> : Returns <tt>true</tt> when a registered nav menu location has a menu assigned. |
||
+ | |||
+ | See also [[Function_Reference/register_nav_menu|register_nav_menu()]]. |
||
+ | |||
+ | === 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 === |
||
+ | ; <tt>[[Function_Reference/is_dynamic_sidebar|is_dynamic_sidebar()]]</tt> : Returns <tt>true</tt> if the theme supports dynamic sidebars. |
||
+ | |||
+ | === Is Sidebar Active === |
||
+ | ; <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 )]]. |
||
+ | |||
+ | === Is Widget Active === |
||
+ | ; <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. |
||
+ | |||
+ | ''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]]. |
||
+ | |||
+ | === Is Blog Installed === |
||
+ | ; <tt>[[Function_Reference/is_blog_installed|is_blog_installed()]]</tt> : Returns <tt>true</tt> if the current is properly installed. |
||
+ | |||
+ | ''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. |
||
+ | |||
+ | === 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) === |
||
+ | ; <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) === |
||
+ | ; <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 === |
||
+ | ; <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 === |
||
+ | ; <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 173: | 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 post\'s title is ' . get_the_title(); |
|
+ | |||
+ | } |
||
+ | </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: |
||
+ | |||
+ | <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> |
||
+ | if ( is_home() || is_single() ) { |
||
+ | |||
+ | the_content(); |
||
+ | |||
+ | } |
||
+ | else { |
||
+ | |||
+ | the_excerpt(); |
||
+ | |||
+ | } |
||
+ | </pre> |
||
+ | |||
+ | When you need display a code or element, in a place that is NOT the home page. |
||
+ | |||
+ | <pre> |
||
+ | <?php if ( ! is_home() ) {?> |
||
+ | |||
+ | Insert your markup ... |
||
+ | |||
+ | <?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 189: | 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 195: | Line 491: | ||
<?php |
<?php |
||
// are we showing a date-based archive? |
// are we showing a date-based archive? |
||
− | if (is_date()) |
+ | if ( is_date() ) { |
+ | if ( date( 'Y' ) != get_the_date( 'Y' ) ) { |
||
− | { |
||
− | if (date('Y') != get_the_date('Y')) |
||
− | { |
||
// this post was written in a previous year |
// this post was written in a previous year |
||
// so let's style the content using the "oldentry" class |
// so let's style the content using the "oldentry" class |
||
Line 208: | Line 502: | ||
echo '<div class="entry">'; |
echo '<div class="entry">'; |
||
} |
} |
||
+ | |||
− | the_content('Read the rest of this entry »'); |
||
+ | the_content( 'Read the rest of this entry »' ); |
||
+ | |||
?> |
?> |
||
</div> |
</div> |
||
Line 222: | Line 518: | ||
<?php |
<?php |
||
// let's generate info appropriate to the page being displayed |
// let's generate info appropriate to the page being displayed |
||
− | if (is_home()) { |
+ | 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()) { |
+ | } 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()) { |
+ | } elseif ( is_single() ) { |
− | + | // we're looking at a single page, so let's not show anything in the sidebar |
|
− | } elseif (is_page()) { |
+ | } 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 { |
} else { |
||
− | + | // catch-all for everything else (archives, searches, 404s, etc) |
|
− | + | 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" /> |
||
− | <input type="submit" value="<?php _e('Search'); ?>" /> |
+ | <input type="submit" value="<?php _e( 'Search' ); ?>" /> |
</div> |
</div> |
||
</form> |
</form> |
||
Line 261: | Line 557: | ||
</pre> |
</pre> |
||
− | ===Helpful 404 |
+ | ===Helpful 404 Page=== |
+ | The [[Creating an Error 404 Page]] article has an example of using the PHP conditional function %%%isset()%%% in the [[Creating an Error 404 Page#Writing Friendly Messages|Writing Friendly Messages]] section. |
||
− | When a visitor gets a 404 error page, it can be intimidating, and unhelpful. Using Wordpress, you can take the edge off a 404 and make it helpful to users, and yourself, too, by emailing whenever the user clicks a link to a non-existent page. If you use this, don't forget o |
||
+ | |||
+ | ===Dynamic Menu Highlighting=== |
||
+ | |||
+ | The [[Dynamic Menu Highlighting]] article discusses how to use the conditional tags to enable highlighting of the current page in a menu. |
||
+ | |||
+ | ===In a theme's footer.php file=== |
||
+ | 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: |
||
<pre> |
<pre> |
||
− | <p>You |
||
<?php |
<?php |
||
+ | wp_reset_query(); |
||
− | #some variables for the script to use |
||
+ | if ( is_page( '2' ) ) { |
||
− | #if you have some reason to change these, do. but wordpress can handle it |
||
+ | echo 'This is page 2!'; |
||
− | $adminemail = get_bloginfo('admin_email'); #the administrator email address, according to wordpress |
||
+ | } |
||
− | $website = get_bloginfo('url'); #gets your blog's url from wordpress |
||
+ | ?> |
||
− | $websitename = get_bloginfo('name'); #sets the blog's name, according to wordpress |
||
− | |||
− | if (!isset($_SERVER['HTTP_REFERER'])) { |
||
− | #politely blames the user for all the problems they caused |
||
− | echo "tried going to "; #starts assembling an output paragraph |
||
− | $casemessage = "All is not lost!"; |
||
− | } elseif (isset($_SERVER['HTTP_REFERER'])) { |
||
− | #this will help the user find what they want, and email me of a bad link |
||
− | echo "clicked a link to"; #now the message says You clicked a link to... |
||
− | #setup a message to be sent to me |
||
− | $failuremess = "A user tried to go to $website" |
||
− | .$_SERVER['REQUEST_URI']." and received a 404 (page not found) error. "; |
||
− | $failuremess .= "It wasn't their fault, so try fixing it. |
||
− | They came from ".$_SERVER['HTTP_REFERER']; |
||
− | mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'], |
||
− | $failuremess, "From: $websitename <noreply@$website>"); #email you about problem |
||
− | $casemessage = "An administrator has been emailed |
||
− | about this problem, too.";#set a friendly message |
||
− | } |
||
− | echo " ".$website.$_SERVER['REQUEST_URI']; ?> |
||
− | and it doesn't exist. <?php echo $casemessage; ?> You can click back |
||
− | and try again or search for what you're looking for: |
||
− | <?php include(TEMPLATEPATH . "/searchform.php"); ?> |
||
− | </p> |
||
</pre> |
</pre> |
||
+ | == Conditional Tags Index == |
||
− | ===Dynamic Menu Highlighting=== |
||
+ | {| cellspacing="10" width="100%" |
||
− | The [[Dynamic Menu Highlighting]] article discusses how to use the conditional tags to enable highlighting of the current page in a menu. |
||
+ | |- style="vertical-align:top;" |
||
+ | | style="width:100%" | |
||
+ | {| class="widefat" |
||
− | ==External Resources== |
||
+ | |- style="background:#464646; color:#d7d7d7;" |
||
− | * French translation : [[fr:Marqueurs_conditionnels]] |
||
+ | ! '''Alphabetical List''' |
||
+ | |- |
||
+ | | |
||
+ | * [[#Any_Page_Containing_Posts|comments_open]] |
||
+ | * [[#A_Tag_Page|has_tag]] |
||
+ | * [[#A_Taxonomy_Page|has_term]] |
||
+ | * [[#A_Category_Page|in_category]] |
||
+ | * [[#A_404_Not_Found_Page|is_404]] |
||
+ | * [[#The_Administration_Panels|is_admin]] |
||
+ | * [[#Any_Archive_Page|is_archive]] |
||
+ | * [[#An_Attachment|is_attachment]] |
||
+ | * [[#An_Author_Page|is_author]] |
||
+ | * [[#A_Category_Page|is_category]] |
||
+ | * [[#A_Child_Theme|is_child_theme]] |
||
+ | * [[#A_Comments_Popup|is_comments_popup]] |
||
+ | * [[#Is_Previewed_in_the_Customizer|is_customize_preview]] |
||
+ | * [[#A_Date_Page|is_date]] |
||
+ | * [[#A_Date_Page|is_day]] |
||
+ | * [[#A_Syndication|is_feed]] |
||
+ | * [[#The_Front_Page|is_front_page]] |
||
+ | * [[#The_Main_Page|is_home]] |
||
+ | * [[#A_Date_Page|is_month]] |
||
+ | * [[#A Multi-author Site|is_multi_author]] |
||
+ | * [[#Part_of_a_network|is_multisite]] |
||
+ | * [[#Main_Site_.28Multisite.29|is_main_site]] |
||
+ | * [[#A_PAGE_Page|is_page]] |
||
+ | * [[#Is_a_Page_Template|is_page_template]] |
||
+ | * [[#A_Paged_Page|is_paged]] |
||
+ | * [[#A_Preview|is_preview]] |
||
+ | * [[#An_RTL_Language|is_rtl]] |
||
+ | * [[#A_Search_Result_Page|is_search]] |
||
+ | * [[#A_Single_Post_Page|is_single]] |
||
+ | * [[#A_Single_Page,_Single_Post_or_Attachment|is_singular]] |
||
+ | * [[#A_Sticky_Post|is_sticky]] |
||
+ | * [[#Admin_of_a_network|is_super_admin]] |
||
+ | * [[#A_Tag_Page|is_tag]] |
||
+ | * [[#A_Taxonomy_Page|is_tax]] |
||
+ | * [[#A_Date_Page|is_time]] |
||
+ | * [[#A_Trackback|is_trackback]] |
||
+ | * [[#A_Date_Page|is_year]] |
||
+ | * [[#Any_Page_Containing_Posts|pings_open]] |
||
+ | |} |
||
+ | ==Function Reference== |
||
+ | {{Conditional Tags}} |
||
+ | |||
+ | {{Tag Footer}} |
||
− | {{No Param Tag Footer}} |
||
[[Category:Conditional Tags|*]] |
[[Category:Conditional Tags|*]] |
||
− | [[Category: |
+ | [[Category:Functions]] |
[[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!'; } ?>