$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Adding additional content on post depending on post categories|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Adding additional content on post depending on post categories

matteradmin11PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 1

The 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' );
Post a comment

comment list (0)

  1. No comments so far