2011-03-15 17 views
5

WP'den son üç yayını almak için The Loop'u kendi web sayfamda kullanıyorum.Bir wordpress yayınından sınırlı, salt metin alıntı mı aldınız?

<?php 
     $args = array('numberposts' => 3); 
     $myposts = get_posts($args); 
     foreach($myposts as $post) : setup_postdata($post); ?> 

        <!-- DATE --> 
        <div class="date"> 
        <?php the_time('m F Y');?> 
        </div> 

        <!-- TITLE --> 
        <div class="title"> 
        <?php the_title(); ?> 
        </div> 

        <!-- SNIPPET --> 
        <div class="content"> 
        <?php the_excerpt(); ?> 
        </div> 
<?php endforeach; ?> 

Her şey yolunda çalışıyor - the_excerpt() hariç. Bir önizleme olarak gösterilecek yazıdan yaklaşık 15-20 kelimelik düz metne ihtiyacım var. Bunu yapmayı nasıl yaparım?

Teşekkür :)

cevap

4

Sen hiçbir alıntı varsa yazının ilk 20 kelime kapmak için böyle bir şey kullanarak deneyebilirsiniz.

$content = get_the_content(); 
echo substr($content, 0, 20); 
+0

Maalesef bu burada kelimeleri istedi [buradan] bunu yapmak için nasıl bir işlev ise, değil açıkça düşünme wasnt sözcükleri karakterleri kapmak etmektir (http://www.nutt.net/2004/12/29/php-a-function-to-return-the-first-n-words-from-a-string/) – Tianbo84

2

bu deneyin:

Mesaj görüntülerini içerir:

$content = get_the_content(); 
$content = apply_filters('the_content', $content); 
$content = str_replace(']]>',']]&gt;', $content); 
echo substr(strip_tags($content),0,100); 

ve olmayan görüntüleri:

$content = get_the_content(); 
echo substr($content, 0, 25); 
9

kaçının kullanarak substr. Niye ya?

substr() kullanırsanız, bu HTML etiket (ler) ini kesebilir ve hatalı biçimlendirilmiş HTML'ye dönüştürebilir.

Tekerleği yeniden icat etmeyin!

WordPress 3.3'ün wp_trim_words() adlı yeni bir çekirdek işlevi vardır.

wp_trim_words Parametre Molası Aşağı:

wp_trim_words($text, $num_words, $more); 

$text 
    (string) (required) Text to trim 
    Default: None 

$num_words 
    (integer) (optional) Number of words 
    Default: 55 

$more 
    (string) (optional) What to append if $text needs to be trimmed. 
    Default: '…' 

Örnek kullanımları:

<?php echo wp_trim_words(get_the_content(), 40, '...'); ?> 

<?php echo wp_trim_words(get_the_excerpt(), 20, '... read more...'); ?> 
0

functions.php Bu kodu koy

function new_excerpt_length($length) { 
    return 20;} 
add_filter('excerpt_length', 'new_excerpt_length'); 

Ve sadece şablon sayfası veya index.php dosyasından bu işlevi çağırmak

the_excerpt(); 
İlgili konular