我以“电话”的名称创建了一个自定义帖子类型。在类别中,我有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; ?>
但是,当我在不同类别中添加多个电话时,所有电话都归入一个类别。
我真的很困惑 请帮我 :(
请在您的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] 删除。
我来说两句