admin管理员组文章数量:1130349
I want to add content to a post dependent on the post categories.
I have found the following code, and it works fine on the posts. But when I try to load a page it comes up with no content.
function my_category_content ( $content ) {
global $post;
if ( is_single() ) {
if ( in_category( "", $post->ID ) ) {
$content .= 'Only showing up on the categories named mini [product_category category="mini" limit="12" columns="4" orderby="date" order="desc"]';
}
return $content;
}
}
add_filter( 'the_content', 'my_category_content' );
I want to add content to a post dependent on the post categories.
I have found the following code, and it works fine on the posts. But when I try to load a page it comes up with no content.
function my_category_content ( $content ) {
global $post;
if ( is_single() ) {
if ( in_category( "", $post->ID ) ) {
$content .= 'Only showing up on the categories named mini [product_category category="mini" limit="12" columns="4" orderby="date" order="desc"]';
}
return $content;
}
}
add_filter( 'the_content', 'my_category_content' );
Share
Improve this question
edited Jan 6, 2019 at 2:35
butlerblog
5,1413 gold badges28 silver badges44 bronze badges
asked Jan 5, 2019 at 20:55
Mark ClowesMark Clowes
1
1 Answer
Reset to default 1The problem is that you are returning $content from within a conditional statement that could potentially resolve as false (and will, in the case of pages, because is_single() does not apply to pages).
Move your return to outside all "if" conditions to make sure that $content is always returned, regardless of whether it is filtered or not:
function my_category_content ( $content ) {
global $post;
if ( is_single() ) {
if ( in_category( "", $post->ID ) ) {
$content .= 'Only showing up on the categories named mini [product_category category="mini" limit="12" columns="4" orderby="date" order="desc"]';
}
}
return $content;
}
add_filter( 'the_content', 'my_category_content' );
I want to add content to a post dependent on the post categories.
I have found the following code, and it works fine on the posts. But when I try to load a page it comes up with no content.
function my_category_content ( $content ) {
global $post;
if ( is_single() ) {
if ( in_category( "", $post->ID ) ) {
$content .= 'Only showing up on the categories named mini [product_category category="mini" limit="12" columns="4" orderby="date" order="desc"]';
}
return $content;
}
}
add_filter( 'the_content', 'my_category_content' );
I want to add content to a post dependent on the post categories.
I have found the following code, and it works fine on the posts. But when I try to load a page it comes up with no content.
function my_category_content ( $content ) {
global $post;
if ( is_single() ) {
if ( in_category( "", $post->ID ) ) {
$content .= 'Only showing up on the categories named mini [product_category category="mini" limit="12" columns="4" orderby="date" order="desc"]';
}
return $content;
}
}
add_filter( 'the_content', 'my_category_content' );
Share
Improve this question
edited Jan 6, 2019 at 2:35
butlerblog
5,1413 gold badges28 silver badges44 bronze badges
asked Jan 5, 2019 at 20:55
Mark ClowesMark Clowes
1
1 Answer
Reset to default 1The problem is that you are returning $content from within a conditional statement that could potentially resolve as false (and will, in the case of pages, because is_single() does not apply to pages).
Move your return to outside all "if" conditions to make sure that $content is always returned, regardless of whether it is filtered or not:
function my_category_content ( $content ) {
global $post;
if ( is_single() ) {
if ( in_category( "", $post->ID ) ) {
$content .= 'Only showing up on the categories named mini [product_category category="mini" limit="12" columns="4" orderby="date" order="desc"]';
}
}
return $content;
}
add_filter( 'the_content', 'my_category_content' );
本文标签: Adding additional content on post depending on post categories
版权声明:本文标题:Adding additional content on post depending on post categories 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749040381a2307042.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论