2

Accordingly to the is_page documentation, supplying the slug as an argument WP can tell me whether the current page is the one represented by the slug. Perhaps I am using the wrong slug? However I did try both the path used in the rewrite argument as well as the bit I consider to be the "slug" or in other words the element used to register the CPT.

Please find below a code snippet to let you understand:

'rewrite'               => array( 'slug' => 'faqs/archivio' )
  );

register_post_type( 'faqs', $args );

I did this a go using the slug used for a custom post type, but I wasn't able to get this working.

So two questions?

  1. Is the is_page supposed to work in this case?
  2. if not, what the best alternative?

Thanks Andrea

3

The creation of a custom post type have it inside the definition, a post type. Also is page only a default post type. If you register your custom post type, then check for this post type, not for a page.

Conditional Tag is_singular

The function, conditional tag is_singular() main purpose is to check if viewing a singular post. However, it can also be used to check if viewing a singular post of a given post type by entering the $post_type parameter as shown in the following code snippet.

is_singular( 'your_custom_post_type' );

or different post types

is_singular( array( 'your_custom_post_type_1', 'your_custom_post_type_2' ) );

Hint to debug

I use the plugin Debug Objects to debug topics in this context. Maybe this helps you also, often easier as read the code or codex. See the follow screenshot of the plugin, the result of the conditional tags, include is_page() and is_single() on a CPT. enter image description here

  • Thanks, I will download this plugin. Can you please clarify where the typo error is? Apparentely my CPT works fine, so I don't understand. – Andrea Moro Apr 30 '14 at 6:40
  • Which typo error do you mean? – bueltge Apr 30 '14 at 14:01
  • Sorry ... I now get the phrase. You wrote " a post typo. " wherease it should have been " a post type ". So I thought it was a mystypo on my side. BTW I've installed the plugin, which is no longer in beta. The layout is different and I can't find the output of my is_page or is_singular. I suppose they are under the debug_hooks section. – Andrea Moro May 1 '14 at 6:51
  • Yes, was a mistake from my side, now fixed to "type". If you have install the plugin, see the settings, activate the conditional topic. If you are on the side, there will debug, click the bug in the admin bar or use the url param, also documented on the settings page. – bueltge May 2 '14 at 18:41
0

You can do like this, for routing single.php file for different type of posts.

I'm having two single.php files named by single-product.php and single-blogs.php.

Now in single.php file i written my below code to redirect different posts based on Custom Post type

if (get_post_type('product'))
{        
  include (TEMPLATEPATH . '/single-product.php');
}
else 
{         
 include (TEMPLATEPATH . '/single-blogs.php');
}

or

if (is_singular('product'))
{        
 include (TEMPLATEPATH . '/single-product.php');
}
else 
{         
  include (TEMPLATEPATH . '/single-blogs.php');
}
  • If you're loading template files it would be best practice to use get_template_part() , rather than include. – Jacob Peattie Apr 10 '18 at 11:04

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.