2016-08-20 132 views
-1

我以'手機'的名義創建了自定義帖子類型。在類別中,我有4個品牌名稱,我希望每個電話在類別頁面中都有一個類別。我的自定義帖子類型根據wordpress中的類別未顯示

我的功能代碼:

function create_phone_post_type() { 
register_post_type('phones', 
    array(
     'labels' => array(
      'name' => 'phones', 
      'singular_name' => 'phones', 
      'add_new' => 'Add New', 
      'add_new_item' => 'Add New phone', 
      'edit_item' => 'Edit phone', 
      'new_item' => 'New phone', 
      'view_item' => 'View phone', 
      'search_items' => 'Search phone', 
      'not_found' => 'Nothing Found', 
      'not_found_in_trash' => 'Nothing found in the Trash', 
      'parent_item_colon' => '' 
     ), 
     'taxonomies' => array('category',), 
     'public' => true, 
     'publicly_queryable' => true, 
     'show_ui' => true, 
     'query_var' => true, 
     'has_archive'   => true, 
     'capability_type' => 'post', 
     'hierarchical' => false, 
     'menu_position' => null, 
     'supports' => array('title', 
      'editor', 
      'excerpt', 
      'trackbacks', 
      'custom-fields', 
      'comments', 
      'revisions', 
      'thumbnail', 
      'author', 
      'page-attributes',) 
      ) 
      ); 
      } 
     add_action('init', 'create_phone_post_type'); 

和分類頁面:

<?php if(have_posts()) : ?> 
    <div class="title_page_category"> 
     <h5> <?php printf(__('Category: %s', 'anaximander'), '<span>' . single_cat_title('', true) . '</span>');?></h5> 
    </div> 

    <?php 
     $args = array('post_type' => 'phones', 
     'posts_per_page' => 20); 
     $loop = new WP_Query($args); 
     while ($loop->have_posts()) : $loop->the_post();?> 
      <div class="col-lg-3"> 
        <div class="phone_thumbnail"> 
         <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > 
          <?php the_post_thumbnail(); ?> 
         </a> 
        </div> 
        <div class="phone_title"> 
      <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" > 
          <?php the_title(); ?> 
         </a> 
        </div> 

      </div> 

      <?php endwhile; ?> 


    <?php else : ?> 
    <?php endif; ?> 

但是,當我在不同的類別增加了幾個電話,所有電話的一類往裏走。

我真的很困惑。請幫我:(

+0

在後端側下一個分類全部轉到 – Coder

+0

請您解釋一下更好,我可以幫你 – Coder

+0

你能解釋一下你所說的「添加了幾個電話在不同的類別」和「所有手機的往裏走的意思到底是什麼在一個類別中「? –

回答

-1
  • 我們已檢查過您的自定義後的類型和自定義分類代碼,我覺得問題在您的自定義分類代碼,您的自定義分類名稱爲「類」,它具有創造的問題,因爲「類別」是的WordPress默認分類後的名稱。

請添加下面的代碼在你的functions.php文件創建在WordPress的自定義後的類型和HIST定製texonomy。

自定義文章類型

<?php 
function my_custom_phones() { 

    $labels = array(

    'name'    => _x('phones', 'post type general name'), 

    'singular_name'  => _x('phones', 'post type singular name'), 

    'add_new'   => _x('Add New', 'phones'), 

    'add_new_item'  => __('Add New phones'), 

    'edit_item'   => __('Edit phones'), 

    'new_item'   => __('New phones'), 

    'all_items'   => __('All phones'), 

    'view_item'   => __('View phones'), 

    'search_items'  => __('Search phones'), 

    'not_found'   => __('No phones found'), 

    'not_found_in_trash' => __('No phones found in the Trash'), 

    'parent_item_colon' => '', 

    'menu_name'   => 'Phones' 

); 

    $args = array(

    'labels'  => $labels, 

    'description' => 'Holds our phones and phones specific data', 

    'public'  => true, 

    'menu_position' => 5, 

    'supports'  => array('title', 'editor', 'thumbnail', 'excerpt', 'comments'), 

    'has_archive' => true, 

); 

    register_post_type('phones', $args); 

} 

add_action('init', 'my_custom_phones'); ?> 

自定義分類代碼

<?php 
function my_taxonomies_Phones() { 
    $labels = array(
    'name'    => _x('Phones Categories', 'taxonomy general name'), 
    'singular_name'  => _x('Phones Category', 'taxonomy singular name'), 
    'search_items'  => __('Search Phones Categories'), 
    'all_items'   => __('All Phones Categories'), 
    'parent_item'  => __('Parent Phones Category'), 
    'parent_item_colon' => __('Parent Phones Category:'), 
    'edit_item'   => __('Edit Phones Category'), 
    'update_item'  => __('Update Phones Category'), 
    'add_new_item'  => __('Add New Phones Category'), 
    'new_item_name'  => __('New Phones Category'), 
    'menu_name'   => __('Phones Categories'), 
); 
    $args = array(
    'labels' => $labels, 
    'hierarchical' => true, 
); 
    register_taxonomy('Phones_cat', 'Phones', $args); 
} 
add_action('init', 'my_taxonomies_Phones', 0); 
?> 

使用下面的查詢代碼,而不是你的查詢代碼,下面的查詢顯示類別明智的文章列表。

<?php 
$catid = $wp_query->get_queried_object_id(); /* get category id of current category */ 

    $args = array(
     'posts_per_page' => 20, 
     'offset' => 0, 
     'tax_query' => array(
      array(
       'taxonomy' => 'Phones_cat', /* Your custom post type category name*/ 
       'field' => 'term_id', 
       'terms' => $catid, 
      ), 
     ), 
     'orderby' => 'rand', 
     'post_type' => 'phones', 
     'post_status' => 'publish' 
    ); 
    $queryall = new WP_Query($args); 

    ?> 
+0

- 請在您的主題文件中添加此代碼之前備份您當前的文件或代碼。 – shivlal

相關問題