0

I am trying to write a shortcode to display the page title. I only got the archive title to work, but not custom post type category and single post. Can anyone shed light how to do this?

The reason for going this path is Elemenentor theme builder generating too much code for simple page title and subheading inside secondary header that is masked wrap around an image. I use the shortcode widget to insert the code and style it.

So far this is what I have written:

// Page Title inside shortcode
function page_title_sc( ) {
    $title = ('Page Title');

   if ( is_post_type_archive() ) {
    $title = post_type_archive_title( '', false ); 
 }
  elseif ( is_page() ) {
    $title = single_post_title();
   } 
   return apply_filters( 'page_title_sc', $title );
}
 
add_shortcode( 'page_title', 'page_title_sc' );

2 Answers 2

1

Add Below Code For post title, page title, category title , tag title , taxonmy title , Author title .

According to your Needed.

function page_title_sc( ) {

    $title = ('Page Title');

    if ( is_page() || is_singular() ) {
        $title = single_post_title();
    } else if ( is_category() ) {
        $title = single_cat_title( '', false );
    } else if ( is_tag() ) {
        $title = single_tag_title( '', false );
    } else if ( is_author() ) {
        $title = '<span class="vcard">' . get_the_author() . '</span>';
    } else if ( is_post_type_archive() ) {
        $title = post_type_archive_title( '', false );
    } else if ( is_tax() ) {
        $title = single_term_title( '', false );
    }

   return apply_filters( 'page_title_sc', $title );
}
 
add_shortcode( 'page_title', 'page_title_sc' );
2
  • Thank for the code but the page and custom post, the title escape from container. Here is what I am trying to achieve <div class="heading-b"> <div class="img-wrap"><img src="Asset-2-8-e1606610476991-3.png"> </div> <div class="heading-text-wrap"> <span class="subheading">Biodiversity Conservation</span> <span class="heading-title">Our Work</span> </div> </div> Jan 26, 2021 at 9:49
  • you are used to elementer shortcode widget please share screenshot for more knowlege. I am not understood.
    – HK89
    Jan 26, 2021 at 10:04
0

I'm not completely sure what you want to do but is_page will check for a page and exclude (custom) posts. If you want the condition to work for posts you will need is_single. If you want the condition to work for both posts and pages you will need is_singular.

That said, going this path may not be the smartest thing to do, because you will be polluting your post content. It would be more logical to write a filter for the title that cancels what Elementor does to it that you don't like. If you write a filter with a high priority, you can achieve the same thing you are now doing with a shortcode without having to modify your post content.

2
  • I have limited experience writing PHP, how can I achieve your suggestion via shortcode? Jan 26, 2021 at 9:24
  • My suggestion is to not use a shortcode in the_content but a filter on the_title. But if you don't know how to do that you could change the condition in the shortcode.
    – cjbj
    Jan 26, 2021 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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