【问题标题】:Wordpress Nav menu: doesn't show on custom post type category pagesWordpress 导航菜单:不显示在自定义帖子类型类别页面上
【发布时间】:2013-10-21 12:45:21
【问题描述】:

我正在开发一个具有 2 种自定义帖子类型的 WordPress 网站。由于某种我无法弄清楚的原因,我的导航菜单没有显示在我的 category.php 页面上。 header.php 文件被调用并渲染得很好,并且导航菜单适用于所有其他页面。所以,我认为问题不在于 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;

我也按自定义元值排序。我可以使用此方法将活动类别传递给新查询:

// 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)。

我最初的问题仍然存在:有没有更清洁的方法来做到这一点?

【问题讨论】:

    标签: wordpress menu nav


    【解决方案1】:

    您是否尝试在页面中每个 wp 循环的末尾添加以下 2 个函数?

    wp_reset_postdata();
    wp_reset_query();

    【讨论】:

    • 谢谢!我试过这个,没有骰子。这是我目前在get_footer(); 之前所拥有的:$wp_query = NULL; $wp_query = new WP_Query(array('post_type' => 'projects'));
    【解决方案2】:

    你必须重置所有的 postdata 数组和查询数组,你可以使用下面的链接。

    [1]http://codex.wordpress.org/Function_Reference/wp_reset_postdata

    [2]http://codex.wordpress.org/Function_Reference/wp_reset_query

    谢谢。

    【讨论】:

      猜你喜欢
      • 2015-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-11
      • 2019-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多