Showing posts with label tags. Show all posts
Showing posts with label tags. Show all posts

Right Way Of Using WordPress Homepage Conditionals

WordPress is a terrific CMS. You can do whatever you want with it. But sometimes, you may find an issue with some basic functions. For example, you are probably using the is_front_page and is_home WordPress Homepage conditional in your themes and plugins (you can learn more about conditional functions in our Conditional Tags post). But you’re doing it wrong !

Homepage Settings

With those conditional functions, you can check if the homepage is being displayed (is_home), or if the homepage is using a static page (is_front_page). You can define such behaviour in Settings=> Reading.

In your plugin or theme, you may use this functions to display or change contents depending on user settings. But you are doing a huge mistake : you are not taking into account the static blog page. When you define a static page for your homepage, you can also define a blog page : it will replace the default WordPress homepage in order to list every post.
Long story made short : is_front_page should always be tested with and before is_home. And here’s why.

How It Works

1. Normal settings:
The default WordPress homepage lists your latest posts.
  • Here is the URL : website.com
  • is_home returns TRUE
  • is_front_page returns TRUE
Here, there isn’t any issue.
2. Static homepage:
The homepage is using one of your static pages.
  • The URL is still website.com
  • is_home returns FALSE
  • is_front_page returns TRUE
There is no issue with your static homepage : is_front_page returns true.
3. Static blog page
One of your page is listing all your post.
  • The URL is website.com/pagename
  • is_home : TRUE
  • is_front_page : FALSE
Here, you may have some problems : is_home is returning TRUE, but this is not your homepage : it’s a WordPress page that lists every post, like a main category archive would do.

Use Conditional Tags

When you create a plugin or a theme, you have to consider that users may use those page settings. So, every time you use is_home, you should always test is_front_page before, and you always should use conditional parameters to do so.
If you don’t, you may have problems with some users. For example, if you only test is_home to display your homepage content, there will be issues with the static homepage that will not display the right content.
Here is how to do it right :
if ( is_front_page() && is_home() ){
	// Default homepage
} elseif ( is_front_page()){
	//Static homepage
} elseif ( is_home()){
	//Blog page
} else {
	//everything else
}
This is the only (right) way to display or alter content with your homepage and your blog page.

How About You?

Did you already encounter this problem? What was your solution? I want to hear about it!
Continue Reading

List Of All WordPress Conditional Tags

We all use conditional statements in our developments under WordPress, right? But do you know all built-in WordPress conditional tags? They are simply great because thanks to these tags it’s very easy to create conditional statements. You can for example display content only for some users, or load scripts only if a page is using a specific custom template, or even styling a post if this post is sticky.
Conditional tags can be used in your themes or in your plugins, it doesn’t matter, but note that in some plugins it will be sometimes a bit tricky to use some tags (in fact all themes specific tags).

Available conditional tags

Here is the list of available conditional functions:
You probably know some of them, but do you really know the difference between is_single_() and is_singular() ? Or between id_day() and is_date() ? Did you know is_preview() ? To understand how each function works, simply click on the tag name, and read carefully the instructions in the codex.

All conditional tags return TRUE or FALSE

Conditional tags are one of my favorite WordPress built-in feature and i really encourage you to use them widely in your codes. All functions always return TRUE or FALSE, never IDs or array().

A close relation with templates hierarchy

As you might know, WordPress uses a really powerful template hierarchy system. Well, the use of conditional tags are closely related to this hierarchy. This is the principle used to select the template page to use depending on the URL you are visiting.

Conditional tags accept many arguments

When using these conditional tags you have to know that you can pass them different variables such as IDs, slug, or arrays. Here is is an example taken from the codex with the is_author() function:
is_author() 
When any Author page is being displayed.
is_author( '4' ) 
When the archive page for Author number (ID) 4 is being displayed.
is_author( 'Vivian' ) 
When the archive page for the Author with Nickname “Vivian” is being displayed.
is_author( 'john-jones' ) 
When the archive page for the Author with Nicename “john-jones” is being displayed.
is_author( array( 4, 'john-jones', 'Vivian' ) ) 
When the archive page for the author is either user ID 4, or user_nicename “john-jones”, or nickname “Vivian”.

Examples

This is how nearly all conditional functions work but you can of course combine many conditions:
if ( is_home() && in_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) ) {
   the_content();
}
else {
   the_excerpt();
}
or you can do an action if a situation or another occurs:
if ( is_home() || in_category( array( 9, 'blue-cheese', 'Stinky Cheeses' ) ) ) {
   the_content();
}
else {
   the_excerpt();
}
And if you want to do a negative condition, use ” ! “:
$paged = $wp_query->get( 'page' );

if ( ! $paged || $paged < 2 ) 
{
    // This is not a paginated page (or it's simply the first page of a paginated page/post)
} 
else 
{
   // This is a paginated page.
}
And finally, using conditional tags is like any other “else if” PHP statement:
if ( is_page( 'about' ) || '2' == $post->post_parent ) {    
    // the page is "About", or the parent of the page is "About"
    $bannerimg = 'about.jpg';

} elseif ( is_page( 'learning' ) || '56' == $post->post_parent ) {	
    $bannerimg = 'teaching.jpg';

} elseif ( is_page( 'admissions' ) || '15' == $post->post_parent ) { 
    $bannerimg = 'admissions.jpg';

} else { 
    $bannerimg = 'home.jpg'; // just in case we are at an unclassified page, perhaps the home page
}	

Creating custom conditional tags

In some cases you need to create your custom conditional functions. That’s pretty easy to do, but very often functions names aren’t following the same logic as the one used by core developers. Please use the same naming convention using prefix such as “is_“, “has_“, “in_“. This is very helpful in particular if you’re not the only person to work on a specific script.
Continue Reading