admin管理员组文章数量:1130349
I need to determine if the current post has a "more" tag. I'm currently using
$pos=strpos($post->post_content, '<!--more-->');
Am I missing a built in method similar to has_excerpt()?
I need to determine if the current post has a "more" tag. I'm currently using
$pos=strpos($post->post_content, '<!--more-->');
Am I missing a built in method similar to has_excerpt()?
Share Improve this question asked Jan 5, 2012 at 16:44 N2MysticN2Mystic 3,1937 gold badges47 silver badges72 bronze badges 1 |4 Answers
Reset to default 2Quite simply put: there is no built in function that does the same thing as your code above.
Bonus content: More tag tricks
Making a quick note of the codes we could use to show the_content(); if the More tag exists, and the_excerpt(); if it doesn't.
Code #1 (Recommended)
<?php
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: MichaelH)
Code #2
<?php
if( strpos( get_the_content(), 'more-link' ) === false ) {
the_excerpt();
}
else {
the_content();
}
?>
(Credit: Michael) Basically does #1 the other way around.
Code #3
<?php
if( preg_match( '/<!--more(.*?)?-->/', $post->post_content ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: helgatheviking) For use only in edge cases where you cannot use strpos(). Generally strpos() is more efficient than preg_match().
Making it more conditional:
<?php
if ( is_home() || is_archive() || is_search() ) {
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) );
}
else {
the_excerpt();
}
}
else {
the_content();
}
?>
What does it do? If the page shown is home, archive or search results page, then show the_content(); if the More tag exists, the_excerpt(); if it doesn't, and simply show the_excerpt(); on all other pages.
I could not get any of the provided solution to work, however I found this to be working well for me, publishing it as an extra solution if anybody else had problems getting it working. Just testing if the content is them same with and without stripping the teaser.
// Choose the manual excerpt if exists
if ( has_excerpt() ) :
the_excerpt();
// Is there a more tag? Then use the teaser. ()
elseif ( get_the_content('', false) != get_the_content('', true) ) :
global $more;
$more = 0;
echo strip_tags(get_the_content( '', false ));
$more = 1;
// Otherwise make an automatic excerpt
else :
the_excerpt(40);
endif;
For the ones searching for a more WP related answer, You can use this logic:
$info = get_extended($post->post_content);
if(!empty($info["extended"])){
// it has a read more tag.
}else{
// it hasn't one.
}
For this, you can blame the WP Core if it doesn't work right. :)
Reference: https://developer.wordpress/reference/functions/get_extended/
I need to determine if the current post has a "more" tag. I'm currently using
$pos=strpos($post->post_content, '<!--more-->');
Am I missing a built in method similar to has_excerpt()?
I need to determine if the current post has a "more" tag. I'm currently using
$pos=strpos($post->post_content, '<!--more-->');
Am I missing a built in method similar to has_excerpt()?
Share Improve this question asked Jan 5, 2012 at 16:44 N2MysticN2Mystic 3,1937 gold badges47 silver badges72 bronze badges 1-
Not sure exactly where you're trying to run your code, but if you're on a single post view the
$pagesglobal should hold a value representing the number of pages(if the post is paged, ie. has content that spans pages, eg. it has more!).. – t31os Commented Jan 6, 2012 at 19:28
4 Answers
Reset to default 2Quite simply put: there is no built in function that does the same thing as your code above.
Bonus content: More tag tricks
Making a quick note of the codes we could use to show the_content(); if the More tag exists, and the_excerpt(); if it doesn't.
Code #1 (Recommended)
<?php
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: MichaelH)
Code #2
<?php
if( strpos( get_the_content(), 'more-link' ) === false ) {
the_excerpt();
}
else {
the_content();
}
?>
(Credit: Michael) Basically does #1 the other way around.
Code #3
<?php
if( preg_match( '/<!--more(.*?)?-->/', $post->post_content ) ) {
the_content();
}
else {
the_excerpt();
}
?>
(Credit: helgatheviking) For use only in edge cases where you cannot use strpos(). Generally strpos() is more efficient than preg_match().
Making it more conditional:
<?php
if ( is_home() || is_archive() || is_search() ) {
if( strpos( $post->post_content, '<!--more-->' ) ) {
the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'twentytwelve' ) );
}
else {
the_excerpt();
}
}
else {
the_content();
}
?>
What does it do? If the page shown is home, archive or search results page, then show the_content(); if the More tag exists, the_excerpt(); if it doesn't, and simply show the_excerpt(); on all other pages.
I could not get any of the provided solution to work, however I found this to be working well for me, publishing it as an extra solution if anybody else had problems getting it working. Just testing if the content is them same with and without stripping the teaser.
// Choose the manual excerpt if exists
if ( has_excerpt() ) :
the_excerpt();
// Is there a more tag? Then use the teaser. ()
elseif ( get_the_content('', false) != get_the_content('', true) ) :
global $more;
$more = 0;
echo strip_tags(get_the_content( '', false ));
$more = 1;
// Otherwise make an automatic excerpt
else :
the_excerpt(40);
endif;
For the ones searching for a more WP related answer, You can use this logic:
$info = get_extended($post->post_content);
if(!empty($info["extended"])){
// it has a read more tag.
}else{
// it hasn't one.
}
For this, you can blame the WP Core if it doesn't work right. :)
Reference: https://developer.wordpress/reference/functions/get_extended/
本文标签: theme developmentIs there a hasmoretag() method or equivalent
版权声明:本文标题:theme development - Is there a has_more_tag() method or equivalent? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749021557a2304454.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


$pagesglobal should hold a value representing the number of pages(if the post is paged, ie. has content that spans pages, eg. it has more!).. – t31os Commented Jan 6, 2012 at 19:28