【问题标题】:Custom template order by id issue按 ID 问题自定义模板顺序
【发布时间】:2018-10-24 01:47:18
【问题描述】:

我正在尝试按 ASC 顺序按 ID 对帖子进行排序,当我使用单个查询时我能够做到这一点,但是当使用数组查询时,我无法按 ASC 顺序按 ID 对帖子进行排序我的代码如下所示

  <?php
    $ourCurrentPage = get_query_var('paged');
    $newposts = new WP_Query(array(
    'category_name'  => 'stock-market-basics',
    'orderby'    => 'ID',
    'order'      => 'ASC',
    'paged'      => $ourCurrentPage
    ));
  ?>
  <?php 
    if ($newposts->have_posts()):
    while ($newposts->have_posts()):
    $newposts->the_post();
  ?> 

// 一些显示代码和关闭后的代码 //

  <?php endwhile; else : ?>
    <p><?php esc_html_e( 'Sorry, no results matched your criteria.' ); ?</p>
  <?php endif; ?>

            <li><?php 
              echo paginate_links(array(
                'total' => $newposts->max_num_pages
              ));
            ?></li>

【问题讨论】:

  • 您想按ID显示分类查询列表顺序吗?
  • 是的,但它是类别的自定义模板。请建议这将非常有帮助
  • 请检查我的回答。

标签: mysql wordpress sql-order-by


【解决方案1】:

WordPress 按类别名称查询

<?php $args = array(
    'posts_per_page'   => 5,
    'offset'           => 0,
    'category'         => '',
    'category_name'    => '',
    'orderby'          => 'date',
    'order'            => 'DESC',
    'include'          => '',
    'exclude'          => '',
    'meta_key'         => '',
    'meta_value'       => '',
    'post_type'        => 'post',
    'post_mime_type'   => '',
    'post_parent'      => '',
    'author'       => '',
    'author_name'      => '',
    'post_status'      => 'publish',
    'suppress_filters' => true 
);
$posts_array = get_posts( $args ); ?>


// Custom query.
$query = new WP_Query( $args );

// Check that we have query results.
if ( $query->have_posts() ) {

    // Start looping over the query results.
    while ( $query->have_posts() ) {

        $query->the_post();

        // Contents of the queried post results go here.

    }

}

希望这对你有用。

【讨论】:

    猜你喜欢
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多