admin管理员组文章数量:1022496
I wonder if anyone knows how to add an if to this function. I need the read more to only be added to the the_excerpt when the category is 429
function mywiki_trim_excerpt($mywiki_text) {
$text = substr($mywiki_text,0,-10);
return $text.'...<a class="bigger" href="'.get_permalink().'" title="'.__('read more...','mywiki').'">'.__('Read more','mywiki').'</a>';
}
add_filter('get_the_excerpt', 'mywiki_trim_excerpt');`
Thanks for any pointers in advance Richard
I wonder if anyone knows how to add an if to this function. I need the read more to only be added to the the_excerpt when the category is 429
function mywiki_trim_excerpt($mywiki_text) {
$text = substr($mywiki_text,0,-10);
return $text.'...<a class="bigger" href="'.get_permalink().'" title="'.__('read more...','mywiki').'">'.__('Read more','mywiki').'</a>';
}
add_filter('get_the_excerpt', 'mywiki_trim_excerpt');`
Thanks for any pointers in advance Richard
Share Improve this question edited Apr 23, 2019 at 15:13 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 23, 2019 at 14:51 frank roburoughfrank roburough 32 bronze badges 3- 1 When you're viewing category 429, or when the current post is in category 429? An important difference. – Jacob Peattie Commented Apr 23, 2019 at 15:07
- Curious, would that be in_category() vs is_category() ? Maybe you could write an answer that included both use cases for the OP. – RiddleMeThis Commented Apr 23, 2019 at 15:46
- Thanks, Is when the current post is in category 429 – frank roburough Commented Apr 24, 2019 at 7:15
1 Answer
Reset to default 0So the answer depends on what you mean by "when the category is 429". This could mean either:
- You are currently viewing the archive for category 429, and want this to be added to any posts that are listed.
- You are currently viewing the blog, date archive, or search results, and want to add this text to any individual post that belongs to category 429.
These are different things, and the solution is different for each.
For #1 you want to use is_category()
. This will tell you if the current page being viewed is the archive for category 429, and will apply the filter to all excerpts displayed on this page:
function mywiki_trim_excerpt( $excerpt ) {
if ( is_category( 429 ) ) {
$excerpt = substr( $excerpt, 0, -10 );
$excerpt = $excerpt . '...<a class="bigger" href="' . get_permalink() . '" title="' . __( 'read more...', 'mywiki' ) . '">' . __( 'Read more', 'mywiki' ) . '</a>';
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'mywiki_trim_excerpt' );
For #2 it's slightly more complicated, because you need to check the category against the post the excerpt is for. You can do this by accepting 2 arguments in the filter (set the 4th argument of add_filter()
to 2
), and using the 2nd argument ($post
, the current post) with has_category()
:
function mywiki_trim_excerpt( $excerpt, $post ) {
if ( has_category( 429, $post ) ) {
$excerpt = substr( $excerpt, 0, -10 );
$excerpt = $excerpt . '...<a class="bigger" href="' . get_permalink() . '" title="' . __( 'read more...', 'mywiki' ) . '">' . __( 'Read more', 'mywiki' ) . '</a>';
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'mywiki_trim_excerpt', 10, 2 );
Technically has_category()
will work without passing $post
. If you leave that out then it will check the category of the 'current post'. 99% of the time this is the same as the post that the excerpt is for, but in rare edge cases the 'current' post might not be the same post as the post who's excerpt is being filtered. By using the $post
value passed to the filter we can guarantee that the post we are checking the category of is the same post that the excerpt is for.
I wonder if anyone knows how to add an if to this function. I need the read more to only be added to the the_excerpt when the category is 429
function mywiki_trim_excerpt($mywiki_text) {
$text = substr($mywiki_text,0,-10);
return $text.'...<a class="bigger" href="'.get_permalink().'" title="'.__('read more...','mywiki').'">'.__('Read more','mywiki').'</a>';
}
add_filter('get_the_excerpt', 'mywiki_trim_excerpt');`
Thanks for any pointers in advance Richard
I wonder if anyone knows how to add an if to this function. I need the read more to only be added to the the_excerpt when the category is 429
function mywiki_trim_excerpt($mywiki_text) {
$text = substr($mywiki_text,0,-10);
return $text.'...<a class="bigger" href="'.get_permalink().'" title="'.__('read more...','mywiki').'">'.__('Read more','mywiki').'</a>';
}
add_filter('get_the_excerpt', 'mywiki_trim_excerpt');`
Thanks for any pointers in advance Richard
Share Improve this question edited Apr 23, 2019 at 15:13 Krzysiek Dróżdż 25.6k9 gold badges53 silver badges74 bronze badges asked Apr 23, 2019 at 14:51 frank roburoughfrank roburough 32 bronze badges 3- 1 When you're viewing category 429, or when the current post is in category 429? An important difference. – Jacob Peattie Commented Apr 23, 2019 at 15:07
- Curious, would that be in_category() vs is_category() ? Maybe you could write an answer that included both use cases for the OP. – RiddleMeThis Commented Apr 23, 2019 at 15:46
- Thanks, Is when the current post is in category 429 – frank roburough Commented Apr 24, 2019 at 7:15
1 Answer
Reset to default 0So the answer depends on what you mean by "when the category is 429". This could mean either:
- You are currently viewing the archive for category 429, and want this to be added to any posts that are listed.
- You are currently viewing the blog, date archive, or search results, and want to add this text to any individual post that belongs to category 429.
These are different things, and the solution is different for each.
For #1 you want to use is_category()
. This will tell you if the current page being viewed is the archive for category 429, and will apply the filter to all excerpts displayed on this page:
function mywiki_trim_excerpt( $excerpt ) {
if ( is_category( 429 ) ) {
$excerpt = substr( $excerpt, 0, -10 );
$excerpt = $excerpt . '...<a class="bigger" href="' . get_permalink() . '" title="' . __( 'read more...', 'mywiki' ) . '">' . __( 'Read more', 'mywiki' ) . '</a>';
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'mywiki_trim_excerpt' );
For #2 it's slightly more complicated, because you need to check the category against the post the excerpt is for. You can do this by accepting 2 arguments in the filter (set the 4th argument of add_filter()
to 2
), and using the 2nd argument ($post
, the current post) with has_category()
:
function mywiki_trim_excerpt( $excerpt, $post ) {
if ( has_category( 429, $post ) ) {
$excerpt = substr( $excerpt, 0, -10 );
$excerpt = $excerpt . '...<a class="bigger" href="' . get_permalink() . '" title="' . __( 'read more...', 'mywiki' ) . '">' . __( 'Read more', 'mywiki' ) . '</a>';
}
return $excerpt;
}
add_filter( 'get_the_excerpt', 'mywiki_trim_excerpt', 10, 2 );
Technically has_category()
will work without passing $post
. If you leave that out then it will check the category of the 'current post'. 99% of the time this is the same as the post that the excerpt is for, but in rare edge cases the 'current' post might not be the same post as the post who's excerpt is being filtered. By using the $post
value passed to the filter we can guarantee that the post we are checking the category of is the same post that the excerpt is for.
本文标签: functionsAppending „read more” to the excerpt conditionally
版权声明:本文标题:functions - Appending „read more” to the excerpt conditionally 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745560013a2156108.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论