• Resolved DanSte

    (@danste)


    Hello,

    How do I go about allowing links to show in the blog posts excerpt that is shown on the homepage?

    thanks,

Viewing 12 replies - 1 through 12 (of 12 total)
  • Hello @danste,
    To allow links to show blog posts excerpt in the homepage, there is a section called ‘Blog’ in Customizer; Appearance > Customizer > Theme Options > Blog. There you can set Layout as ‘Excerpt’ and set the length of the words that will be appearing before readmore link. Also, set your front-page to show ‘latest-posts’ & you will achieve it.
    Hope that helps resolving your issue.
    Thanks!

    Regards,

    Thread Starter DanSte

    (@danste)

    Hi,

    Thanks for the quick response.

    I have done the above and that creates a “read more” link, but I want to show a link within the excerpt text that is clickable from the homepage excerpt.

    Thanks

    Hello @danste,
    Put this code in functions.php of your child theme and see if it works. If not, kindly post your site URL.

    function keep_my_links($text) {
      global $post;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace('\]\]\>', ']]>', $text);
        $text = preg_replace('@<script[^>]*?>.*?</script>@si', '', $text);
        $text = strip_tags($text, '<a>');
      }
      return $text;
    }
    //remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'keep_my_links');

    Thanks for your time.

    regards,

    Thread Starter DanSte

    (@danste)

    Hi,

    Should I not replace something with that?

    Where to put the code exactly as I didn’t make any difference.

    Thanks

    You just put that code into your functions.php that is inside your theme folder no need to replace the existing code.

    Thread Starter DanSte

    (@danste)

    I’ve pasted it in but hasn’t made any difference…

    Thanks

    Hi DanSte

    Considering your have added widget in Front Page Widget Area.
    Copy and paste below code in functions.php in child theme’s folder.

    function wen_corporate_the_excerpt( $length = 40, $post_obj = null ) {
    
        global $post;
        if ( is_null( $post_obj ) ) {
          $post_obj = $post;
        }
        $length = absint( $length );
        if ( $length < 1 ) {
          $length = 40;
        }
        $source_content = get_the_excerpt();
        if ( ! empty( $post_obj->post_excerpt ) ) {
          $source_content = $post_obj->post_excerpt;
        }
        $source_content = preg_replace( '<code>\[[^\]]*\]</code>', '', $source_content );
    
        return $source_content;
    
      }
    
    function custom_wp_trim_excerpt($text) {
    $raw_excerpt = $text;
    if ( '' == $text ) {
        //Retrieve the post content.
        $text = get_the_content('');
    
        //Delete all shortcode tags from the content.
        $text = strip_shortcodes( $text );
    
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    
        $allowed_tags = '<a>,</a>'; /*** MODIFY THIS. Add the allowed HTML tags separated by a comma.***/
        $text = strip_tags($text, $allowed_tags);
    
        $excerpt_word_count = 55; /*** MODIFY THIS. change the excerpt word count to any integer you like.***/
        $excerpt_length = apply_filters('excerpt_length', $excerpt_word_count); 
    
        $excerpt_end = '[...]'; /*** MODIFY THIS. change the excerpt endind to something else.***/
        $excerpt_more = apply_filters('excerpt_more', ' ' . $excerpt_end);
    
        $words = preg_split("/[\n\r\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
        if ( count($words) > $excerpt_length ) {
            array_pop($words);
            $text = implode(' ', $words);
            $text = $text . $excerpt_more;
        } else {
            $text = implode(' ', $words);
        }
    }
    return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
    }
    remove_filter('get_the_excerpt', 'wp_trim_excerpt');
    add_filter('get_the_excerpt', 'custom_wp_trim_excerpt');

    And you can control excerpt length to display in front page by going to Admin Panel -> Appearance -> Customize -> Theme Options -> Blog -> Excerpt Length.
    Hope this solution works for you.

    Best Regards!!!

    Thread Starter DanSte

    (@danste)

    Hi,

    Yeah the widget is added in the Front Page Widget Area and now shows this after changing the functions.php file:

    Warning: preg_replace(): Unknown modifier ‘\’ in /home/public_html………

    line 39 is:

    $source_content = preg_replace( ‘\[[^\]]*\]‘, ”, $source_content );

    Thanks again

    Thread Starter DanSte

    (@danste)

    sorry, this:

    line 39 is:

    $source_content = preg_replace( '<code>\[[^\]]*\]</code>', '', $source_content );

    Hi DanSte

    Sorry for the inconvenience.
    Just replace the line of code with error with line shown as in this screenshot.Cause the editor is not rendering some symbols.Look carefully in image and write the line of code manually.

    https://www.dropbox.com/s/90pspasnhlb6hm4/code.png?dl=0

    Hope this one works.Let us know if any problem.

    Best Regards!!!

    Thread Starter DanSte

    (@danste)

    Spot on! Thanks very much for this ??

    Hi DanSte

    We are very happy to hear that your problem is solved.

    Best Regards
    WEN Solutions
    CHEERS!!!

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Show links in blog post excerpts’ is closed to new replies.