我的自定义帖子类型未按照wordpress中的类别显示

时间:2016-08-20 11:08:21

标签: php wordpress

我创建了一个名为“手机”的自定义帖子类型。在类别I中我有4个品牌名称,我希望每个手机在类别页面中都有一个类别。

我的功能代码:

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

但是当我在不同的类别中添加了几部手机时,所有的手机都进入了一个类别。

我真的很困惑。请帮帮我:(

1 个答案:

答案 0 :(得分:-1)

  • 我们检查了您的自定义帖子类型和自定义分类代码,我认为您的自定义分类代码中存在问题,您的自定义分类名称是“类别”,因为“类别”是WordPress默认的分类后名称。

请在您的functions.php文件中添加以下代码,以在wordpress中创建自定义帖子类型和自定义词典。

自定义帖子类型

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

    ?>