如果之前有人问过我这个问题,我很抱歉,但我找不到另一个让我朝着正确方向前进的问题。
我正在一个页面上创建一系列多个循环。我还为帖子创建了另一个分类法“问题”。我试图创建的循环之一是,我想查询这个分类法和一个特定的类别“特色”。但更进一步,我首先需要找出“最新”的问题,然后用一个来抵消。
示例:在我的自定义分类法中,我有术语“问题1”、“问题2”和“问题3”。全部按时间顺序创建。这个循环应该找到这个分类法中的最新术语(读作:highest ID),然后将其从循环中排除,然后给出剩下的内容,只显示类别“Featured”中的帖子。
以下是迄今为止我编写的代码:
$issue = get_terms(\'issue\', array(
\'orderby\' => \'none\',
\'order\' => \'DESC\',
\'number\' => 1,
\'offset\' => 1
)
);
$latest_issue = $issue->slug;
$args = array(
\'post_type\' => \'post\',
\'posts_per_page\' => \'5\',
\'order_by\' => \'date\',
\'order\' => \'DESC\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array( \'featured\' ),
),
array(
\'taxonomy\' => \'issue\',
\'field\' => \'slug\',
\'terms\' => array( $latest_issue )
)
)
);
$query = new WP_Query( $args );
echo \'<div class="home-middle widget-area">\';
echo \'<h4 class="widget-title widgettitle"><span>From the Archives</span></h4>\';
if (have_posts()) {
while( $query->have_posts() ) { $query->the_post();
echo \'<h4>\' . get_the_title() . \'</h4>\';
}
} else {
echo \'No posts in the Archive\';
}
echo \'</div>\';
wp_reset_postdata();
UPDATE EDIT
感谢@adelval指出查询中的错误。我已经更新了这一点,并取得了一些进展。我现在的问题是,我的查询只获取最新(我指的是具有最高分类ID的最新)分类术语中的帖子,然后将其显示5次。
You can see it in action, at the bottom of the page, here (在档案的标题下)。下面是新代码:
$issue = get_terms(array(
\'taxonomy\' => \'issue\',
\'hide_empty\' => false,
\'orderby\' => \'id\',
\'order\' => \'DESC\',
\'number\' => 1
)
);
$latest_issue = $issue[0]->slug;
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
\'relation\' => \'AND\',
array(
\'taxonomy\' => \'category\',
\'field\' => \'slug\',
\'terms\' => array( \'featured\' ),
),
array(
\'taxonomy\' => \'issue\', // My Custom Taxonomy
\'terms\' => array( $latest_issue ), // My Taxonomy Term that I wanted to exclude
\'field\' => \'slug\', // Whether I am passing term Slug or term ID
\'operator\' => \'NOT IN\',
)
)
);
$query = new WP_Query( $args );
echo \'<div class="home-middle widget-area">\';
echo \'<h4 class="widget-title widgettitle"><span>From the Archives</span></h4>\';
if ( $query->post_count > 0 ){
foreach ( $query->get_posts() as $post ):
echo \'<article class="post type-post status-publish format-standard has-post-thumbnail entry">\';
echo \'<a href="\' . get_the_permalink() . \'" class="alignleft" aria-hidden="true">\';
echo the_post_thumbnail( \'home-middle\' );
echo \'<time class="entry-time" itemprop="datePublished">\' . get_the_date() . \'</time>\';
echo \'</a>\';
echo \'<h4>\' . get_the_title() . \'</h4>\';
echo get_the_excerpt();
echo \'</article>\';
endforeach;
} else {
echo \'No posts in the Archive\';
}
echo \'</div>\';
wp_reset_postdata();
为了明确我要实现的目标,此函数应该:
获取分类问题的所有术语完成确定最新的分类术语并将其设置为变量(我目前有“第1期”、“第2期”和“第3期”,均按时间顺序创建。“第3期”是最新的分类术语)完成创建一个同时使用类别和问题的查询;在该查询中,排除在上面的#2中找到的分类术语需要改进谢谢您的帮助!