2017-06-19 120 views
1

我有一个循环,我用它来显示新闻类别的帖子,如果我点击标题,缩略图和阅读更多,它应该能够指引我到相对帖子。永久链接去404页

我的循环如下所示:

<?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(); ?> 

所有作品,除了阅读更多链接罚款。

问题是,当我点击阅读更多内容时,它会将我引导至404页面而不是帖子内容。

我该如何解决这个问题?

+0

在标题工程中的永久链接佩尔利? – Exprator

+0

是的,它工作正常 –

回答

1
<?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(); 
     } ?> 

试试这个

+0

这是一个使wordpress打印摘录两次 –

+0

尝试编辑ans – Exprator

+0

我刚刚发现问题并修复,你可以在我自己的答案中看到。 无论如何欣赏支持 –

1

我只是意识到了这个主题的functions.php有一个函数来获取摘录并有添加一个固定链接:

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; 
} 

我删除线的地方一个标签被添加,而是我编辑我的循环到这个:

<?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 } ?>