Wordpress Conditional if is_single

10,886

Solution 1

WordPress automatically uses a different template page for different post types, if your post type is called products the files should be named single-products.php. You can read more about it from there Codex

In the same way single posts and their archives can be displayed using the single.php and archive.php template files, respectively,

  • single posts of a custom post type will use single-{post_type}.php
  • and their archives will use archive-{post_type}.php

where {post_type} is the $post_type argument of the register_post_type() function.

Solution 2

You can use is_singular() function, according to WordPress documentation: Ref Link

This conditional tag checks if a singular post is being displayed, which is the case when one of the following returns true: is_single(), is_page() or is_attachment(). If the $post_types parameter is specified, the function will additionally check if the query is for one of the post types specified.

True when viewing a post of the Custom Post Type book.

is_singular('book');

This will accomplish your task.

Share:
10,886
Remo Web
Author by

Remo Web

Updated on June 04, 2022

Comments

  • Remo Web
    Remo Web almost 2 years

    Im trying to use a conditional statement on my single.php page.

    What I want to do is, if it is of the Custom Post Type current-products, use a specific single -product.php template page, if not (ie a standard blog post) use the default single.php page.

    I think Im using the right statement, but don't know what to do afterwards (to get the template to be used):

    if ( is_single( 'current-products' == get_post_type() ) {    
        // If the post type is "Current Products" use this template...
        // Please help me out on this bit
    
    } elseif ( is_single() ) {
        // If not, use the standard one...
        // please help me out on this bit
    }
    

    I think that's right...?

  • Remo Web
    Remo Web over 10 years
    Perfect, I thought that was the case!