2017-09-08 5 views
3

私はCostom投稿タイプを作成しましたが、ページ属性で選択した番号で投稿を並べ替えたいと思います。アーカイブファイルのページ属性によるカスタム投稿タイプの並べ替えが動作しません

Page Attributes

カスタムポスト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); 

ここでは、私のアーカイブ-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(); ?> 
です

私はページ属性で何らかの注文番号を選択すると、まったく並べ替えません。私は現時点では、作成された日付までに投稿を表示していると思います。

+1

Desc順序の[はい]の日付は、任意のクエリのデフォルトのWP順序です。このクエリにカスタム注文を追加する必要があります。私は例を使って答えを書いてみましょう。 – Mohsin

答えて

2

これをfunction.phpファイルに追加して、テンプレートファイルを呼び出す前に適用する必要があります。あなたが呼ばれて探しているのorderby: - 注文ページの順序によって

menu_order」:Menu_orderはここコーデックスからの説明です。ページの編集(Edit Page AttributesボックスのOrderフィールド)とAttachments(Insert/Upload Media Galleryダイアログの整数フィールド)によく使用されますが、異なる 'menu_order'値を持つ任意のポストタイプに使用できます0にする)。 出典:あなたはTEAM CPTのためにのみアーカイブにこの順序を適用するとして、それは、ブログの記事やページなどのようなウェブサイトの他の部分に影響を与えないようにhttps://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

、あなたは以下のような条件を追加することができます...

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

注:コードはテストされていません。ローカルホストでタイプミスを修正してください。

+0

コードの入力ミスが修正されました。 – Mohsin

+0

あなたのソリューションは100%で動作します。ありがとうございました。 –

+0

お役立ち情報:)ハッピーコーディング! – Mohsin

関連する問題