Döngü dışında the_excerpt () elde etmek için wp_trim_excerpt kullanma


20

Düzinelerce yayın için ana sayfada alıntılar gösterecek bir tema oluşturuyorum. Tüm yayınlarımda manuel alıntılar yok, bu yüzden $post->post_excerptbirçok yayın için boş. Manuel bir alıntı olmaması durumunda, yerleşik get_the_excerpt () işlevini kullanmak istiyorum, ancak döngü dışında kullanılamıyor.

İşlevi takip ederken, anında alıntılar oluşturmak için wp-include / formatting.php dosyasından wp_trim_excerpt kullanıyor gibi görünüyor. Kodum gibi çağırıyorum wp_trim_excerpt( $item->post_content ), ama sadece tam içeriği döndürüyor. Yanlış bir şey mi yapıyorum?

Bir alıntı oluşturmak için kendi fonksiyonumu yaratabileceğimi biliyorum, ancak kodumu diğer potansiyel eklentiler / filtrelerle uyumlu tutarak mümkün olduğunca yerleşik fonksiyonları kullanmayı seviyorum.

http://adambrown.info/p/wp_hooks/hook/wp_trim_excerpt?version=3.0&file=wp-includes/formatting.php


Alıntı filtreleri çağırmayı deneyebilirsiniz ...$myvar = apply_filters( 'the_excerpt', $myvar );
— t31os

Yanıtlar:



8

wp_trim_excerpt() biraz meraklı bir mekaniği var - eğer ona bir şey iletilirse hiçbir şey yapmaz.

Arkasındaki temel mantık:

  • get_the_excerpt() manuel alıntı kontrolleri;
  • wp_trim_excerpt() manuel bir alıntı yoksa ve bir içerik veya teaser yaparsa zil.

Her ikisi de küresel değişkenlere ve dolayısıyla Loop'a sıkı sıkıya bağlıdır.

Loop dışında, kodu wp_trim_excerpt()kendi süslemenizden çıkarmanız ve kendi kırpma işlevinizi yazmanız daha iyi olur .


6

Güncelleme:

Burada kullandığım wp_trim_excerpt () 'nin bir türevi. Mükemmel çalışıyor. Wordpress sürüm 3.0.4'ten türetilmiştir

