2014-11-03 46 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