1

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

3 Answers 3

0

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.

1
  • ok I will try this . but will take me some time.
    – A Kashyap
    Oct 14, 2015 at 14:32
0

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

1
  • ok I will try this . but will take me some time.
    – A Kashyap
    Oct 14, 2015 at 14:32
0

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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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