【问题标题】:Outputting WordPress custom post type content by custom taxonomy and sorting that content通过自定义分类法输出 WordPress 自定义帖子类型内容并对该内容进行排序
【发布时间】:2012-03-23 06:22:27
【问题描述】:

我正在使用@Ellum2009 (http://stackoverflow.com/q/4160319/705100) 的此解决方案按分类术语输出自定义帖子类型。示例取自链接:

<?php $posts = new WP_Query(array( 
   'taxonomy' => 'type-mario',
   'term' => 'games',
   'posts_per_page' => 10 
)); ?>
<p>Mario games</p>
<?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
  <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2><?php the_title(); ?></h2>
  </div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>

但是,此解决方案不允许对条目进行进一步排序。它只是首先输出它们的最后一个条目。我想我可以为每个术语创建子分类并将它们标记为“第一”、“第二”、“第三”等。然后用户只需选中管理界面中的相应框来控制排序顺序.

我的问题是,我将如何输出排序后的内容?

谢谢!

【问题讨论】:

    标签: wordpress custom-post-type custom-taxonomy


    【解决方案1】:

    我对分类学了解不多,但是 WP_Query 确实支持 orderby 和 order 参数 - 这些不适用于您的情况吗?

    <?php $posts = new WP_Query(array( 
       'taxonomy' => 'type-mario',
       'term' => 'games',
       'posts_per_page' => 10,
       'orderby' => 'date',
       'order' => 'ASC'
    )); ?>
    <p>Mario games</p>
    <?php while ( $posts->have_posts() ) : $posts->the_post(); ?>
      <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <h2><?php the_title(); ?></h2>
      </div>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?>
    

    【讨论】:

    • @user705100 ,也可以改成 'orderby' => 'title' 如果你想按帖子标题排序,只需更改 orderby 值即可根据该值排序。 'orderby' =>'title date' 也是允许的。更多codex.wordpress.org/Class_Reference/…
    • @Ryan-Kempt:在这种情况下,我需要让用户选择以他选择的方式对项目进行排序。父分类法的简单升序或降序不会削减它。
    • 对不起,我没有完全理解你的问题,以 javascript 可以排序的方式输出它可能是最简单的解决方案。
    • 我想知道输出是否可以要求按分类子项排序的数组(第一、第二、第三)。不过,超出了我的 php 技能。
    猜你喜欢
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    • 2013-08-03
    • 2019-08-28
    • 2014-07-21
    • 2020-10-04
    • 2020-11-17
    相关资源
    最近更新 更多