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

时间:2014-10-21 18:22:49

标签: php jquery wordpress

我已成功管理,在stackoverflow上非常有帮助的人的帮助下,为我网站中使用的所有帖子添加自定义和动态描述。我现在的问题是,在打开我的一个页面时,我没有获得页面内容,而是仅用于帖子的动态描述。

我添加到我的functions.php中以生成动态描述的代码如下:

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');

现在的问题是我在帖子页面上找到了例如。版权页面,正在使用动态内容而不是wordpress管理员中设置的内容。

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. 

我想继续为我的帖子使用动态内容,同时排除显示它的页面。任何有关此事的帮助都将非常感激。

最好的问候

1 个答案:

答案 0 :(得分:0)

您想使用is_singular功能。现在您想要定义这样的内容:

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');