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

萨贾德

我以“电话”的名称创建了一个自定义帖子类型。在类别中,我有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; ?>

但是,当我在不同类别中添加多个电话时,所有电话都归入一个类别。

我真的很困惑 请帮我 :(

希夫拉尔
  • 我们已经检查了您的自定义帖子类型和自定义分类法代码,并且我认为您的自定义分类法代码中存在问题,您的自定义分类法名称为“类别”,因为“类别”是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);

    ?>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

显示自定义帖子类型类别 (Wordpress)

WP_Query以在Wordpress中按类别显示帖子(自定义帖子类型)

自定义帖子类型中的Wordpress列表类别

WORDPRESS:使用自定义帖子类型显示自定义分类法中某个类别的帖子

类别未按自定义帖子类型过滤

在WordPress的自定义帖子类型的永久链接中仅显示特定类别

在Wordpress中显示自定义帖子类型的内容

在 wordpress 自定义帖子类型查询中获取当前帖子类别 slug

在循环中显示自定义帖子类型(专门针对该帖子)的WordPress类别

显示自定义帖子类型存档的类别和帖子标题-Wordpress

自定义帖子类型“单个”不会在wordpress中显示我的代码

自定义帖子类型Wordpress按类别查询

自定义帖子类型默认类别wordpress

从自定义帖子类型 Wordpress 的类别中获取帖子

WordPress的自定义帖子类型帖子不显示

Wordpress 自定义帖子类型显示类别的图像

WordPress自定义帖子类型类别列表未显示

如何在WordPress中自定义帖子类型的自定义类别的帖子循环?

在Wordpress Gutenberg自定义块中显示自定义帖子类型的列表

从Wordpress中的自定义帖子类型的类别中获取ACF文本字段值

从自定义帖子类型中获取类别

禁用Wordpress中自定义帖子类型的类别选项

自定义帖子类型类别中的WordPress``圆形''分页

获取自定义帖子类型 [Wordpress] [活动管理器] 中的每个类别

WordPress自定义帖子类型帖子未在管理员中显示

来自自定义帖子类型的特定子类别的 Wordpress 查询帖子

排序和显示自定义帖子类型Wordpress

停止显示自定义帖子类型的wordpress搜索

WordPress自定义帖子类型查询显示