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

报告相同问题?

悬赏问题

  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了