In this series, we're going through one of the fundamental features of WordPress: Conditional Tags. In this fourth part, we'll continue introducing and reviewing the Conditional Tags. Be sure to check out the previous parts if you haven't yet.
Let's begin!
is_month()
In WordPress, the most commonly used date-based archive type is probably the monthly archives. When you need to detect whether your code is running in a monthly archive page, the Conditional Tag is_month()
will help you.
This Conditional Tag doesn't accept any parameters.
current_theme_supports()
While developing plugins, you might need to check whether the active theme allows thumbnails, post formats, custom headers or even widgets. With the help of current_theme_supports()
, you can check whether the theme in use supports the feature you specify.
This Conditional Tag has only one parameter:
$feature
(string, optional): Name of the features. (Default: None)Feature names:
current_theme_supports()
Let's say you're developing a plugin specifically for WordPress themes with the "custom backgrounds" feature. Your plugin will be useless for themes which don't support custom backgrounds, so you need to display an error message in the administration panel to inform the user. Here's what you do:
<?php add_action( 'admin_notices', 'custom_bg_feature_warning' ); function custom_bg_feature_warning() { if ( ! current_theme_supports( 'custom-background' ) ) { echo '<div class="error"> <p>' . __( 'This plugin requires a theme with the "custom backgrounds" feature enabled.', 'translation-domain' ) . '</p> </div>'; } } ?>
is_plugin_active()
Working in the administration panel only, the Conditional Tag is_plugin_active()
helps your code know whether the given plugin is active. This Conditional Tag can be useful if you're writing "addon plugins" for popular plugins (like WooCommerce) or base frameworks installed as plugins (like the Redux Framework).
This Conditional Tag has only one parameter:
$plugin
(string, required): Plugin's or sub-directory's name. (Default: None)is_local_attachment()
WordPress attachments are a blessing when used properly, but how do you find out whether a URL is actually an attachment? If you're working with URLs while developing, the Conditional Tag is_local_attachment()
might be very useful for you: It checks whether the given URL is uploaded to the same WordPress installation as an attachment.
This Conditional Tag has only one parameter:
$url
(string, required): URL to check. (Default: None)is_local_attachment()
Since this conditional tag is pretty straightforward, the example will be straightforward, too. Imagine you're developing a plugin and you need URLs (generated by another function you created) to be checked to see whether they're local attachments or not:
<?php $url = some_url_generator_function(); if ( is_local_attachment( $url ) ) { _e( 'Yep, this is a local attachment!', 'translation-domain' ); } else { _e( 'Nope, this is not a local attachment.', 'translation-domain' ); } ?>
is_time()
Did you know that WordPress has hour-based, minute-based and even second-based archives? (To be honest, I didn't know that you could create these kinds of archives before writing this series. Writing at Tuts+ can be an educational journey, too!)
It doesn't come out of the box, which means you can't type in mywebsite.com/2014/02/22/18/30/
and get archives for 18:30 on February 22, 2014; but you can create one with the help of the WP_Query
class. Anyway: If you want to detect these kinds of time-based archives, you can use the is_time()
Conditional Tag.
This Conditional Tag doesn't accept any parameters.
is_rtl()
In some languages, mostly in the countries of the Middle East, text is written from right to left instead of left to right. As a WordPress developer, you must consider all WordPress users, and if you want your theme or plugin to work nicely with WordPress websites with RTL text, you should use is_rtl()
to determine whether the locale is RTL.
This Conditional Tag doesn't accept any parameters.
is_rtl()
Most web designers create separate stylesheets for RTL locales—it's a common practice. And if you want to load your RTL stylesheet conditionally, here's what you do:
<?php add_action( 'wp_enqueue_scripts', 'mytheme_enqueue_styles' ); function mytheme_enqueue_styles() { if ( is_rtl() ) { wp_enqueue_style( 'style-rtl', plugins_url( '/css/style-rtl.css', __FILE__ ) ); } } ?>
is_tax()
The Conditional Tag is_tag()
can detect tag archives, but it can't work with custom taxonomies. To determine whether a taxonomy archive page is being displayed, you can use is_tax()
. By default, it includes all taxonomy archive pages, including regular tags. But it has two parameters, so you can target specific taxonomy archive pages, or even specific terms.
This Conditional Tag has only one parameter:
$taxonomy
(array/string, optional): Taxonomy slug or an array of slugs. (Default: None)$term
(array/string/integer, optional): Term's ID, name, slug or an array of those. (Default: None)is_attachment()
While creating a plugin (or theme), you might want to know when an attachment page is being displayed. The Conditional Tag is_attachment()
is the one that will help you: It returns TRUE
in an attachment page and FALSE
anywhere else.
This Conditional Tag doesn't accept any parameters.
is_attachment()
Let's say you want to display a "Back to the post" link above the attachment. Here's what you do:
<?php if ( is_attachment() ) { // Get attachment's parent. $parent = get_post_field( 'post_parent', $id ); // Get permalink of the parent post. $parent_link = get_permalink( $parent ); // Echo the link. echo '<a href="' . $parent_link . '" class="parent-post-link">' . __( 'Back to the post', 'translation-domain' ) . '</a>'; } ?>
term_exists()
This is the second Conditional Tag that doesn't return TRUE
and FALSE
, but more useful information: The Conditional Tag term_exists()
checks whether a term of a taxonomy exists, and returns the term ID (if no taxonomy is specified) or an array of the taxonomy and term pairing instead of TRUE
. If the specified term doesn't exist, it returns 0
or NULL
.
This Conditional Tag has three parameters:
$term
(string/integer, required): Term's name or ID. (Default: None)$taxonomy
(string, optional): Taxonomy's name. (Default: Empty)$parent
(integer, optional): Parent term's ID (to confine the search under it). (Default: 0)has_term()
We saw that we could use has_tag()
if we wanted to find a tag in a post. What if we wanted to do the same thing, but with a term of a custom taxonomy? Easy: The Conditional Tag has_term()
has a third parameter (second in order) that allows us to specify a custom taxonomy.
Fun fact: Even the Conditional Tag has_tag()
uses has_term()
to return TRUE
or FALSE
! And as with the has_tag()
function, has_term()
also allows you to specify more than one term (but in only one taxonomy).
This Conditional Tag has three parameters:
$term
(array/string/integer, optional): Term's name, ID, slug, or an array of those. (Default: Empty)$taxonomy
(string, required): Taxonomy's name. (Default: Empty)$post
(object/integer, optional): Post to check. (Default: NULL
)has_term()
Let's say you run an e-commerce website and you're going to change your supplier for Dell laptops, so you want to inform your users that shipments will be delayed until next week. Here's what you do:
<?php if ( has_term( 'dell', 'laptops' ) ) { echo '<div class="above-product-warning"><strong>' . __( 'Warning', 'translation-domain' ) . ':</strong> ' . __( 'We\'re switching suppliers for Dell laptops, so the shipments will be delayed until next week. Sorry about that!', 'translation-domain' ) . '</div>'; } ?>
is_trackback()
Greetings, old-timer! You want to find out if a comment is a "trackback"? The is_trackback()
Conditional Tag will help you with that!
This Conditional Tag doesn't accept any parameters.
is_multisite()
While being a not-so-popular feature of WordPress, Multisite comes in very handy when necessary. If you want your code to run in normal WordPress installations and behave differently in Multisite installations, the Conditional Tag is_multisite()
can help you detect whether Multisite support is enabled.
This Conditional Tag doesn't accept any parameters.
is_multisite()
Let's say you're developing a plugin, and if Multisite is enabled, the plugin requires another class from your plugin. Here's what you do:
<?php if ( is_multisite() ) { require_once( 'path/to/multisite-class.php' ); } ?>
is_post_type_archive()
Sometimes, you might want your code to know whether an archive page for a custom post type (or a couple of custom post types, or any custom post type) is being displayed. The Conditional Tag is_post_type_archive()
does exactly that: It checks whether the query is for an archive page for a custom post type.
This Conditional Tag has only one parameter:
$post_types
(array/string, optional): Name of the post type, or an array of post type names. (Default: None)In this part, we reviewed another batch of the 65 documented Conditional Tags in WordPress. In the next parts, we're going to go through the remaining 26. If you have any questions or comments, shoot them below—and if you liked this article, don't forget to share it!
See you in the next part!
Create Modern Vue Apps Using Create-Vue and Vite
/Pros and Cons of Using WordPress
/How to Fix the “There Has Been a Critical Error in Your Website” Error in WordPress
/How To Fix The “There Has Been A Critical Error in Your Website” Error in WordPress
/How to Create a Privacy Policy Page in WordPress
/How Long Does it Take to Learn JavaScript
/The Best Way to Deep Copy an Object in JavaScript
/Adding and Removing Elements From Arrays in JavaScript
/Create a JavaScript AJAX Post Request: With and Without jQuery
/How to Enable or Disable a Button With JavaScript: jQuery vs. Vanilla
/How to Enable or Disable a Button With JavaScript: jQuery vs Vanilla
/Confirm Yes or No With JavaScript
/How to Change the URL in JavaScript: Redirecting
/15+ Best WordPress Twitter Widgets
/27 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/21 Best Tab and Accordion Widget Plugins for WordPress (Free & Premium)
/30 HTML Best Practices for Beginners
/31 Best WordPress Calendar Plugins and Widgets (With 5 Free Plugins)
/25 Ridiculously Impressive HTML5 Canvas Experiments
/How to Create a Simple Web-Based Chat Application
/30 Popular WordPress User Interface Elements
/Top 18 Best Practices for Writing Super Readable Code
/Best Affiliate WooCommerce Plugins Compared
/18 Best WordPress Star Rating Plugins
/10+ Best WordPress Twitter Widgets
/20+ Best WordPress Booking and Reservation Plugins
/Working With Tables in React: Part Two
/Best CSS Animations and Effects on CodeCanyon
/30 CSS Best Practices for Beginners
/How to Create a Custom WordPress Plugin From Scratch
/10 Best Responsive HTML5 Sliders for Images and Text… and 3 Free Options
/16 Best Tab and Accordion Widget Plugins for WordPress
/18 Best WordPress Membership Plugins and 5 Free Plugins
/25 Best WooCommerce Plugins for Products, Pricing, Payments and More
/10 Best WordPress Twitter Widgets
1 /12 Best Contact Form PHP Scripts for 2020
/20 Popular WordPress User Interface Elements
/10 Best WordPress Star Rating Plugins
/12 Best CSS Animations on CodeCanyon
/12 Best WordPress Booking and Reservation Plugins
/12 Elegant CSS Pricing Tables for Your Latest Web Project
/24 Best WordPress Form Plugins for 2020
/14 Best PHP Event Calendar and Booking Scripts
/Create a Blog for Each Category or Department in Your WooCommerce Store
/8 Best WordPress Booking and Reservation Plugins
/Best Exit Popups for WordPress Compared
/Best Exit Popups for WordPress Compared
/11 Best Tab & Accordion WordPress Widgets & Plugins
/12 Best Tab & Accordion WordPress Widgets & Plugins
1 /New Course: Practical React Fundamentals
/Short Course: Better Angular App Architecture With Modules
/Preview Our New Course on Angular Material
/Build Your Own CAPTCHA and Contact Form in PHP
/Object-Oriented PHP With Classes and Objects
/Best Practices for ARIA Implementation
/Accessible Apps: Barriers to Access and Getting Started With Accessibility
/Dramatically Speed Up Your React Front-End App Using Lazy Loading
/15 Best Modern JavaScript Admin Templates for React, Angular, and Vue.js
/15 Best Modern JavaScript Admin Templates for React, Angular and Vue.js
/19 Best JavaScript Admin Templates for React, Angular, and Vue.js
/New Course: Build an App With JavaScript and the MEAN Stack
/Hands-on With ARIA: Accessibility Recipes for Web Apps
/10 Best WordPress Facebook Widgets
13 /Hands-on With ARIA: Accessibility for eCommerce
/New eBooks Available for Subscribers
/Hands-on With ARIA: Homepage Elements and Standard Navigation
/Site Accessibility: Getting Started With ARIA
/How Secure Are Your JavaScript Open-Source Dependencies?
/New Course: Secure Your WordPress Site With SSL
/Testing Components in React Using Jest and Enzyme
/Testing Components in React Using Jest: The Basics
/Create Interactive Gradient Animations Using Granim.js
/How to Build Complex, Large-Scale Vue.js Apps With Vuex
1 /Examples of Dependency Injection in PHP With Symfony Components
/Set Up Routing in PHP Applications Using the Symfony Routing Component
1 /A Beginner’s Guide to Regular Expressions in JavaScript
/Introduction to Popmotion: Custom Animation Scrubber
/Introduction to Popmotion: Pointers and Physics
/New Course: Connect to a Database With Laravel’s Eloquent ORM
/How to Create a Custom Settings Panel in WooCommerce
/Building the DOM faster: speculative parsing, async, defer and preload
1 /20 Useful PHP Scripts Available on CodeCanyon
3 /How to Find and Fix Poor Page Load Times With Raygun
/Introduction to the Stimulus Framework
/Single-Page React Applications With the React-Router and React-Transition-Group Modules
/12 Best Contact Form PHP Scripts
1 /Getting Started With the Mojs Animation Library: The ShapeSwirl and Stagger Modules
/Getting Started With the Mojs Animation Library: The Shape Module
/Getting Started With the Mojs Animation Library: The HTML Module
/Project Management Considerations for Your WordPress Project
/8 Things That Make Jest the Best React Testing Framework
/Creating an Image Editor Using CamanJS: Layers, Blend Modes, and Events
/New Short Course: Code a Front-End App With GraphQL and React
/Creating an Image Editor Using CamanJS: Applying Basic Filters
/Creating an Image Editor Using CamanJS: Creating Custom Filters and Blend Modes
/Modern Web Scraping With BeautifulSoup and Selenium
/Challenge: Create a To-Do List in React
1Deploy PHP Web Applications Using Laravel Forge
/Getting Started With the Mojs Animation Library: The Burst Module
/A Gentle Introduction to Higher-Order Components in React: Best Practices
/A Gentle Introduction to HOC in React: Learn by Example
/A Gentle Introduction to Higher-Order Components in React
/Creating Pretty Popup Messages Using SweetAlert2
/Creating Stylish and Responsive Progress Bars Using ProgressBar.js
/18 Best Contact Form PHP Scripts for 2022
/How to Make a Real-Time Sports Application Using Node.js
/Creating a Blogging App Using Angular & MongoDB: Delete Post
/Set Up an OAuth2 Server Using Passport in Laravel
/Creating a Blogging App Using Angular & MongoDB: Edit Post
/Creating a Blogging App Using Angular & MongoDB: Add Post
/Introduction to Mocking in Python
/Creating a Blogging App Using Angular & MongoDB: Show Post
/Creating a Blogging App Using Angular & MongoDB: Home
/Creating a Blogging App Using Angular & MongoDB: Login
/Creating Your First Angular App: Implement Routing
/Persisted WordPress Admin Notices: Part 4
/Creating Your First Angular App: Components, Part 2
/Persisted WordPress Admin Notices: Part 3
/Creating Your First Angular App: Components, Part 1
/How Laravel Broadcasting Works
/Persisted WordPress Admin Notices: Part 2
/Create Your First Angular App: Storing and Accessing Data
/Persisted WordPress Admin Notices: Part 1
/Error and Performance Monitoring for Web & Mobile Apps Using Raygun
/Using Luxon for Date and Time in JavaScript
7 /How to Create an Audio Oscillator With the Web Audio API
/How to Cache Using Redis in Django Applications
/20 Essential WordPress Utilities to Manage Your Site
/Introduction to API Calls With React and Axios
/Beginner’s Guide to Angular 4: HTTP
/Rapid Web Deployment for Laravel With GitHub, Linode, and RunCloud.io
/Beginners Guide to Angular 4: Routing
/Beginner’s Guide to Angular 4: Services
/Beginner’s Guide to Angular 4: Components
/Creating a Drop-Down Menu for Mobile Pages
/Introduction to Forms in Angular 4: Writing Custom Form Validators
/10 Best WordPress Booking & Reservation Plugins
/Getting Started With Redux: Connecting Redux With React
/Getting Started With Redux: Learn by Example
/Getting Started With Redux: Why Redux?
/How to Auto Update WordPress Salts
/How to Download Files in Python
/Eloquent Mutators and Accessors in Laravel
1 /10 Best HTML5 Sliders for Images and Text
/Creating a Task Manager App Using Ionic: Part 2
/Creating a Task Manager App Using Ionic: Part 1
/Introduction to Forms in Angular 4: Reactive Forms
/Introduction to Forms in Angular 4: Template-Driven Forms
/24 Essential WordPress Utilities to Manage Your Site
/25 Essential WordPress Utilities to Manage Your Site
/Get Rid of Bugs Quickly Using BugReplay
1 /Manipulating HTML5 Canvas Using Konva: Part 1, Getting Started
/10 Must-See Easy Digital Downloads Extensions for Your WordPress Site
22 Best WordPress Booking and Reservation Plugins
/Understanding ExpressJS Routing
/15 Best WordPress Star Rating Plugins
/Creating Your First Angular App: Basics
/Inheritance and Extending Objects With JavaScript
/Introduction to the CSS Grid Layout With Examples
1 /Performant Animations Using KUTE.js: Part 5, Easing Functions and Attributes
/Performant Animations Using KUTE.js: Part 4, Animating Text
/Performant Animations Using KUTE.js: Part 3, Animating SVG
/New Course: Code a Quiz App With Vue.js
/Performant Animations Using KUTE.js: Part 2, Animating CSS Properties
/Performant Animations Using KUTE.js: Part 1, Getting Started
/10 Best Responsive HTML5 Sliders for Images and Text (Plus 3 Free Options)
/Single-Page Applications With ngRoute and ngAnimate in AngularJS
/Deferring Tasks in Laravel Using Queues
/Site Authentication in Node.js: User Signup and Login
/Working With Tables in React, Part Two
/Working With Tables in React, Part One
/How to Set Up a Scalable, E-Commerce-Ready WordPress Site Using ClusterCS
/New Course on WordPress Conditional Tags
/TypeScript for Beginners, Part 5: Generics
/Building With Vue.js 2 and Firebase
6 /Essential JavaScript Libraries and Frameworks You Should Know About
/Vue.js Crash Course: Create a Simple Blog Using Vue.js
/Build a React App With a Laravel RESTful Back End: Part 1, Laravel 5.5 API
/API Authentication With Node.js
/Beginner’s Guide to Angular: HTTP
/Beginner’s Guide to Angular: Routing
/Beginners Guide to Angular: Routing
/Beginner’s Guide to Angular: Services
/Beginner’s Guide to Angular: Components
/How to Create a Custom Authentication Guard in Laravel
/Learn Computer Science With JavaScript: Part 3, Loops
/Build Web Applications Using Node.js
/Learn Computer Science With JavaScript: Part 4, Functions
/Learn Computer Science With JavaScript: Part 2, Conditionals
/Create Interactive Charts Using Plotly.js, Part 5: Pie and Gauge Charts
/Create Interactive Charts Using Plotly.js, Part 4: Bubble and Dot Charts
/Create Interactive Charts Using Plotly.js, Part 3: Bar Charts
/Awesome JavaScript Libraries and Frameworks You Should Know About
/Create Interactive Charts Using Plotly.js, Part 2: Line Charts
/Bulk Import a CSV File Into MongoDB Using Mongoose With Node.js
/Build a To-Do API With Node, Express, and MongoDB
/Getting Started With End-to-End Testing in Angular Using Protractor
/TypeScript for Beginners, Part 4: Classes
/Object-Oriented Programming With JavaScript
/Stateful vs. Stateless Functional Components in React
/Build a To-Do API With Node and Restify
/Testing Components in Angular Using Jasmine: Part 2, Services
/Testing Components in Angular Using Jasmine: Part 1
/Creating a Blogging App Using React, Part 6: Tags
/React Crash Course for Beginners, Part 3
/React Crash Course for Beginners, Part 2
/React Crash Course for Beginners, Part 1
/Set Up a React Environment, Part 4
1 /Set Up a React Environment, Part 3
/New Course: Get Started With Phoenix
/Set Up a React Environment, Part 2
/Set Up a React Environment, Part 1
/Command Line Basics and Useful Tricks With the Terminal
/How to Create a Real-Time Feed Using Phoenix and React
/Build a React App With a Laravel Back End: Part 2, React
/Build a React App With a Laravel RESTful Back End: Part 1, Laravel 9 API
/Creating a Blogging App Using React, Part 5: Profile Page
/Pagination in CodeIgniter: The Complete Guide
/JavaScript-Based Animations Using Anime.js, Part 4: Callbacks, Easings, and SVG
/JavaScript-Based Animations Using Anime.js, Part 3: Values, Timeline, and Playback
/Learn to Code With JavaScript: Part 1, The Basics
/10 Elegant CSS Pricing Tables for Your Latest Web Project
/Getting Started With the Flux Architecture in React
/Getting Started With Matter.js: The Composites and Composite Modules
/Getting Started With Matter.js: The Engine and World Modules
/10 More Popular HTML5 Projects for You to Use and Study
/Understand the Basics of Laravel Middleware
/Iterating Fast With Django & Heroku
/Creating a Blogging App Using React, Part 4: Update & Delete Posts
/Creating a jQuery Plugin for Long Shadow Design
/How to Register & Use Laravel Service Providers
2 /Unit Testing in React: Shallow vs. Static Testing
/Creating a Blogging App Using React, Part 3: Add & Display Post
/Creating a Blogging App Using React, Part 2: User Sign-Up
20 /9 More Popular HTML5 Projects for You to Use and Study
/Creating a Grocery List Manager Using Angular, Part 2: Managing Items
/9 Elegant CSS Pricing Tables for Your Latest Web Project
/Dynamic Page Templates in WordPress, Part 3
/Angular vs. React: 7 Key Features Compared
/Creating a Grocery List Manager Using Angular, Part 1: Add & Display Items
/New eBooks Available for Subscribers in June 2017
/Create Interactive Charts Using Plotly.js, Part 1: Getting Started
/The 5 Best IDEs for WordPress Development (And Why)
/33 Popular WordPress User Interface Elements
/New Course: How to Hack Your Own App
/How to Install Yii on Windows or a Mac
/What Is a JavaScript Operator?
/How to Register and Use Laravel Service Providers
/
waly Good blog post. I absolutely love this…