• Hi!

    I use “the_excerpt” instead of “the_content” within my “search.php”.
    In case the excerpt is automatically generated, I get a “… more”-link beneath it. But not so when using manually created excerpts.
    No “…more”-link in this case.

    Any idea how to add it automatically to any form of excerpt?

    thx,
    p.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Try putting this in your theme’s functions.php file. It replaces […] with a read more link for both automatic and manual excerpts. If you don’t want read more links, post back & I’ll correct for that.

    function custom_excerpt($text) {  // custom 'read more' link
       if (strpos($text, '[...]')) {
          $excerpt = strip_tags(str_replace('[...]', '... <a href="'.get_permalink().'">read more...</a>', $text), "<a>");
       } else {
          $excerpt = '<p>' . $text . '&nbsp;&nbsp;<a href="'.get_permalink().'">read more...</a></p>';
       }
       return $excerpt;
    }
    add_filter('the_excerpt', 'custom_excerpt');
    ?>
    Thread Starter YoChen

    (@piedro)

    Hi stvwlf!

    thx for helping out!

    Yes, your code works. The thing is I used another piece of code to include a “… more” in the excerpts. And I would really like to have this “…more” link within the content of the expert and not below it. I tried to get both pieces of code together – and messed it all up.

    Here’s both parts:

    function link_hellip_in_excerpt( $content ) {
                                  global $post_ID;
                       return preg_replace( '~\[\.\.\.\]$~', ' ... <a href="' . get_permalink( $post_ID ) . '">more</a>', $content);
                      }
                add_filter( 'the_excerpt', 'link_hellip_in_excerpt', 9);
                add_filter( 'the_content', 'link_hellip_in_excerpt', 9);
    
               function excerpt($num)
                      {
                      $limit = $num+1;
                      $excerpt = explode(’‘, get_the_excerpt(), $limit);
                      array_pop($excerpt);
                      $excerpt = implode(”“,$excerpt).”…”;
                      echo $excerpt;
                      }

    and

    function custom_excerpt($text)
                      {  // custom 'read more' link
                      if (strpos($text, '[...]')) {
                      $excerpt = strip_tags(str_replace('[...]', '...&nbsp;<a href="'.get_permalink().'">more</a>', $text), "<a>");
                      } else {
                      $excerpt = '' . $text . '...&nbsp;<a href="'.get_permalink().'">more</a>';
                      }
                      return $excerpt;
                      }
              add_filter('the_excerpt', 'custom_excerpt');

    My guess was that I should insert your custom_exerpt in the upper code … but I just don’t know …

    Any ideas?

    thx,
    piedro

    Hi

    I’m not exactly clear on what you mean by ‘I would really like to have this “…more” link within the content of the excerpt and not below it’

    Can you take a screenshot of the way your page looks with the link_hellip_in_excerpt in place, and then another of how it looks with the code I sent in place? You can either post them on the web on a photo site & post links here, or else email to me at the email address on my website on my profile link.

    I can make my code look however you want – just need to know how you want it to look – let me know also which excerpts on the page are from manual excerpts and which from automatic

    Thread Starter YoChen

    (@piedro)

    Hi stvwlf!

    Have a look at this page:
    (It’s a page for my chinese friends expanding on chinese arts in germany ?? , – it’s work in progress and far from complete but I think there should be stilll no harm in posting this link here)

    https://www.lebenspflege.de//?s=test

    This is a sample fulltext search.
    As you see the “more” – link uses your technique, but I like it more to look like the “mehr”-link (well that’s german for “more” as you’d guess …). The “more” should look like the “mehr” in manually written excerpts, automatically (cut after xy words) excerpts and the excerpts generated by using the tag <more> when writing an article or page …

    Additionally it’s not only used on the search.php but in all kinds of pages, – like in

    https://www.lebenspflege.de/tradition/ueber-uns/

    I hope this is better than a screenshot.

    Thx for your effort looking into it.

    All the best,
    piedro

    Try this revised version of what I sent you

    function custom_excerpt($text) {  // custom 'read more' link
       if (strpos($text, '[...]')) {
          $excerpt = strip_tags(str_replace('[...]', '...&nbsp;<a href="'.get_permalink().'">more</a>', $text), "<a>");
       } else {
          $excerpt = '' . strip_tags($text)) . '...&nbsp;<a href="'.get_permalink().'">more</a>';
       }
       return $excerpt;
    }
    add_filter('the_excerpt', 'custom_excerpt');

    Might not be all the way there yet. should be closer, at least.

    Thread Starter YoChen

    (@piedro)

    hi stvwlf,

    Using this I get a
    Parse error: syntax error, unexpected ‘)’

    $excerpt = '' . strip_tags($text)) . '...&nbsp;<a href="'.get_permalink().'">more</a>';

    has to be

    $excerpt = '' . strip_tags($text) . '...&nbsp;<a href="'.get_permalink().'">more</a>';

    I guess …

    Alltogether this works fine. Only one thing is annoying:
    Stripping the tags completely of the $test seems to let the
    css-styling fail.

    Is it possible to do that without stripping the tags completely?

    thx,
    piedro

    the error is $excerpt = '' . strip_tags($text)) . '...&nbsp;<a
    – double closing )) on strip_tags

    At first I did not have strip_tags which is why the More was 2 lines below the text, because it appeared after a P tag closed, and spaced everything down two lines.

    What CSS is failing with the strip_tags in place? Strip_tags can be set to allow certain tags to remain. Need to know which. (The original problem was there was a lot of bottom margin below your paragraphs.)

    Thread Starter YoChen

    (@piedro)

    hi stvwlf!

    Sorry for leaving the topic for 2 weeks now.
    Had some other troubles in between.

    To the subject: It was just an obviously unformatted line-height – easy to solve bey adding a global line-height to the body-tag.

    But still good to know that it’s possible to completely control the stripping of tags individually. (looked it up myself also …)

    Anyways thx so much for your piece of code,
    that was a lot of help to me and a lot new to learn!

    Have a good time,
    piedro

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘add “… more” to manually created excerpts’ is closed to new replies.