• Hi,

    I’m trying to add manually my latest posts on my homepage and customize their excerpt link (“continue reading”)

    I did it using :

    <?php
     $postslist = get_posts('numberposts=2&order=DESC&orderby=date');
     foreach ($postslist as $post) :
        setup_postdata($post);
     ?>

    adding thumbnails and metas and, finally, using <?php the_excerpt(); ?>.

    Ideally, I’d like the last link to be customized exactly as it is in my category page, not made by me, (basically a block text with a border), so I had a look at it to see what happens there and I found a couple of things:

    1) My category template uses <?php if (have_posts()) : while (have_posts()) : the_post(); ?> which is normal, I think, and

    2) A function was added to functions.php:

    add_filter('excerpt_more', 'custom_excerpt_more');
    	// no more jumping for read more link
    	function no_more_jumping($post) {
    		return '...<a href="'.get_permalink($post->ID).'" class="read-more">'.'Continue Reading'.'</a>';
    	}

    which adds to it the “read-more” class which customizes it.

    How can I apply this category also to my homepage excerpt?

    Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter Dancar11

    (@dancar11)

    I mean “apply this class”, not “this category” of course.

    Hi Dancar11,

    there seems to be some problem in your code, function name should be ‘custom_excerpt_more’ and not ‘no_more_jumping’ as per add_filter call.
    Also, i have tried get_posts with the_excerpt in test template (using 2012 theme), its working fine with this code below

    functions.php

    function new_excerpt_more( $more ) {
    	return ' <a class="read-more" href="' . get_permalink( get_the_ID() ) . '">' . __( 'Read More', 'your-text-domain' ) . '</a>';
    }
    add_filter( 'excerpt_more', 'new_excerpt_more' );

    Test template code

    <?php
    /*
    * Template Name: Test Template
    */
    
    get_header();
    
    $postslist = get_posts('numberposts=2&order=DESC&orderby=date');
     foreach ($postslist as $post) :
        setup_postdata($post);
    
    echo '<div>';
    echo '<h2>'.get_the_title().'</h2>';
    the_excerpt();
    echo '</div>';
    
    endforeach;
    
    get_footer();
    
    ?>

    Let me know if it helps !

    Thread Starter Dancar11

    (@dancar11)

    That easily worked!

    Thank you very much ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Customize the_excerpt link’ is closed to new replies.