【问题标题】:Wordpress - Need help populating a menu with the titles of Custom Posts with a specific CategoryWordpress - 需要帮助填充具有特定类别的自定义帖子标题的菜单
【发布时间】:2011-07-04 15:35:31
【问题描述】:

我正在使用 Wordpress 3.1.4 - 并尝试使用具有特定类别的自定义帖子动态填充菜单,如您所见 here.

所以在页面上 - 我有主循环 - 列出所有玩具产品。这些产品都来自标有“产品”的自定义帖子类型 - 每个产品属于不同的类别 - 毛绒玩具、游戏等。

在侧边栏菜单中,我为每个类别编写了以下代码,以便列出给定类别中的产品:

<ul class="acitem">
<?php query_posts(array ('post_type' => 'products''cat=games')); ?>
<?php $games_query = new WP_Query("category_name=games"); ?>
<?php while ($games_query->have_posts()) : $games_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>      
</ul>

由于某种原因,没有生成列表项。这可能与页面上的主循环冲突吗?

任何建议或指针表示赞赏! -

PS - 我正在使用以下插件:

  • 自定义帖子类型用户界面
  • WP 页面导航

如果这有任何影响。

【问题讨论】:

    标签: wordpress menu custom-post-type


    【解决方案1】:

    请尝试以下代码sn-p。

    <ul class="acitem">
    <?php $games_query = new WP_Query(array('post_type'=>'product','category_name'=>'games','posts_per_page'=>-1)); ?>
    <?php while ($games_query->have_posts()) : $games_query->the_post(); ?>
    <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>   
    </ul>
    

    http://codex.wordpress.org/Class_Reference/WP_Query

    【讨论】:

    • 感谢您的帮助 boxoft - 但是当我尝试此代码时,我收到以下错误:解析错误:语法错误,意外 T_DOUBLE_ARROW。我最终通过将您的第二行换成以下内容来实现这一点 - 。菜单现在像梦一样工作 - 非常感谢您的指点!
    【解决方案2】:

    我最终通过将 boxofts 代码的第二行替换为以下代码来实现这一点 -

    <?php $games_query = new WP_Query('post_type=products&category_name=soft-toys'); ?>
    

    【讨论】:

      猜你喜欢
      • 2013-06-30
      • 1970-01-01
      • 1970-01-01
      • 2017-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 1970-01-01
      相关资源
      最近更新 更多