I figured it out!!
https://codepen.io/maxds/pen/jgeoA
I have a blog-entry-single.php file which is called in index.php. I am using a premium theme which auto generated posts excerpts on category/archives page meaning in posts I didn’t add a read more link.
This is my blog-entry-single.php file after editing:
<?php
/**
* Blog entry single
*
* @package Total WordPress theme
* @subpackage Partials
* @version 3.6.0
*/
// Exit if accessed directly
if ( ! defined( ‘ABSPATH’ ) ) {
exit;
}
// Should we check for the more tag?
$check_more_tag = apply_filters( ‘wpex_check_more_tag’, true ); ?>
<div class=”blog-entry-excerpt wpex-clr”>
<?php
// Display excerpt if auto excerpts are enabled in the admin
if ( my_get_mod( ‘blog_exceprt’, true ) ) :
// Check if the post tag is using the “more” tag
if ( $check_more_tag && strpos( get_the_content(), ‘more-link’ ) ) :
// Display the content up to the more tag
the_content( ”, ‘…’ );
// Otherwise display custom excerpt
else :
/* // Display custom excerpt
my_excerpt( array(
‘length’ => my_excerpt_length(),
) );*/
// I deleted what was here and replaced it with div below
?>
<div class=”more”><?php the_content();?></div><?php
endif;
// If excerpts are disabled, display full content
else :
the_content( ”, ‘…’ );
endif; ?>
</div><!– .blog-entry-excerpt –>
Then I added a new javascript function to my functions.php file
add_action( ‘wp_footer’, function() { ?>
<script>
( function( $ ) {
‘use strict’;
// Configure/customize these variables.
var showChar = 100; // How many characters are shown by default
var ellipsestext = “…”;
var moretext = “Show more >”;
var lesstext = “Show less”;
$(‘.more’).each(function() {
var content = $(this).html();
if(content.length > showChar) {
var c = content.substr(0, showChar);
var h = content.substr(showChar, content.length – showChar);
var html = c + ‘<div class=”moreellipses”>’ + ellipsestext+ ‘ </div><div class=”morecontent”><div>’ + h + ‘</div> ‘ + moretext + ‘</div>’;
$(this).html(html);
}
});
$(“.morelink”).click(function(){
if($(this).hasClass(“less”)) {
$(this).removeClass(“less”);
$(this).html(moretext);
} else {
$(this).addClass(“less”);
$(this).html(lesstext);
}
$(this).parent().prev().toggle();
$(this).prev().toggle();
return false;
});
} ) ( jQuery );
</script>
<?php } );
Also I needed to add css:
.morecontent div {
display: none;
}
.morelink {
display: block;
}
I had to copy blog-entry-readmore.php into child theme and save it blank otherwise I would have 2 read more buttons.
Thanks ??