2014-11-03 16 views
0

拼版頁我有,我跑WP_Query頁面模板WordPress是表明不存在

$temp = $wp_query; 
$wp_query= null; 
$wp_query = new WP_Query(); 
$wp_query->query('post_type=post&posts_per_page=4'.'&paged='.$paged); 
while ($wp_query->have_posts()) : $wp_query->the_post(); 

我面臨的問題是,如果你去網站/網頁/ 100它會顯示模板,它不應該在404時它應該。

我的博客頁面正在閱讀設置是另一個頁面,這是我正在做的自定義模板。

我已閱讀此文章https://wordpress.stackexchange.com/questions/46116/non-existing-blog-pages-are-not-redirected-to-404並嘗試了所有功能,但都沒有工作。

我也花了3個小時搜索谷歌,但無法找到解決方法。

回答

0

您正在創建新的WP查詢。所以我認爲你必須重置wp_postdata

WP Codex提供了示例代碼。在使用WP_Query顯示帖子列出你需要自己,如果他們是對當前頁面的任何職位,以檢查你可以嘗試像

$wp_query = new WP_Query(); 
    $wp_query->query('post_type=post&posts_per_page=4'.'&paged='.$paged); 

if(($wp_query->have_posts()) : 
    while ($wp_query->have_posts()) : $wp_query->the_post(); 
     // Do here something with the post 
    endwhile; 

    wp_reset_postdata(); // At the end reset your query 
endif; 
+0

嗨pbaldauf謝謝您的回答,我有嘗試重置發佈數據,但是如果我轉到domain.com/page/100,它仍然顯示模板而不是轉到404 – Jaypee 2014-11-03 19:09:34

+0

顯示模板而不是404的另一個原因可能是重定向規則(可以在.htaccess或一個wp_rewrite_rule) – pbaldauf 2014-11-03 19:13:10

0

。下面是Wordpress docs

<?php if (have_posts()) : ?> 

<!-- Add the pagination functions here. --> 

<!-- Start of the main loop. --> 
<?php while (have_posts()) : the_post(); ?> 

<!-- the rest of your theme's main loop --> 

<?php endwhile; ?> 
<!-- End of the main loop --> 

<!-- Add the pagination functions here. --> 

<div class="nav-previous alignleft"><?php next_posts_link('Older posts'); ?></div> 
<div class="nav-next alignright"><?php previous_posts_link('Newer posts'); ?></div> 

<?php else : ?> 
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p> 
<?php endif; ?> 

示例代碼替換「_e(‘對不起,沒有職位符合您的條件。’);」 用自己的錯誤或功能顯示404錯誤。

編輯:上面的代碼當然是Main Loop的例子。在你的具體情況下,你需要調用$ wp_query-> have_posts()並確保它不是false。如果它確實顯示404錯誤。

例如:

if(!$wp_query->have_posts()) { 
    echo "404"; 
} 

或者,如果你想與HTTP代碼爲404,而不是200的服務器響應,那麼你應該嘗試這樣的事:

if(!$wp_query->have_posts()) { 
status_header(404); 
nocache_headers(); 
include(get_404_template()); 
exit; 
} 
+0

嗨Gemi倪,謝謝你的回答。我認爲這是接近的,但不是我正在尋找的,因爲這將只在頁面上打印404,而不是在分頁頁面不存在時轉到404。不知道我是否正確或者不在此聲明中,如果我不是,請告訴我。我更談論如果你去那個頁面/頁面/ 100,它應該去404頁面,而不是顯示模板?不確定if/else語句是否是正確的方法?請讓我知道,並再次感謝 – Jaypee 2014-11-03 19:49:39

+0

例如,如果谷歌抓取此頁面,它將被添加到索引,因爲它顯示404,但「它不是物理404頁面」不知道我是否自我解釋好。 – Jaypee 2014-11-03 19:51:03

+0

另一個例子,如果你把這個頁面設置爲設置 - >閱讀的「帖子頁面」,即使你沒有if/else語句,一旦你去到不存在的分頁頁面,它會直接到404。沒有使用自定義查詢,只是wp循環。 – Jaypee 2014-11-03 19:54:52