0

So I'm displaying 3 different layout types, one for pages, one for posts (blog, archives, tags, search etc..) and one for single posts.

Originally, I was using this to decide whether I was on a page which displayed posts:

if ( is_home() ||
    is_category() || 
    is_tag() || 
    is_archive() || 
    is_tax() || 
    is_author() || 
    is_date() || 
    is_search() || 
    is_attachment() ) :

It's not pretty, but it covers everything.

My question is, is this practically the same thing, but cleaner?

if ( ! is_page() && ! is_single() ) :

Does that cover everything in the first block of code? Am I missing something completely obvious that will cause this to not work as intended?

I think it does the same thing, but I wanted to bounce this off the community as well.

Thanks!

EDIT - here's what I have now:

// Set up the layout variable for pages
$layout = $generate_settings['layout_setting'];

// If we're on the single post page
if ( is_single() ) :
    $layout = null;
    $layout = $generate_settings['single_layout_setting'];
endif;

// If we're on the blog, archive, attachment etc..
if ( is_home() || is_archive() || is_search() || is_attachment() || is_tax() ) :
    $layout = null;
    $layout = $generate_settings['blog_layout_setting'];
endif;

// Finally, return the layout
return apply_filters( 'generate_sidebar_layout', $layout );

Seems to work perfectly, and is cleaner than my original group of conditionals.

I couldn't find any proof that is_tax is taken care of by is_archive() - did I just miss it?

Thanks!

2

Both will do the same job in the end of the day. You can however improve on the first code.

is_archive() returns true on all archives page, which includes the following

  • taxonomy archive pages

  • category archive pages

  • date archive pages

  • author archive pages

  • tag archive pages

So, if you need to target all these pages, you can simply make use of is_archive().

ONE OTHER NOTE

is_single() returns true when is_attachment() return true as an attachment is also a single post. So be careful when using the second block of code when you need something different on attachment pages and normal single pages

IN GENERAL

There are a few things that I don't get on your implementation. You can do what you need to do with the correct templates. You do not need conditionals

Look at the Template Hierarchy, and then look what you need to achieve. You only need 4 templates

  • For pages - Create page.php

  • For home, archive and search pages, create index.php. index.php is a fall back if the custom templates (like search.php or category.php) don't exist. So Wordpress will use index.php to show the homepage, archives and search result pages

  • For attachements, copy index.php and rename it attachment.php

  • For single posts, create single.php. This should be more than enough

  • Thanks for the info! Definitely have more to look into.. The reason I'm using these checks is I have options for page layouts in the Customizer. You can choose one layout for pages, one for the blog (and other blog templates like archive.php etc..) and one for the single post layout. I definitely have index.php, page.php and other templates. This code is just to check for which sidebar layout to use :) – Tom Feb 20 '15 at 19:20
1

To answer the question, no it's not the same. It is cleaner, but for example a home page can have true results for is_page and is_single.

Additionally, why are you making these checks for template display? There isn't enough detail to be sure, but it sounds like you could utilize the Wordpress template hierarchy to accomplish this without any checks and a much neater file organization / code separation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.