web-dev-qa-db-fra.com

Coupez les 2 premiers mots de l'extrait

Je suis en train d'extraire l'extrait de l'article, mais il y a 2 mots au début que je préférerais ne pas avoir à cet endroit particulier, car il y a déjà un titre.

J'ai déjà utilisé wp_trim mais cela ne prend que des mots, existe-t-il un moyen de le faire pour les 2 premiers mots? Ces mots sont toujours les mêmes si cela aide? Je ne sais pas si je vais obtenir l'extrait sous forme de chaîne puis le remplacer par rien ou si wp_trim peut le faire.

<?php $tagname = get_the_title (); ?>
<?php

$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>1, 
    'orderby' => 'Rand',
    'tag' => sluggify( $tagname));
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
    while (have_posts()) : the_post();
        echo '<h2 class="entry-title">';
        echo 'CASE STUDY';
        echo '</h2>';
        echo '<span>';
        the_post_thumbnail();
        echo '</span>';
        echo '<strong>';
        the_title();
        echo '</strong>';
        echo '<p>';
        the_excerpt();
        echo '</p>';
    endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();?>

Code modifié de la réponse suggérée de @RRikesh:

<?php $tagname = get_the_title (); ?>
<?php

$original_query = $wp_query;
$wp_query = null;
$args=array('posts_per_page'=>1, 
    'orderby' => 'Rand',
    'tag' => sluggify( $tagname));
$wp_query = new WP_Query( $args );
if ( have_posts() ) :
    while (have_posts()) : the_post();
    $str = get_the_excerpt();

        echo '<h2 class="entry-title">';
        echo 'CASE STUDY';
        echo '</h2>';
        echo '<span>';
        the_post_thumbnail();
        echo '</span>';
        echo '<strong>';
        the_title();
        echo '</strong>';
        echo '<p>';
        echo ltrim($str, "INSTRUCTION SYNOPSIS"); // Output: This is another Hello World.
        echo '</p>';
    endwhile;
endif;
$wp_query = null;
$wp_query = $original_query;
wp_reset_postdata();?>
2
Ben H

J'ai utilisé substr pour supprimer les 21 premiers caractères de la chaîne. C'était plus cohérent.

$str = get_the_excerpt();
$str2 = substr($str, 21);
echo str2;
1
Ben H

Un moyen plus fiable serait de filtrer l’extrait et d’exploser la chaîne dans un tableau, supprimez les deux premières paires clé/valeur du tableau, puis renvoyez votre chaîne.

add_filter( 'wp_trim_excerpt', function ( $text )
{
    // Make sure we have a text
    if ( !$text )
        return $text;

    $text               = ltrim( $text );
    $text_as_array      = explode( ' ', $text );

    // Make sure we have at least X amount of words as an array
    if ( 10 > count( $text_as_array ) )
        return $text;

    $text_array_to_keep = array_slice( $text_as_array, 2 );
    $text_as_string     = implode( ' ', $text_array_to_keep );
    $text               = $text_as_string;

    return $text;
}):
3
Pieter Goosen

preg_replace au sauvetage en un appel. /\w+/ correspondra aux mots, tandis que le troisième argument de preg_replace() spécifiera le nombre de correspondances. Puisque vous voulez les supprimer, nous passons simplement une chaîne vide en remplacement.

$str = 'These are some words. But the first two will not remain.';

// pattern, replacement, string, limit

echo preg_replace( '/\w+/', '', $str, 2 );

// output: some words. But the first 2 will not remain.

Une alternative consiste à utiliser substr avec strpos .

// reduce the extra whitespace

$str = trim( "   This is some text and stuff.  " );

// find the second space and pull everything after

echo trim( substr( $str, strpos( $str, ' ', strpos( $str, ' ' ) + 1 ) ) );

// output: some text and stuff.
2
jgraup