I don’t know if this is somehow relevant to the “Read More” issue, but let me share it anyway. I know ChatGPT is not the most reliable thing, but as a non-developer myself, I was able to do some things with it. A lot of headaches, yes, a lot of mistakes by ChatGPT, but after a few attempts (and some basic knowledge on my end to know how to ask certain things), I was able to build some simple things with HTML, CSS, JavaScript.
For this particular case I asked it to do what I described above and to apply it to a category called “Photography” (as an example). Here’s what it offered me, if you know how to check if it’s valuable information or not:
Step 1: Create or Edit the Category Template
First, create a category template for the “Photography” category if it doesn’t already exist. This will be a PHP file named?category-photography.php
. Place this file in your theme directory.
<?php
get_header(); // Include the header
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
?>
<div class="post-content">
<div class="post-excerpt">
<?php echo wp_trim_words(get_the_content(), 10, '...'); ?>
</div>
<div class="post-full-content" style="display: none;">
<?php the_content(); ?>
</div>
<a href="javascript:void(0);" class="read-more">Read More</a>
</div>
<?php
endwhile;
else :
echo '<p>No posts found</p>';
endif;
get_footer(); // Include the footer
?>
Step 2: Add CSS
Add the following CSS to your theme’s?style.css
?file or via the Customizer (Appearance > Customize > Additional CSS) – (I know this is outdated with the new Site Editor, but that’s what GPT suggested):
.post-full-content {
display: none;
}
Step 3: Add JavaScript
Add the JavaScript to handle the “Read More” functionality. To ensure it only runs on the “Photography” category page, enqueue the script conditionally in your theme’s?functions.php
?file:
function custom_read_more_script() {
if (is_category('photography')) {
?>
<script>
document.addEventListener("DOMContentLoaded", function() {
var readMoreLinks = document.querySelectorAll('.read-more');
readMoreLinks.forEach(function(link) {
link.addEventListener('click', function(e) {
e.preventDefault();
var postContent = this.previousElementSibling;
if (postContent.style.display === "none") {
postContent.style.display = "block";
this.innerText = "Read Less";
} else {
postContent.style.display = "none";
this.innerText = "Read More";
}
});
});
});
</script>
<?php
}
}
add_action('wp_footer', 'custom_read_more_script');
——-
Now I know that even if this works (most definitely with some changes to the code), how would I be able to do this and still be able to update my theme? I always had a hard time full understanding the Child Theme concept and always relied on plugins, but I had issues in the past when trying to do it.