我正在使用2种自定义帖子类型在WordPress网站上工作。由于某些原因,我无法弄清楚,我的nav菜单没有显示在我的category.php页面上。正在调用header.php文件并将其渲染得很好,Nav菜单适用于所有其他页面。因此,我认为问题与WP Nav菜单无关,而是与查询有关。
我的页脚导航菜单出现了同样的问题。我发现其他人通过在调用footer.php之前将查询重置为NULL来解决此问题。这有效,虽然看起来像是一个糟糕的解决方案。
当然,在调用header.php之前我无法执行此操作,因为我需要使用查询来获取和呈现页面上的所有内容。我真的被困在这里了。还有其他人有这个问题吗?
---更新---
这是有效的,并且基于上面提到的相同原则:重置查询,获取导航菜单。
// store the query in a variable
$query_store = $wp_query;
// reset the query
$wp_query = NULL;
$wp_query = new WP_Query(array('post_type' => 'projects'));
// get the header
get_header();
// retrieve the query from storage, use it
$wp_query = $query_store;
我也按自定义meta_values排序。我可以使用此方法将活动类别传递给新查询:
// get the current category, pass it to the new query arguments below
if (is_category('category-one')) { $category_name = 'category-one'; }
if (is_category('category-two')) { $category_name = 'category-two'; }
然而,这提出了另一个问题:类列表(在header.php中设置)不再包含类别类(这会影响我的CSS)。
我原来的问题仍然存在:有更清洁的方法吗?
答案 0 :(得分:0)
您是否尝试在页面中的每个wp循环结束时添加以下2个函数?
wp_reset_postdata();
wp_reset_query();
答案 1 :(得分:0)
您必须重置所有postdata数组和查询数组,因为您可以使用以下链接。
[1] http://codex.wordpress.org/Function_Reference/wp_reset_postdata
[2] http://codex.wordpress.org/Function_Reference/wp_reset_query
感谢。