How to remove first three words from content and display the excerpt

Hi I am making a video game site. I can display the excerpt, shortened it as well after following some advice. What I can't do is remove the first three words from the content and then display it as excerpt . eg if the content is ...

"the content starts this is the excerpt" after stripping the html tags

so the_excerpt() should only display "this is the excerpt"

The site

I just want to remove description and instructions from each category display, of which I have a custom template..

Topic excerpt Wordpress

Category Web


The following is what you are looking for. Place it in your functions.php file making appropriate changes.

function custom_excerpt() {
    $text = get_the_excerpt(); //Assigns the excerpt to $text
    $text = str_replace("Word1_to_replace","", $text); // replacing the word with empty string
    $text = str_replace("Word2_to_replace","", $text);
    $text = str_replace("Word3_to_replace","", $text);
    return $text;
}
add_filter('the_excerpt', 'custom_excerpt');

Not really the most efficient way but this should work:

function the_excerpt1($s='')
{
    if($s !== '')
    {
        $a = explode(' ', $s);
        array_shift($a);
        array_shift($a);
        array_shift($a);
        $s = implode(' ', $a);
        unset($a);
    }

    return $s;
}

echo the_excerpt1('the content starts this is the excerpt');

Update - A more efficient way

function the_excerpt2($s='')
{
    if($s !== '')
    {
        $s = substr($s, (strpos($s, ' ')+1));
        $s = substr($s, (strpos($s, ' ')+1));
        $s = substr($s, (strpos($s, ' ')+1));
    }

    return $s;
}

echo the_excerpt2('the content starts this is the excerpt');

the_excerpt2() will execute 50-66% faster than the_excerpt1()


Here's a quick way to do it, assuming $excerpt has your excerpt you want to remove the first 3 words from

$excerpt = "the content starts this is the excerpt";
$words = explode(' ', $excerpt);
array_shift($words); // 1st word
array_shift($words); // 2nd word
array_shift($words); // 3rd word
$excerpt = implode(' ', $words);

It splits the excerpt into an array of words based on the space ' ', shifts the first 3 words off the array and recombines it back into a string.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.