2016-11-21 21 views
2

私はブログの投稿にこのカスタム抜粋を追加しようとしているので、投稿から最初の35文字を取得し、その抜粋を作成します。しかし、このコードはすべてのPost Types、カスタムPostタイプでも機能します。ブログの投稿のWordpressのカスタム抜粋

このカスタム抜粋のみをブログ投稿にのみ適用するにはどうすればよいですか?私は以下の条件で、以下のコードをラップしようとしました - これはデフォルトのブログ投稿ページが設定されていますが、動作しませんでした。

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

global $post行の後に次の行を追加します。

if(is_page_template('template-blog-list.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(count($words) > $excerpt_length) : 
      array_pop($words); 
      array_push($words, '...'); 
      $content = implode(' ', $words); 
     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'); 
} 

答えて

1

は、あなただけの$post->post_typeを確認することができます。

+0

非常に素晴らしい戻る - 短いと甘いです。ありがとう。 – optimus203

-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

+0

これは同じになります –

関連する問題