2017-06-19 301 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 } ?>