- WordPress 2.7 Cookbook
- Jean-Baptiste Jung
- 758字
- 2021-04-01 13:55:50
Using conditional tags to display content on specific pages
Sometimes, you may want to display some content only on specific pages or sections. For example, you may wish to display a welcome message on your blog homepage or show specific information on the Categories page. In this recipe, Let's learn how to do it with the help of WordPress conditional tags.
How to do it...
- WordPress conditional tags are Boolean variables so you have to use them as a condition on a
php
if
statement as shown in the following example:<?php if(is_page()) { echo "Page title:"; the_title; } ?>
- The preceding
if
statement will returnfalse
if the current page template isn't a WordPress page template so nothing would happen. Otherwise, theif
statement will returntrue
and will print the page title on screen. - Conditional tags can be used in any theme files.
WordPress features a large amount of conditional tags for all needs. They all work as described above. Following are the conditional tags:
is_home()
: It returns true if the current page is the blog homepageis_front_page()
: It returns true if the current page is the blog front pageis_single()
: It returns true if the current page is a single post templateis_page()
: It returns true if the current page is a page templateis_page_template("about.php")
: It returns true if a page template is currently being usedis_category()
: It returns true if the current page is a category templatein_category('4')
: It returns true if the current posts belongs to the specified categoryis_tag()
: It returns true if the current page is a tag templatehas_tag("wordpress")
: It returns true if the post have the tag specified in parameteris_author()
: It returns true if the current page is an author archiveis_date()
: It returns true if the post or page is a date-based archiveis_year()
: It returns true if it's a yearly archiveis_month()
: It returns true if it's a monthly archiveis_day()
: It returns true if it's a daily archiveis_time()
: It returns true if an hourly, minutely, or secondly archive is being displayedis_archive()
: It returns true if the current page display any type of archives (time, author, tag, and so on)is_search()
: It returns true if the current page displays search resultsis_paged()
: It returns true if the current page is pagedis_404()
: It returns true if the current page is a 404is_sticky()
: It returns true if the Stick this post to the front page check box has been checked for the current posthas_tag("WordPress")
: It return true if the current posted has been tagged with WordPressis_admin()
: It returns true if the dashboard or an admin page is currently displayedcomments_open()
: It returns true if commenting is allowed on the postpings_open()
: It returns true if pinging is allowed on the postis_preview()
: It returns true if the post or page is displayed in preview mode
How it works...
Unless specified, the above tags doesn't take any parameter. They have to be used in a PHP conditional statement.
<?php if (is_404()) "{ //Do something only if the post or page is a 404 error. } ?>
On the other hand, some conditional tags request a parameter to work. For example, to use the is_year()
tag, you have to provide a year in parameter.
<?php if (is_year("2007")) { //Do something only if the post or page was published during the year 2007. } ?>
There's more...
Conditional tags, as we have seen, are very useful. But there's more—some conditional tags accept optional arguments that allow you to create a more specific condition. For example, you should create a code snippet that will only be executed if the current page is a WordPress template.
Some conditional tags can accept up to 4 different types of parameters:
- ID: The ID parameter can be used in
is_category
,is_page()
,is_tag()
,is_single()
,is_author()
, andis_sticky()
.<?php if (is_category(5)) { }?>
As a result, the preceding code returns true if the category ID is
5
. - Name: The name parameter can be used in is_category, is_page(), is_single() and is_author().
<?php if (is_category("Blogging Tips")) { }?>
As a result, the preceding code returns true if the category name is
"Blogging Tips"
- Slug: The slug parameter can be used in
is_category
,is_page()
,is_tag()
,is_single()
, andis_author()
.<?php if (is_category("blogging-tips")) { }?>
As a result, the preceding code returns true if the category slug is
"blogging-tips"
. - Array: The array parameter can be used in is_category, is_page(), is_tag(), has_tag(), is_single() and is_author().
<?php if(is_category(array(5,'blogging-tips','Blogging Tips'))) { } ?>
As a result, the preceding code returns true if the category of posts being displayed either has the ID as 5, slug as
"blogging-tips"
, or name as"Blogging Tips"
.
- Irrlicht 1.7 Realtime 3D Engine Beginner's Guide
- 穿越Photoshop CC
- Photoshop CS6完全自學案例教程(微課版)
- DWR Java AJAX Applications
- Moodle JavaScript Cookbook
- After Effects CC 2018影視特效與合成案例教程
- 中文版3ds Max 2016實用教程
- WordPress Theme Design
- Implementing SugarCRM 5.x
- Apache Solr 3.1 Cookbook
- PHP and MongoDB Web Development Beginner's Guide
- 社會調查數據管理:基于Stata 14管理CGSS數據
- 夢幻森林Procreate童話風插畫繪制專業技法
- Oracle ADF Enterprise Application Development—Made Simple
- SolidEdge ST10中文版標準教程