Reputation: 3584
I have a loop which I use to display posts from news category and If I click either on the title, thumbnail and read more, it should be able to direct me to the relative post.
My loop looks like this:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 3,
'category_name' => 'news'
);
$query = new WP_Query($args);
while($query->have_posts()) : $query->the_post();
?>
<div class="news_box_content">
<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
<figure><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(); ?></a></figure>
<?php if($post->post_excerpt) { ?>
<p><?php echo substr(get_the_excerpt(), 0,300); ?></p>
<a href="<?php the_permalink(); ?>">Read more...</a>
<?php } else {
echo get_excerpt();
} ?>
</div>
<?php endwhile; wp_reset_postdata(); ?>
All works fine except on the read more link.
Problem is that when I click on read more, It leads me to the 404 page instead of the post content.
How can I solve that?
Upvotes: 1
Views: 137
Reputation: 3584
I just realised that the theme functions.php has a function to get the excerpt and there a permalink was being added:
function get_excerpt(){
$excerpt = get_the_content();
$excerpt = preg_replace(" ([.*?])",'',$excerpt);
$excerpt = strip_shortcodes($excerpt);
$excerpt = strip_tags($excerpt);
$excerpt = substr($excerpt, 0, 145);
$excerpt = substr($excerpt, 0, strripos($excerpt, " "));
$excerpt = $excerpt.'<a class="more-link" href="<?php the_permalink();?>">Read more</a>';
return $excerpt;
}
I removed the line where the a tag is added, and instead I edited the my loop to this:
<?php if($post->post_excerpt) { ?>
<p><?php echo substr(get_the_excerpt(), 0,300); ?></p>
<a href="<?php the_permalink(); ?>">Read more...</a>
<?php } else { ?>
<?php echo get_excerpt(); ?>
<a class="more-link" href="<?php the_permalink();?>">Read more</a>
<?php } ?>
Upvotes: 1
Reputation: 27503
<?php if( get_the_excerpt() ) { ?>
<p><?php echo substr(get_the_excerpt(), 0,300); ?></p>
<a href="<?php the_permalink(); ?>">Read more...</a>
<?php } else {
echo get_excerpt();
} ?>
try this
Upvotes: 1