【问题标题】:Wordpress: How to sort posts on a custom fieldWordpress:如何对自定义字段上的帖子进行排序
【发布时间】:2012-08-16 04:00:27
【问题描述】:

我想以特定顺序显示来自自定义分类的帖子标题列表。 我认为控制顺序的最佳方法是添加一个自定义字段并对该自定义字段进行排序。

我遇到的问题是我正在尝试使用 Wordpress 的内置功能,但找不到添加排序功能的方法。

我的场景看起来像这样 调用 url 是 ...com/taxonomy/term

这会调用一个模板,其文件名为 taxonomy-taxonomyname-term.php

我的模板只是 index.php 模板重命名和编辑以包含此循环

<?php if ( have_posts() ) : ?>

<?php twentyeleven_content_nav( 'nav-above' ); ?>
<ul>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => 'Permalink to: ', 'after' => '' ) ); ?>"><?php the_title(); ?></a></li>

<?php endwhile; ?>
</ul>
<?php twentyeleven_content_nav( 'nav-below' ); ?>

<?php else : ?> etc

这会显示标题列表,但我找不到控制标题顺序的方法。

我见过设置一组帖子顺序的唯一方法是定义查询中帖子的顺序。但当然在这种情况下我没有查询,因为我已经通过调用 url 获得了帖子。

有没有办法在不添加其他查询的情况下添加排序功能,或者查询是强制性的。

【问题讨论】:

    标签: wordpress sorting custom-fields custom-taxonomy


    【解决方案1】:

    假设您的自定义字段是 my_date 您可以像这样创建自定义查询。

    query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
    

    使用它

    <?php if ( have_posts() ) : ?>
    
    <?php twentyeleven_content_nav( 'nav-above' ); ?>
    <ul>
    <?php /* Start the Loop */ 
      query_posts('meta_key=my_day&meta_compare=<=&meta_value=20&orderby=meta_value&order=DESC');
    ?>
    
    <?php while ( have_posts() ) : the_post(); ?>
    <li><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute( array( 'before' => 'Permalink to: ', 'after' => '' ) ); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    </ul>
    <?php twentyeleven_content_nav( 'nav-below' ); ?>
    
    <?php else : ?> etc
    

    欲了解更多信息http://wpengineer.com/1915/sort-posts-custom-fields/

    【讨论】:

    • 我是否假设您不需要“wp_reset_query()”,因为您已经创建了自定义搜索?
    猜你喜欢
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    相关资源
    最近更新 更多