• I’m trying to figure out what I need to do to control the behaviour of the Excerpt in two separate locations, the first on my website’s non-wp home page and the other on the wp/blog section index page.
    On my site’s index page I wish to display just an image from the excerpt, to achieve this I pasted an image link (such as <img src=”xxx”> etc) into the excerpt field when writing the post.
    In the non-wp page code I have the following:

    <?php
      $myExcerpt = get_the_excerpt();
      $tags = array("<p>", "</p>");
      $myExcerpt = str_replace($tags, "", $myExcerpt);
      echo $myExcerpt;
    ?>

    This ensures I can control the output in <p> tags.

    On the index page of the blog section I want just some text (not an image) as an excerpt from the text of the post. I did have the standard get_excerpt code to do this but this just brought up the image and nothing else since that was in the post’s excerpt field box, I then added the following to strip the <img> tags:

    <?php
      $excerpt = get_the_excerpt();
      $postOutput = preg_replace('/<img[^>]+./','', $excerpt);
      echo $postOutput;
    ?>

    Which works but if I then add text to the excerpt field box it then appears in both locations, there is no ‘read more’ link and I can’t seem to specify a cut off (custom length in functions.php doesn’t work).
    I may have gone about it the wrong way on the blog index page. Is there a way to display just the text of the post content when the excerpt field has an image link in there? If not what code should I use to limit the length of get_content as a ‘hacked’ excerpt as it were and still allow my website index page to retrieve the image in the excerpt field?
    If I use a hacked get_content then I’d need to strip any images from that as I tend to begin each post with an image…

  • The topic ‘Excerpt on non-wp home page vs excerpt on blog index’ is closed to new replies.