0

I have a slight issue. I have managed, with the help of someone one stackoverflow to write a code to add dynamic content to all my wordpress posts. The code is now in my functions.php file and is as follows.

function add_after_post_content($content) {
    global $post;
    if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
        $post_categories = wp_get_post_categories( $post->ID );
        $cats = array();

        foreach($post_categories as $c){
            $cat = get_category( $c );
            $cats[] = array( 'name' => $cat->name, 'slug' => $cat->slug );
        }

        $content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';

        foreach($cats as $c){
            $content .= $c['name'];
        }

        $content .= ' category by <strong>Free Wallpapers</strong> on '. $post->post_date . '. <br /><br />';
    }
    return $content;
}

add_filter('the_content', 'add_after_post_content');

The issue is that some of my older posts already have content in them and hence both the old content as well as the new are displayed beneath them. whereas I would like this to be the new content/description for all posts. Currently in my content-single.php I believe this is the line which calls the original post content plus the code I have mentioned above.

<?php the_content(); ?> 

I would like to wither remove or modify my content-single.php/code added to functions.php file so that only the new code displays the dynamic description, and the original post content is not displayed. i.e. putting it simply if the entire new code could be labeled content2 and the content-single.php code will output this content2 only. I have tried simply moving the code from functions.php to replace

<?php the_content(); ?> 

in the content-single.php but this gives me all kinds of errors.

Any help on this issue would be greatly appreciated.

Best Regards

1 Answer 1

0

Remove the dot after $content.

$content .= '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';

Replace above line with following

$content = '<strong>'. $post->post_title . '</strong> is a <strong>wallpaper</strong> posted in the ';
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.