2016-08-20 95 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

相关问题