我见过的所有代码示例都涉及父类和子猫。但是,我所拥有的是如此类别(简化):
-Towns
--NY
--Chicago
--LA
-Restaurants
-Theatres
-Stores
现在,我为芝加哥创建了一个页面。在它上面我想展示与芝加哥相匹配的所有其他物品:
-Restaurants
--restaurant 1
--restaurant 2
--restaurant 3
-Stores
--store 1
--store 2
etc
我该怎么做?在模板中,我想我必须从当前页面的URL获取slug并首先查询该类别(芝加哥),然后循环其他但我不完全确定如何。
编辑:我还需要反过来,显示按城市分组的所有国家/地区的餐馆。答案 0 :(得分:0)
您可以尝试其他方法。 创建自定义帖子类型:餐馆,商店等。示例:
add_action( 'init', 'create_post_type' );
function create_post_type() {
register_post_type( 'restaurants',
array(
'labels' => array(
'name' => __( 'Restaurants' ),
'singular_name' => __( 'Restaurants' )
),
'public' => true,
'has_archive' => true,
)
);
}
然后您为芝加哥创建一个页面,为餐厅1,餐厅2,餐厅1类型的餐厅创建条目,并将所有条目分配到芝加哥类别。
现在,在芝加哥的模板中,您可以显示分配给芝加哥类别的所有餐馆类型条目。
//add this after the Loop
query_posts( array(
'post_type' => 'restaurants',
'category_name' => 'chicago', //use category slug
'posts_per_page' => -1,
'post_status' => 'publish',
) );
// The Loop
while ( have_posts() ) : the_post();
echo '<li>';
the_title();
echo '</li>';
endwhile;
// Reset Query
wp_reset_query();
您也可以使用category_name
参数代替cat
参数,该参数需要类别ID。