官术网_书友最值得收藏!

  • 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.

Getting ready

To achieve this recipe, you'll need a WordPress theme that you can edit.

How to do it...

  1. WordPress conditional tags are Boolean variables so you have to use them as a condition on a phpif statement as shown in the following example:
    <?php if(is_page())
    {
        echo "Page title:";
        the_title;
    } ?>
  2. The preceding if statement will return false if the current page template isn't a WordPress page template so nothing would happen. Otherwise, the if statement will return true and will print the page title on screen.
  3. 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 homepage
  • is_front_page() : It returns true if the current page is the blog front page
  • is_single() : It returns true if the current page is a single post template
  • is_page(): It returns true if the current page is a page template
  • is_page_template("about.php"): It returns true if a page template is currently being used
  • is_category(): It returns true if the current page is a category template
  • in_category('4'): It returns true if the current posts belongs to the specified category
  • is_tag(): It returns true if the current page is a tag template
  • has_tag("wordpress"): It returns true if the post have the tag specified in parameter
  • is_author(): It returns true if the current page is an author archive
  • is_date(): It returns true if the post or page is a date-based archive
  • is_year(): It returns true if it's a yearly archive
  • is_month(): It returns true if it's a monthly archive
  • is_day(): It returns true if it's a daily archive
  • is_time(): It returns true if an hourly, minutely, or secondly archive is being displayed
  • is_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 results
  • is_paged(): It returns true if the current page is paged
  • is_404(): It returns true if the current page is a 404
  • is_sticky(): It returns true if the Stick this post to the front page check box has been checked for the current post
  • has_tag("WordPress"): It return true if the current posted has been tagged with WordPress
  • is_admin(): It returns true if the dashboard or an admin page is currently displayed
  • comments_open(): It returns true if commenting is allowed on the post
  • pings_open(): It returns true if pinging is allowed on the post
  • is_preview(): It returns true if the post or page is displayed in preview mode

    Note

    The is_home() function has been deprecated and replaced by the is_front_page() function. To ensure backward compatibility with older WordPress versions, you should use the following piece of code:

    <?php if (is_front_page() || is_home()) 
       {
          //Do something
       } ?>
    

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(), and is_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(), and is_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".

主站蜘蛛池模板: 视频| 屯留县| 图们市| 和田市| 邢台市| 广平县| 澳门| 巫山县| 两当县| 永年县| 佛教| 金昌市| 邛崃市| 南靖县| 辽阳县| 安溪县| 吉隆县| 平江县| 大连市| 昭平县| 泰顺县| 崇文区| 贺兰县| 南靖县| 江川县| 南昌市| 香港| 衡水市| 夹江县| 丹凤县| 富川| 诏安县| 庆元县| 基隆市| 灌云县| 南昌市| 龙游县| 海阳市| 上犹县| 九江县| 安庆市|