0

I'm starting a reviews website and at the moment I have, as standard, my post excerpts on the main index page.

I was wondering if anyone knows of a way in which I can add a link (preferably a button) at the bottom of each post excerpt on the index page in which the viewer can go straight to the site I'm reviewing, without reading the post?

I'd like the post heading to go to my article, but a button at the bottom to go to said website that's being reviews, saying something along the lines of "Go to Site"

For example:

  • Site 1 Name: Review
  • Post excerpt here...read more
  • External Link to site 1 (as a button).

  • Site 2 Name: Review

  • Post excerpt here...read more
  • External Link to site 2 (as a button).

If it can be done by plugin, then I'm OK with that. I'm also OK with editing the code of my site should that be the only way to achieve this.

Theme is Codilight-lite if this helps in any way, shape or form.

If anyone can help, that would be amazing.

Many thanks

7
  • Add a custom meta box so you're adding the external URL directly in the post editor as postmeta. Then you can filter the_excerpt to include that postmeta as a link.
    – WebElaine
    Commented Jun 2, 2017 at 21:28
  • I like this answer. After a few occurrences of the white screen of death, I've added a custom meta box in the Edit Post area and added the code to save the meta date. However, please could you advise how to filter the_excerpt?
    – alfieph
    Commented Jun 2, 2017 at 22:11
  • You may get an actual answer outlining what @WebElaine has suggested, it's the technique many WP developers would use if they are comfortable with building their own plugins and themes (I'm too lazy right now to do it myself). You'll probably find it much easier to use Advanced Custom Fields (ACF) though. It will allow you to add a field to any post where you can fill in the URL for the site you are reviewing, and then use a small bit of php to output that field in your template.
    – totels
    Commented Jun 2, 2017 at 22:13
  • add_filter( 'the_excerpt', function ( $excerpt ) { return $excerpt . " more stuff for the excerpt." } );
    – totels
    Commented Jun 2, 2017 at 22:20
  • Thanks! Took me a while to figure out exactly where to add the additional code, but I found it eventually. I've added plain text at the moment, but will have a look at adding it in as a button. I'll report back!
    – alfieph
    Commented Jun 2, 2017 at 23:13

1 Answer 1

1

The Codilight theme is bypassing the usual the_excerpt with their own version:

inc/extras.php +26:

if ( ! function_exists( 'codilight_lite_excerpt' ) ) :
  /**
   * Get the except content limit by characters.
   *
   * @param string $characters
   * @return string
   */
  function codilight_lite_excerpt( $characters ){
    // $characters = 160;
    $excerpt = get_the_content();
    $excerpt = preg_replace(" (\[.*?\])",'',$excerpt);
    $excerpt = strip_shortcodes($excerpt);
    $excerpt = strip_tags($excerpt);
    $excerpt = substr($excerpt, 0, $characters);
    $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
    $excerpt = trim(preg_replace( '/\s+/', ' ', $excerpt));
    $excerpt = $excerpt.'...';
    return '<div class="ft-excerpt">'. $excerpt .'</div>';
  }
endif;

And on top of that they aren't even using "proper" methods of modifying via actions or filters to make it easy for child themes. Instead they've wrapped their function with a clause to check whether you've written your own, which allows you to override theirs. So in your functions.php you'll need to basically copy their function and add your own code as needed.

add to your-child-theme/functions.php:

  /**
   * override the parent theme excerpt
   *
   * @param string $characters
   * @return string
   */
  function codilight_lite_excerpt( $characters ){
    $excerpt = get_the_content();
    $excerpt = preg_replace( " (\[.*?\])", '', $excerpt );
    $excerpt = strip_shortcodes( $excerpt );
    $excerpt = strip_tags( $excerpt );
    $excerpt = substr( $excerpt, 0, $characters );
    $excerpt = substr( $excerpt, 0, strripos( $excerpt, " " ) );
    $excerpt = trim( preg_replace( '/\s+/', ' ', $excerpt ) );
    $excerpt = $excerpt . '...';
    $excerpt .= "<br><button>Visit Site</button>";
    return '<div class="ft-excerpt">' . $excerpt . '</div>';
  }

Fetching the metadata and building the actual button code I will leave upto you.

Your Answer

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

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