2

I am currently pulling in the post excerpt however there are 2 words at the start of it that I would rather not have in this particular place as there is a title already.

I have used wp_trim previously but this only takes words off the end, is there a way to do this for the first 2 words. These words are always the same if that helps? I'm not sure if I have get get the excerpt as a string then replace with nothing or if wp_trim can do this.

<?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();?>

Amended code from suggested answer from @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();?>
3

A more reliable way would be to filter the excerpt and to explode the string into an array, remove the first two key/value pairs from the array and then return your string

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;
}):
1
  • Please see my updated code ;-) – Pieter Goosen Jan 29 '16 at 7:06
2

preg_replace to the one-call rescue. /\w+/ will match words, while the third argument of preg_replace() will specify the number of matches. Since you want to remove them then we just pass an empty string as the replacement.

$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.

An alternative is to use substr with 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.
1

I used substr to remove the first 21 characters from the string in the end. This was more consistent.

$str = get_the_excerpt();
$str2 = substr($str, 21);
echo str2;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.