• Hey,
    I have implemented this code into my functions.php file

    function new_excerpt_more($more) {
           global $post;
    	return '<a>ID) . '"> read more...</a>';
    }
    
    add_filter('excerpt_more', 'new_excerpt_more', 99);

    , because i wanted to add read more for my posts on my home page. but it seems that it works only for some posts, and not for all of them.
    Do you know why maybe?

    • This topic was modified 3 years, 10 months ago by yda5.
    • This topic was modified 3 years, 10 months ago by yda5.

    The page I need help with: [log in to see the link]

Viewing 12 replies - 1 through 12 (of 12 total)
  • The “more” is only added if the text is longer than the ‘excerpt_length’.

    Without commenting on why you need to do this ………

    Your code above shouldn’t be working at all.
    You aren’t using a link to the a anywhere – it should be more like this:

    function new_excerpt_more($more) {
    	global $post;
    	return '<a href="' . $post-->ID . "' . > read more...</a>';
    }
    add_filter('excerpt_more', 'new_excerpt_more', 99);
    Thread Starter yda5

    (@yda5)

    So how can i extend it to work for all of the posts? @joyously

    The code the you sent, doesn’t work at all, it gives me an error when i’m trying to save. @corrinarusso

    Oops – syntax error fixed:

    function new_excerpt_more($more) {
    	global $post;
    	return '<a href="' . $post->ID . "' . > read more...</a>';
    }
    add_filter('excerpt_more', 'new_excerpt_more', 99);

    or this:

    function new_excerpt_more($more) {
    	global $post;
    	return '<a href="' . $post->ID . "' . > read more...</a>';
            // or maybe this:
            // echo get_permalink($post->ID);
    
    }
    add_filter('excerpt_more', 'new_excerpt_more', 99);

    Don’t rely on the length of the excerpt if you want a link on everything. Output the excerpt as is (no filter), then output the link to the post.

    Thread Starter yda5

    (@yda5)

    I don’t why buy these codes just don’t work.. it gives me error again. @corrinarusso

    And practically, how can i do it ? haha @joyously

    I tested this one on my own site:

    function new_excerpt_more($more) {
            global $post;
            return '<a href="' . $post->ID . '" . > read more...</a>';
            // or maybe this:
            // echo get_permalink($post->ID);
    
    }
    add_filter('excerpt_more', 'new_excerpt_more', 99);
    Moderator bcworkz

    (@bcworkz)

    “excerpt_more” filtering still only works on longer posts. To have a link after content no matter how short the content is, you can add something like <a href="<?php the_permalink(); ?>">Permalink</a> to the appropriate theme template right after content is output. Preferably a template for a child theme.

    Instead of altering templates, use “the_content” filter and concatenate the anchor tag shown above to the passed content.

    Thread Starter yda5

    (@yda5)

    Ok, but under which folder should i insert this of code?
    Sorry, i have a basic knowledge on the code stuff.

    Thread Starter yda5

    (@yda5)

    Or maybe there is another way to add “read more” to my website, I don’t know, i was thinking that is something basic that should be on every proper website..

    @bcworkz

    It is very likely that there is already a link to the post, on the title or the featured image or both. Not every site uses “read more” links since it looks cluttered and there are already links.
    If you are determined to add it, you can create a child theme and change the template, or you can add a filter for the_excerpt (not “the_content” as suggested above) and concatenate the link on the end.
    https://developer.www.ads-software.com/reference/hooks/the_excerpt/
    Be sure to make it accessible. Or you can choose a theme that already does this.

    Thread Starter yda5

    (@yda5)

    Thank you! i Used this code and it worked:

    add_filter( 'excerpt_more', '__return_empty_string', 21 );
    
    function wpse_134143_excerpt_more_link( $excerpt ) {
        $excerpt .= sprintf( 
                '... <a href="%s">%s</a>.',
                esc_url( get_permalink() ),
                __( 'continue reading' )
        );
        return $excerpt;
    }
    add_filter( 'the_excerpt', 'wpse_134143_excerpt_more_link', 21 );
Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Read more function’ is closed to new replies.