嗨,我不确定这个问题是否可以理解,但我会解释我想要达到的目标。我有一个称为菜单项的自定义帖子类型和称为菜单项类型的自定义分类法。
密码
function create_menu_post_type()
{
$labels = array(
\'name\' => __( \'Food Menu\',\'framework\'),
\'singular_name\' => __( \'Menu Item\',\'framework\' ),
\'add_new\' => __(\'Add New\',\'framework\'),
\'add_new_item\' => __(\'Add New Menu Item\',\'framework\'),
\'edit_item\' => __(\'Edit Menu Item\',\'framework\'),
\'new_item\' => __(\'New Menu Item\',\'framework\'),
\'view_item\' => __(\'View Menu Item\',\'framework\'),
\'search_items\' => __(\'Search Menu Items\',\'framework\'),
\'not_found\' => __(\'No Menu Item found\',\'framework\'),
\'not_found_in_trash\' => __(\'No Menu Item found in Trash\',\'framework\'),
\'parent_item_colon\' => \'\'
);
$args = array(
\'labels\' => $labels,
\'public\' => true,
\'exclude_from_search\' => false,
\'publicly_queryable\' => true,
\'show_ui\' => true,
\'query_var\' => true,
\'menu_icon\' => get_template_directory_uri() . \'/images/menu.png\',
\'capability_type\' => \'post\',
\'hierarchical\' => false,
\'menu_position\' => 5,
\'supports\' => array(\'title\',\'editor\',\'thumbnail\'),
\'rewrite\' => array( \'slug\' => __(\'menu-item\', \'framework\') )
);
register_post_type(\'menu-item\',$args);
}
add_action( \'init\', \'create_menu_post_type\' );
function create_menu_item_type_taxonomy(){
$labels = array(
\'name\' => __( \'Menu Item Types\', \'framework\' ),
\'singular_name\' => __( \'Menu Item Type\', \'framework\' ),
\'search_items\' => __( \'Search Menu Item Types\', \'framework\' ),
\'popular_items\' => __( \'Popular Menu Item Types\', \'framework\' ),
\'all_items\' => __( \'All Menu Item Types\', \'framework\' ),
\'parent_item\' => __( \'Parent Menu Item Type\', \'framework\' ),
\'parent_item_colon\' => __( \'Parent Menu Item Type:\', \'framework\' ),
\'edit_item\' => __( \'Edit Menu Item Type\', \'framework\' ),
\'update_item\' => __( \'Update Menu Item Type\', \'framework\' ),
\'add_new_item\' => __( \'Add New Menu Item Type\', \'framework\' ),
\'new_item_name\' => __( \'New Menu Item Type Name\', \'framework\' ),
\'separate_items_with_commas\' => __( \'Separate Menu Item Types with commas\', \'framework\' ),
\'add_or_remove_items\' => __( \'Add or Remove Menu Item Types\', \'framework\' ),
\'choose_from_most_used\' => __( \'Choose from the most used Menu Item Types\', \'framework\' ),
\'menu_name\' => __( \'Menu Item Types\', \'framework\' )
);
register_taxonomy(
\'menu-item-type\',
array( \'menu-item\' ),
array(
\'hierarchical\' => true,
\'labels\' => $labels,
\'show_ui\' => true,
\'query_var\' => true,
\'rewrite\' => array(\'slug\' => __(\'menu-item-type\', \'framework\'))
)
);
}
add_action( \'init\', \'create_menu_item_type_taxonomy\', 0 );
我在“日本人”一词下创建了一个名为“寿司”的帖子。然后在“中国人”一词下创建了名为“周眉”的帖子。我想用不同的术语填充归档帖子。我有一个归档页面,即归档。我在php中填充帖子,但我的代码填充所有术语中的所有帖子。
<?php $loop = new WP_Query( array( \'post_type\' => \'menu-item\', \'posts_per_page\' => -1 ) ); ?>
<?php while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( \'content\', get_post_format() );
endwhile; wp_reset_query();
?>
<?php else : ?>
<?php get_template_part( \'content\', \'none\' ); ?>
<?php endif; ?>
我正在通过动态侧栏导航到存档页
<?php dynamic_sidebar( \'Archive\' ); ?>
请注意,我使用了来自主题的自定义帖子类型和分类代码作为参考。此外,如果这是一个无效的问题,请随时发表评论,我将删除该问题,并尝试找到其他解决方案。
添加更清晰的示例
Custom post -> menu-item
menu-item -> 2 posts -> sushi, chow mein
taxonomy -> menu-item-type
menu-item-type -> japenese, chinese
sushi -> japenese
chowmein -> chinese
已更新的查询
<?php
$current_term = get_term_by( \'slug\', get_query_var(\'term\') ,\'menu-item-type\' );
$loop = new WP_Query( array( \'post_type\' => \'menu-item\', \'posts_per_page\' => -1, \'year\' => $year_filter, \'tax_query\' => array(
array( \'taxonomy\' => \'menu-item-type\', \'field\' => \'slug\', \'terms\' => $current_term->name) ) ) ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( \'content\', get_post_format() );
endwhile; wp_reset_query();?>
<?php else : ?>
<div class="no-post"><?php _e( \'Sorry, no posts matched your criteria.\' ); ?></div>
<?php endif; ?>