简体   繁体   中英

Sorting custom post type by Page Attributes in Archive file does not work

I created my Costom Post Type but I would like to order the posts by the number choosen in Page attributes.

页面属性

The 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 );

Here is my Archive-team.php(the file where I would like to order the posts)

<?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(); ?>

It looks like even If I choose some order number in page attributes it doesn't sort at all. I think that at the moment it display the posts by the date it had been created.

You need to add this into your functions.php file so it gets applied before template file is called. The orderby you are looking for is called: Menu_order here is explanation from codex:

' menu_order ' - Order by Page Order. Used most often for Pages (Order field in the Edit Page Attributes box) and for Attachments (the integer fields in the Insert / Upload Media Gallery dialog), but could be used for any post type with distinct 'menu_order' values (they all default to 0). Source: https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

As you want to apply this order only to Archive for TEAM CPT, you can add conditional like below so it does not effect other parts of the website like Blog posts or pages etc...

    /* 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' );

Note: Code is untested, please check on localhost to fix any typos.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM