is singular with custom post type but without id

I want to echo some text on all custom post types from the same group.

The following is_singular code successfully displays the text on all CPTs:

Read More
if (is_singular () ) {
   echo 'something'; }

The following is_singular code with the plugin’s CPT identifier successfully displays the text on just one specific instance of that CPT group:

if (is_singular ('tu_result_1836') ) {
   echo 'something'; }

To apply it to all CPTs from the tu_result group, if I try ‘tu_result’ or ‘tu_result_’ instead of ‘tu_result_1836’, it no longer works. Do I need to use a wildcard function, e.g. ‘tu_result_*’? Or is it incorrect to use is_singular here. I have also tried all of the above combinations with is_single.
Hopefully someone can help. Thanks 🙂

Related posts

2 comments

  1. Does the following works for you? :

    if( is_singular() && strpos(get_post_type(), 'tu_result_') !== false ){}
    
  2. There’s a few ways to handle what you are tying to do:

    Note: All information below assumes you’ve named your custom post type tu_result. If not, adjust as necessary.

    First, you could use the Template Heirarchy System to your advantage, and create a template for your tu_result. If it doesn’t exist, create it – name it single-tu_result.php, and it will be the template that WordPress automatically displays for any single posts that are tu_result types.

    Second, you could extend your functionality slightly using the get_post_type() function, like so:

    if ( is_singular() ) {
        if ( 'tu_result' == get_post_type() ) {
            echo 'This will show ONLY for the tu_result post types!';
        }
    }
    

Comments are closed.