【问题标题】:Sorting custom post type by Page Attributes in Archive file does not work按存档文件中的页面属性对自定义帖子类型进行排序不起作用
【发布时间】:2018-02-17 06:36:43
【问题描述】:

我创建了我的 Cotom 帖子类型,但我想按页面属性中选择的数字对帖子进行排序。

Custom-post-type.php

function register_post_team() {
  $labels = array(
    'name'               => __( 'Zespół', '_tk' ),
    'singular_name'      => __( 'Zespół', '_tk' ),
    'add_new'            => __( 'Dodaj nową osobę', '_tk' ),
    'add_new_item'       => __( 'Dodaj nową osobę', '_tk' ),
    'edit_item'          => __( 'Edytuj', '_tk' ),
    'new_item'           => __( 'Nowa', '_tk' ),
    'all_items'          => __( 'Wszystkie', '_tk' ),
    'view_item'          => __( 'Zobacz', '_tk' ),
    'search_items'       => __( 'Szukaj', '_tk' ),
    'not_found'          => __( 'Nie zneleziono żadnej', '_tk' ),
    'not_found_in_trash' => __( 'Nie zneleziono żadnej w koszu', '_tk' ), 
    'parent_item_colon'  => '',
    'menu_name'          => __( 'Zespół', '_tk' ),
  );
  $args = array(
    'labels'            => $labels,
    'hierarchical'      => true,
    'supports'          => array( 'title', 'page-attributes', 'revisions', 'thumbnail', 'editor' ),
    'public'            => true,
    'show_ui'           => true,
    'show_in_menu'      => true,
    'show_in_nav_menus' => true,
    'publicly_queryable' => true,
    'exclude_from_search' => false,
    'has_archive'       => true,
    'rewrite'           => array('slug' => 'zespół','with_front' => false),
    'menu_position'     => 6,
    'menu_icon'         => 'dashicons-groups'
  );
  register_post_type( 'team', $args ); 
}
add_action( 'init', 'register_post_team' );

/* realisation taxonomies */
function add_team_category() {
  $labels = array(
    'name'              => __( 'Kategorie zespołu', '_tk' ),
    'singular_name'     => __( 'Kategoria zespołu', '_tk' ),
    'search_items'      => __( 'Szukaj kategorii', '_tk' ),
    'all_items'         => __( 'Wszystkie kategorie', '_tk' ),
    'parent_item'       => __( 'Kategoria nadrzędna', '_tk' ),
    'parent_item_colon' => __( 'Kategoria nadrzędna:', '_tk' ),
    'edit_item'         => __( 'Edytuj kategorię', '_tk' ), 
    'update_item'       => __( 'Aktualizuj kategorię', '_tk' ),
    'add_new_item'      => __( 'Dodaj nową kategorię', '_tk' ),
    'new_item_name'     => __( 'Nowa kategoria', '_tk' ),
    'menu_name'         => __( 'Kategorie zespołu', '_tk' ),
  );
  $args = array(
    'labels' => $labels,
    'hierarchical' => true,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'kategorie-zespołu' )
  );
  register_taxonomy( 'team_category', 'team', $args );
}
add_action( 'init', 'add_team_category', 0 );

这是我的 Archive-team.php(我想订购帖子的文件)

<?php get_header(); ?>

    <div class="container main-content">
        <?php if ( have_posts() ) : ?>
            <?php if (get_field('entry_text_team','option')) { ?>
                <div class="row text-center entry-text">
                    <?php the_field('entry_text_team','option'); ?>
                </div>
            <?php } ?>
            <div class="row archive-list">
                <?php $del = 100;
                while ( have_posts() ) : the_post();?>


                    <?php $personal_img = get_field('personal_img');?>
                    <article id="post-<?php the_ID(); ?>" <?php post_class('archive-item col-sm-24 single-opinion wow fadeInUp'); ?> data-wow-delay="<?php echo $del; ?>ms">
                        <div class="col-sm-4 personal_img_box">
                            <div class="personal_img">
                                <img src="<?php echo $personal_img['url']; ?>" alt="">
                            </div>
                        </div>
                        <div class="col-sm-20">
                            <div class="person_name">
                                <h3>
                                    <?php the_title(); ?>
                                </h3>
                            </div>
                            <div class="person_position">
                                <?php the_field('position');?>
                            </div>
                            <div class="person_description">
                                <?php the_field('description');?>
                            </div>
                        </div>
                    </article>

                <?php $del = $del + 150;
                endwhile; ?>
            </div>

            <?php //_tk_content_nav( 'nav-below' ); ?>
            <?php _tk_pagination(); ?>

        <?php else : ?>

            <?php get_template_part( 'no-results', 'index' ); ?>

        <?php endif; ?>

        <div class="row text-center">
            <a href="http://pokochajlatanie.pro-page.pl/opinie/" class="btn btn-primary">Opinie o nas</a>
        </div>
    </div>

<?php get_footer(); ?>

看起来即使我在页面属性中选择了一些订单号,它也根本没有排序。我认为目前它会按创建日期显示帖子。

【问题讨论】:

  • 是 Desc 顺序中的日期是任何查询的默认 WP 顺序。您需要向此查询添加自定义订单。让我用例子写答案。

标签: php wordpress custom-post-type


【解决方案1】:

您需要将此添加到您的functions.php 文件中,以便在调用模板文件之前应用它。您要查找的 orderby 称为:Menu_order 这里是 codex 的解释:

'menu_order' - 按页面顺序排列。最常用于页面(编辑页面属性框中的顺序字段)和附件(插入/上传媒体库对话框中的整数字段),但可用于具有不同 'menu_order' 值的任何帖子类型(它们都是默认值为 0)。 来源:https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

由于您只想将此订单应用于 TEAM CPT 存档,您可以添加如下条件,这样它就不会影响网站的其他部分,例如博客文章或页面等...

    /* Sort team members like page order i.e. the number assigned */
function team_custom_post_order_sort( $query ){
  if ( $query->is_main_query() && is_post_type_archive( 'team' )){
    $query->set( 'orderby', 'menu_order' );
    $query->set( 'order' , 'ASC' );
  }
}
add_action( 'pre_get_posts' , 'team_custom_post_order_sort' );

注意:代码未经测试,请检查本地主机以修复任何拼写错误。

【讨论】:

  • 很高兴知道! :) 编码愉快!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2013-06-02
  • 2014-07-21
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 2016-06-05
相关资源
最近更新 更多