admin管理员组文章数量:1305411
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' );
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' );
Share
Improve this question
edited Jan 26, 2021 at 8:56
cjbj
15k16 gold badges42 silver badges89 bronze badges
asked Jan 26, 2021 at 8:44
William JeromeWilliam Jerome
1836 silver badges13 bronze badges
2 Answers
Reset to default 1Add 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' );
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.
本文标签: Display page and custom post title inside shortcode
版权声明:本文标题:Display page and custom post title inside shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741803686a2398372.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论