• mixdmatt

    (@mixdmatt)


    Hey all

    I’ve got a search results page on which I’m outputting the title of the post/page with an excerpt below.

    At present I’m using this function to trim the_excerpt down to 250 characters and follow with a ‘…’ :

    <?php $little_excerpt = trim(substr(strip_tags(get_the_excerpt()),0,250))."..."; ?>
    <?php echo $little_excerpt; ?>

    This works, even if there is no excerpt specified for the post (WP generates it automatically am I right?)

    The problem I’m having is that all my post content entries start with <h1>Page title</h1> so when my excerpt is output I get something like this:

    Expertise In Action Our flexible, professional training has produced great results for diverse organisations

    Because my post content looks like this
    <h1>Expertise In Action</h1><p>Our flexible, professional training has produced great results for diverse organisations</p>

    As the h1 and page title are the same in most cases what I’m trying to do is strip out the <h1> and output everything past this i.e.

    Our flexible, professional training has produced great results for diverse organisations

    I’ve tried using PHP preg_replace such as this

    <?php $little_excerpt = trim(substr(strip_tags(preg_replace("<h1>.*?<\/h1>","",get_the_excerpt())),0,200))."..."; ?>

    But I get an Unknown modifier ‘.’ error.

    Can anyone provide me with a function which can achieve what I’m after?

    Thanks in advance
    Matt

Viewing 1 replies (of 1 total)
  • Thread Starter mixdmatt

    (@mixdmatt)

    I have figured it out now. get_the_excerpt strips out HTML tags anyway so the Reg-ex was looking for a <h1> that wasn’t there

    I used this in the end

    <?php $little_excerpt = trim(substr(strip_tags(preg_replace("/(<h1>.*?<\/h1>|\[.*\])/m","",get_the_content())),0,200))."..."; ?>

    Which also strips out any shortcodes ??

Viewing 1 replies (of 1 total)
  • The topic ‘Search results – Remove <h1>’ is closed to new replies.