Obtenga un extracto usando get_the_excerpt fuera de un bucle

30

Tengo un código que llama a get_the_title() y funciona, pero get_the_excerpt() devuelve vacío. ¿Cómo puedo hacer que funcione?

Este código está dentro de un complemento llamado "protocolo WP Facebook Open Graph". Aquí está la parte que quiero cambiar:

if (is_singular('post')) {
  if (has_excerpt($post->ID)) {
    echo "\t<meta property='og:description' content='".esc_attr(strip_tags(get_the_excerpt($post->ID)))."' />\n";
  }else{
    echo "\t<meta property='og:description' content='". [?] ."' />\n";
  }
}else{
  echo "\t<meta property='og:description' content='".get_bloginfo('description')."' />\n";
}

Aquí, has_excerpt siempre falla, y get_the_excerpt($post->ID) ya no funciona (en desuso).

Entonces, ¿cómo puedo mostrar el extracto allí?

ps: También estoy usando el complemento "Extracto avanzado"

    
pregunta ariel 24.08.2011 - 01:55

9 respuestas

5

lo obtuve usando my_excerpt($post->post_content, get_the_excerpt()) y usando la función my_excerpt() de Uso de wp_trim_excerpt para obtener the_excerpt () fuera del bucle

    
respondido por el ariel 13.09.2011 - 07:44
26

Encontré esta pregunta al buscar cómo hacer esto sin el objeto de publicación.

Mi investigación adicional reveló esta técnica ingeniosa:

$text = apply_filters('the_excerpt', get_post_field('post_excerpt', $post_id));

    
respondido por el cale_b 18.06.2014 - 17:33
21

Como parece que ya tienes el objeto para el que necesitas el extracto, puedes forzar que las cosas funcionen:

setup_postdata( $post );
$excerpt = get_the_excerpt();

La función setup_postdata() globalizará el objeto $post y lo hará disponible para la función de bucle antiguo normal. Cuando estás dentro del bucle, llamas a the_post() y configura las cosas para ti ... fuera del bucle que necesitas para forzarlo manualmente.

    
respondido por el EAMann 09.06.2012 - 01:06
18

Prueba esto:

Crea una nueva función en functions.php y luego llámala desde cualquier lugar.

function get_excerpt_by_id($post_id){
    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = $the_post->post_content; //Gets post_content to be used as a basis for the excerpt
    $excerpt_length = 35; //Sets excerpt length by word count
    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images
    $words = explode(' ', $the_excerpt, $excerpt_length + 1);

    if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '…');
        $the_excerpt = implode(' ', $words);
    endif;

    $the_excerpt = '<p>' . $the_excerpt . '</p>';

    return $the_excerpt;
}

Aquí hay una post describiendo el código.

    
respondido por el Withers Davis 09.06.2012 - 01:02
9

Ahora puede simplemente utilizar la función get_the_excerpt( $postID ) . Desde: WordPress 4.5.0 introdujo el parámetro $post .

    
respondido por el docker 20.05.2016 - 13:07
1

En caso de que no tengas el objeto post, aquí hay una función corta como la de Withers.

function get_excerpt_by_id($post_id){
    $the_post = get_post($post_id);
    $the_excerpt = $the_post->post_excerpt; 
    return $the_excerpt;
}
    
respondido por el OKParrothead 25.11.2012 - 20:28
1

Esto es para cuando quieres usar get_the_excerpt() fuera del bucle:

function custom_get_excerpt($post_id) {
    $temp = $post;
    $post = get_post($post_id);
    setup_postdata($post);

    $excerpt = get_the_excerpt();

    wp_reset_postdata();
    $post = $temp;

    return $excerpt;
}
    
respondido por el Gixty 01.10.2014 - 01:17
0

Si desea generar el extracto automáticamente del contenido en una línea, puede usar wp_trim_words funciona así:

// 30 is the number of words ehere
$excerpt = wp_trim_words(get_post_field('post_content', $post_id), 30);
    
respondido por el Picard 15.05.2017 - 20:25
-1
$trimexcerpt = get_the_content();
$shortexcerpt = wp_trim_words( $trimexcerpt, $num_words = 18, $more = '… ' ); 
echo $shortexcerpt;
    
respondido por el Rinzler 14.03.2018 - 17:53

Lea otras preguntas en las etiquetas