• Resolved CrouchingBruin

    (@crouchingbruin)


    When excerpts are automatically generated for the blog page, the Read More link shows up. However, when I create a manual excerpt by filling out the Excerpt field for the post, or if I use the <!--more--> tag, then the excerpt doesn’t have the Read More link. Please take a look at the page linked below. You should be able to tell which posts had the excerpts automatically generated (with the Read More link) versus the ones where I provided a manual excerpt.

    Is it possible to modify the theme so that the Read More link shows up with manual excerpts? If I need to create a child theme to do it, please let me know which function I need to modify. I looked at io_excerpt_more in functions.php and it looks like it should already be working, but maybe I don’t understand the code enough.

    Thanks.

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

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter CrouchingBruin

    (@crouchingbruin)

    Sorry, but one more request: is it possible to put some sort of separator between the category words, either a comma or a dot? That way, the words don’t look like they make one phrase. If you refer to the same site above, you can see that some of the Categories look like “Advanced Demo” or “Intermediate Tutorial”, where it would be much more clearer to have some sort of delimiter so it would look like “Advanced, Demo” or “Advanced | Demo”.

    regarding the first question:

    reminder:
    the ‘read more’ link is added via a filter on excerpt_more which transforms the iconic three dots … into a link (and those three dots get only added in certain circumstances).

    there are a few scenarios:

    – there is a hand-written excerpt -> no dots … (therfore also no ‘read more’ link); you might want the ‘read more’ link because the full post is so different from your manual excerpt.
    — to add the ‘read more’ in this case would be easy enough to use a filter on 'get_the_excerpt' based on the conditional has_excerpt();

    – there is a ‘more tag’ in the cleaned up content, before the excerpt length limit -> no dots … (therfore also no ‘read more’ link); that location might not be what you really intended, and also what is shown is missing formatting and images etc.
    — in this case, to add the ‘read more’ link, you could check for the ‘more tag’ in the content, for example using strpos( $post->content, '<!--more-->' );
    — however, this ‘more tag’ could be within the excerpt length limit, and would therefore shorten the excerpt and leave it without the dots, or it could be anywhere outside the excerpt length limit and would not interfere with the excerpt and the dots … giving you a ‘read more’ link.
    —- you will need to check where the ‘more tag’ actually is within the content, and add the ‘read more’ link depending on that position.

    – the cleaned up content (after strip tags etc) is shorter than the excerpt length limit; it would not get any three dots … (therfore also no ‘read more’ link); although it might contain images, links, formatting like lists etc which makes it worthwhile to view the full post.
    — there is also a conditional check for that.

    the code:

    //ADDING THE READ MORE LINK TO ALL OTHER EXCERPTS
    add_filter( 'get_the_excerpt', 'excerpt_read_more_all_cases' );
    
    function excerpt_read_more_all_cases( $text ) {
    
    	global $post;
    	$raw = $post->post_content;
    	$excerpt_length = apply_filters( 'excerpt_length', 55 );
    	$raw_to_more = substr( $raw, 0, strpos( $raw, '<!--more-->' ) );
    	$excerpt_of_raw = wp_trim_words( $raw, $excerpt_length, ''); 
    	$excerpt_of_raw_to_more = wp_trim_words( $raw_to_more, $excerpt_length, ''); 
    
    // CHECKING FOR MANUALLY WRITTEN EXCERPT
    	if( has_excerpt() ) { 
    	
    		$text .= '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' manually done, so Read More', 'textdomain' ) . ' &raquo;</a></span>';
    		
    // CHECKING FOR EXCERPT BEING SHORT BY THE 'MORE TAG'
    	} elseif( strpos( $raw, '<!--more-->' ) && strlen( $excerpt_of_raw_to_more ) < strlen( $excerpt_of_raw ) ) {
    	
    		$text .= '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' cut short by \'more tag\', so Read More', 'textdomain' ) . ' &raquo;</a></span>';
    		
    // CHECKING FOR EXCERPT BEING SHORT BECAUSE OIF SHORT CONTENT
    	} elseif( strlen( $text ) == strlen( $excerpt_of_raw ) ) {
    	
    		$text .= '&hellip; <span class="read-more"><a href="'. get_permalink($post->ID) . '">' . __( ' too short for excerpt, but Read More anyway', 'textdomain' ) . ' &raquo;</a></span>';
    		
    	}
    	
    	return $text;
    }

    checked in functions.php of a child theme in Twenty Twenty and in Iconic One.

    I added different texts to the ‘read more’ links to point out the different scenarios, in production, this could be all the same ‘read more’ text …

    second question; separating the categories:

    the code is in content.php in Iconic ~One:

    	<footer class="entry-meta">
    		<?php if ( is_home() && ( get_theme_mod( 'iconic_one_catg_home' , '1' ) == '1' ) ) : ?>
    			<span><?php _e('Category:','iconic-one'); ?> <?php the_category(' '); ?></span>
    		<?php elseif( !is_home() ): ?>
    			<span><?php _e('Category:','iconic-one'); ?> <?php the_category(' '); ?></span>
    		<?php endif; ?>
    		<?php if ( is_home() && ( get_theme_mod( 'iconic_one_tag_home' , '1' ) == '1' ) ) : ?>
    				<span><?php the_tags(); ?></span>
    		<?php elseif( !is_home() ): ?>
    				<span><?php the_tags(); ?></span>
    		<?php endif; ?>	
    

    you can either hack a copy of that file in your child theme,

    or try with CSS:

    .entry-meta span:first-child a:first-child:before { content: ''; width: 0; display: none; }
    .entry-meta span:first-child a:before { content: '|'; padding: 0.5em 0.4em 0.5em 0em; }

    or you could use a filter function in functions.php of the child theme, to add the | pipe character:

    add_filter( 'the_category', 'the_category_add_separator' );
    function the_category_add_separator( $thelist ) {
    	$thelist = str_replace( '</a> <a ', '</a> | <a ', $thelist );
    	return $thelist;
    }
    Thread Starter CrouchingBruin

    (@crouchingbruin)

    @alchymyth, that was so awesome, both pieces of code work perfectly. Got the Read More links on all my excerpts as well as the separator character between my category words.

    Thanks very much, sincerely appreciate it.

    Michael (@alchymyth)

    I will thank you many times for taking the time to give a very thorough and informative answer. It answered something else that I had puzzled with ??
    Especially the answer around the separator – once again Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Read More for Manual Excerpts’ is closed to new replies.