Wordpress自定义摘录博客帖子

时间:2016-11-21 13:12:21

标签: wordpress custom-post-type

我正在尝试将此自定义摘录添加到博客帖子中,以便它从帖子中抓取前35个字符并将其作为摘录。但是,该代码适用于所有帖子类型,甚至是自定义帖子类型。

我如何仅将这个自定义摘录仅适用于博客帖子?我试图在下面的条件中包装下面的代码 - 这是默认的博客文章页面设置为 - 但没有用。

@api.multi
def write(self, vals):
    vals['flaeche'] = 37
    return super(models.Model, self).write(vals)

2 个答案:

答案 0 :(得分:1)

您可以查看$post->post_type

if ( $post->post_type !== 'post' ) {
    return $content;
}

global $post行之后添加此行。

答案 1 :(得分:-1)

    <?php 
    function custom_excerpts($content = false) {
    global $post;
    $content = wp_strip_all_tags($post->post_content);
    $excerpt_length = 35;
    $words = explode(' ', $content, $excerpt_length + 1);
 if(is_blog ()) :
    if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '...');
        $content = implode(' ', $words);
    endif;
    endif;
    $content = $content . '<br><br><a class="more-button" href="'.  get_permalink($post->ID) . '">Read More</a>';

    return $content;
}

add_filter('get_the_excerpt', 'custom_excerpts');
    function is_blog () {
        return ( is_archive() || is_author() || is_category() || is_home() || is_single() || is_tag()) && 'post' == get_post_type();
    }
    ?>

is_blog ()检查是否是页面博客,并根据该返回$content

相关问题