WordPress.org

Ready to get started?Download WordPress

Forums

Get_the_excerpt (1 post)

  1. Arterus
    Member
    Posted 1 year ago #

    Hello everyone,

    I need help with the get_the_excerpt function... However I don't really understand how it fits with my theme, and its custom code.

    I dont have a link as I'm working locally, sorry but here is the code that inserts a custom length excerpt on the nivo-slider in my theme.

    function excerpt($num) {
      $limit = $num+1;
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      array_pop($excerpt);
      $excerpt = implode(" ",$excerpt)."...";
      echo $excerpt;
    }

    This ordinarily would work fine, as it grabs the first 30 words of the post in question and then puts '...' after them and a button that says read more.

    However, my client wants just the name of the item to show (in this case they are cupcakes) such as:

    Vintage Cupcakes
    Christening cupcakes

    etc..

    If I change:

    $excerpt = explode(' ', get_the_excerpt(), $limit );

    to

    $excerpt = explode(' ', get_the_excerpt(), $limit -28);

    I can get just the two words to show... Great...

    But what about when the cupcake name is:

    Baby Shower Cupcakes - 3 words... -28 no longer works.

    Can someone help me rewrite this code to include the first LINE of the post? or is that not possible? or am I barking up the wrong tree entirely??

    I am fairly new to all this, just getting to the right php code took me an entire day, lol so please go gentle. :)

    Luckily the 'client' is a friend :)

    Thanks in advance all.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.