function my_excerpt($text, $excerpt)
{
    if ($excerpt) return $excerpt;

    $text = strip_shortcodes( $text );

    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
    $text = strip_tags($text);
    $excerpt_length = apply_filters('excerpt_length', 55);
    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if ( count($words) > $excerpt_length ) {
            array_pop($words);
            $text = implode(' ', $words);
            $text = $text . $excerpt_more;
    } else {
            $text = implode(' ', $words);
    }

    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

Yeni bir yanıt göndermeniz gerekmez, eskisini her zaman yeni bilgiler içerecek şekilde düzenleyebilirsiniz. Örneğin, ilk cevabınızdan WP kodunun bağlantısını bu cevaba kopyalayabilir ve ardından ilk cevabınızı silebilirsiniz.
— Jan Fabry

Oradaki kopyalar / macunlar için: $ raw_excerpt = $ text;
— Svetoslav Marinov

1

Burada, yazı nesnesini veya posta kimliğini parametre olarak alan bir "trim_excerpt" ifadesini kullanıyorum.

Açıkçası özünde ne olduğuna bağlı. Bunun (ve get_the_author ()) döngü olmayan eşdeğerlerine sahip olmadığını bilmiyorum.

/**
     * Generates an excerpt from the content, if needed.
     *
     * @param int|object $post_or_id can be the post ID, or the actual $post object itself
     * @param string $excerpt_more the text that is applied to the end of the excerpt if we algorithically snip it
     * @return string the snipped excerpt or the manual excerpt if it exists         
     */
    function zg_trim_excerpt($post_or_id, $excerpt_more = ' [...]') {
        if ( is_object( $post_or_id ) ) $postObj = $post_or_id;
        else $postObj = get_post($post_or_id);

        $raw_excerpt = $text = $postObj->post_excerpt;
        if ( '' == $text ) {
            $text = $postObj->post_content;

            $text = strip_shortcodes( $text );

            $text = apply_filters('the_content', $text);
            $text = str_replace(']]>', ']]>', $text);
            $text = strip_tags($text);
            $excerpt_length = apply_filters('excerpt_length', 55);

            // don't automatically assume we will be using the global "read more" link provided by the theme
            // $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
            $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
            if ( count($words) > $excerpt_length ) {
                array_pop($words);
                $text = implode(' ', $words);
                $text = $text . $excerpt_more;
            } else {
                $text = implode(' ', $words);
            }
        }
        return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }

0

Rast için +1. Olması gerektiği oldukça açık olması gereken get_the_excerpt ($ post-> ID) diye bir şey olmaması çok garip. Her neyse, wordpress sürüm 3.0.4'te wp_trim_excerpt ():

http://core.trac.wordpress.org/browser/tags/3.0.4/wp-includes/formatting.php

function wp_trim_excerpt($text) {
1824            $raw_excerpt = $text;
1825            if ( '' == $text ) {
1826                    $text = get_the_content('');
1827    
1828                    $text = strip_shortcodes( $text );
1829    
1830                    $text = apply_filters('the_content', $text);
1831                    $text = str_replace(']]>', ']]>', $text);
1832                    $text = strip_tags($text);
1833                    $excerpt_length = apply_filters('excerpt_length', 55);
1834                    $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
1835                    $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
1836                    if ( count($words) > $excerpt_length ) {
1837                            array_pop($words);
1838                            $text = implode(' ', $words);
1839                            $text = $text . $excerpt_more;
1840                    } else {
1841                            $text = implode(' ', $words);
1842                    }
1843            }
1844            return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
1845    }

1826 satırında get_the_contents aracılığıyla $ post global değişkenine bağlı olduğunu görebilirsiniz. Ve evet, ne düşündüklerini bilmiyorum. Ancak buradan, get_the_content öğesini kendi my_excerpt öğenizde $ text ile değiştirin ve benzer bir şekilde davranması gerekir.


azure_ardee : wp_trim_words () kullanmayı düşünün

0

$ More! = 0 olursa get_the_content () işlevi tam içeriği döndürür. Get_the_content () işlevinin döndürüldüğünden emin olmak için $ global değişkenini 0'a ayarlamanız gerekir.

Değiştirilmiş wp_trim_excerpt () işlevi:

function wp_trim_excerpt($text) {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        global $more;
        $tmp = $more;
        $more = 0;
        $text = get_the_content('');
        $more = $tmp;

        $text = strip_shortcodes( $text );

        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
        $text = strip_tags($text);
        $excerpt_length = apply_filters('excerpt_length', 55);
        $excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
        $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
        if ( count($words) > $excerpt_length ) {
            array_pop($words);
            $text = implode(' ', $words);
            $text = $text . $excerpt_more;
        } else {
            $text = implode(' ', $words);
        }
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}

0

Yukarıda başkalarının cevaplarını kullanarak, iyi işleyen basit bir cevap:

global $post;

$excerpt = apply_filters('get_the_excerpt', get_post_field('post_excerpt', $post->ID));

if ( $excerpt == '' ) {
    $excerpt = wp_trim_words( $post->post_content, 55 );
}

Ben bunu kullanıyorum <meta>OpenGraph açıklamaları tanımlamak için bir işlevi olarak etiketlerin. O zaman sadece ekliyorum:

<meta property="og:description" content="<?php echo esc_html( $excerpt ); ?>" />

HTML içeriği ne olacak? Bu etiketlerle nasıl başa çıkacak? alıntı ayrıca html etiketlerini ve kısa kodları da şeritler. ya alıntıdaki ilk kelimeler bir resim içeriyorsa? Bu muhtemelen düzeninizi bozacaktır.
— brett
Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.