2016-11-21 5 views
2

Я пытаюсь добавить этот пользовательский отрывок в сообщения в блоге, чтобы он захватывал первые 35 символов из сообщений и делал это выдержкой. Однако код работает для всех типов сообщений, даже для настраиваемых типов сообщений.Wordpress Custom Excerpt для записей в блоге

Как только этот пользовательский отрывок применяется только к сообщениям в блоге? Я попытался обернуть код ниже в следующем условном выражении - это то, на что установлена ​​страница по умолчанию для Blog 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:

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

Добавьте эту строку после global $post линии.

+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

это будет работать так же –

Смежные вопросы