I want to change functionality of read more button in archive page
and make it expand post content and hide excerpt whenn click on it
I am using jquery to do this
but when you click on button , all excerpts in page are hidden
i need to hide the excerpt only in the article where the button is located
any help to do this
My code :
<div class="page-content">
<?php
while ( have_posts() ) {
the_post();
$post_link = get_permalink();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<div class="article-content">
<header class="entry-header">
<h3 class="entry-title"><?php the_title(); ?></h3>
</header>
<div class="entry-excerpt">
<?php the_excerpt(); ?>
</div>
<div class="news-content"><?php the_content(); ?></div>
<div class="read-more-div"><a class="read-more-btn" href='javascript:read_fun()'> Read more + </a></div>
</div>
</article>
<?php } ?>
</div>
My Jquery code
jQuery(document).ready(function($) {
$('.read-more-div').click(function() {
var $toggle = $(this);
$('.entry-excerpt').hide();
$('.news-content').show();
$(this).remove();
});
});
2
Answers
Sounds like your selector is not specific enough, and the click event listener is applying to all
.read-more-div
,.entry-excerpt
, and.news-content
elements on the page simultaneously instead of just the one within the clicked element as intended. Try looping through each element to assign the event listener so you can access each individual element:Your issue is tree navigation. Your hiding all
$('.entry-excerpt').hide();
and showing all$('.news-content').show();
where you only want the siblings to.read-more-div
that was clicked.There are two ways to do this, you can navigate the tree based on the click event using .parent() and .sibling() selectors, or place the event on the handler.
or using a delegate target method (listening on a parent)