douyan9417 2014-10-21 18:22
浏览 44
已采纳

从functions.php-WP中设置的帖子的动态内容中排除页面

I have successfully managed, with the help of someone extremely helpful here on stackoverflow, to add a a custom and dynamic description to all the posts used in my website. My issue now is that when opening one of my pages, I do not get the page content but the dynamic description that was intended for posts only.

The code I have added to my functions.php to generate the dynamic descriptions 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 = '<h2>About the wallpaper:</h2><br />The <strong>'. $post->post_title . ' wallpaper</strong> is a high quality and high resolution wallpaper that has been posted in the <strong>';

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

        $content .= '</strong> category by <strong>Free Wallpapers</strong> on '. $post->post_date . '.';
    }
    return $content;
}
add_filter('the_content', 'add_after_post_content');

The issue now is that I am finding on post pages eg. copyright page, that the dynamic content is being used instead of the content set in the wordpress admin.

Copyright Policy and Usage Rights
About the wallpaper:

The Copyright Policy and Usage Rights wallpaper is a high quality and high resolution wallpaper that has been posted in the category by Free Wallpapers on 2014-05-14 20:56:29. 

I would like to continue using the dynamic content for my posts, while excluding pages from displaying it. Any help in regards to this matter would be greatly appreciate.

Best Regards

  • 写回答

1条回答 默认 最新

  • dsf4s5787 2014-10-21 18:32
    关注

    You want to use the is_singular function. Now you want to define your content like this:

    function add_after_post_content($content) {
        global $post;
        if(!is_feed() && !is_home() && is_singular('post') && 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 = '<h2>About the wallpaper:</h2><br />The <strong>'. $post->post_title . ' wallpaper</strong> is a high quality and high resolution wallpaper that has been posted in the <strong>';
    
            foreach($cats as $c){
                $content .= $c['name'];
            }
    
            $content .= '</strong> category by <strong>Free Wallpapers</strong> on '. $post->post_date . '.';
        }
        return $content;
    }
    add_filter('the_content', 'add_after_post_content');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?