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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源