我有一个自定义的帖子类型,叫做books
. 此自定义帖子类型有一个名为book_category
. 截至目前,在可预见的未来,每本书都有5个类别可供筛选。
现在,这些类别中的每一个都有各自的页面,该页面将根据各自的类别(以及与每个类别相关的其他辅助信息)查询书籍。
在下面的代码中,我尝试根据is_page()
. 虽然这确实有效。。。有些事情告诉我有一种更有效/更恰当的方法来处理这个问题。
<?php
if (is_page(\'horror\')) {
$theTermBasedOnPage = \'horror\';
} elseif (is_page(\'comedy\')) {
$theTermBasedOnPage = \'comedy\';
} elseif (is_page(\'romantic\')) {
$theTermBasedOnPage = \'romantic\';
} elseif (is_page(\'nonfiction\')) {
$theTermBasedOnPage = \'nonfiction\';
} elseif (is_page(\'drama\')) {
$theTermBasedOnPage = \'drama\';
}
$args = array(
\'posts_per_page\' => -1,
\'post_type\' => \'books\',
\'post_status\' => \'publish\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'book_category\',
\'terms\' => $theTermBasedOnPage,
),
),
);
?>
What is the best way to query posts (Custom Post Type > Taxonomy) based on page?