IF HOT NEWS IS A TAG (FIRST QUESTION ASKED FOR TAGS)
您必须使用WP查询,其中您可以查询带有标记“quot;热门新闻”;并按帖子ID的降序排列结果,然后返回1篇帖子,如下所示:
$query_args = array(
\'post_type\' => \'your_post_type\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'your_tag_taxonomy\',
\'field\' => \'slug\',
\'terms\' => \'hot_news\'
)
),
\'order_by\' => \'ID\',
\'order\' => \'DESC\',
\'posts_per_page\' => 1
);
// Fire WP post query
$my_query = new WP_Query( $query_args );
// Check if at least one matching post was found
if ( $my_query->have_posts() ) {
// If yes, iterate through the found post(s)
while ( $my_query->have_posts() ) {
// Forward the post iterator to the currently iterated post of the loop.
// Like this you\'ll be able to get the iterated posts\' content via the
// ```get_the...``` etc. functions
$my_query->the_post();
$posts_title = get_the_title();
$posts_excerpt = get_the_excerpt();
$link = get_the_permalink();
echo $posts_title." ".$posts_excerpt." ".$link;
}
}
// Reset the global $post object (should always be done after post queries)
wp_reset_postdata();
IF HOT NEWS IS A META KEY (LATER ASKED FOR META)如果要在metavalues函数中进行查询,则需要按照以下方式制定查询参数:
$query_args = array(
\'post_type\' => \'your_post_type\',
\'meta_query\' => array(
array(
\'key\' => \'hotnews-status\',
\'value\' => 1,
\'type\' => \'NUMERIC\'
)
),
\'order_by\' => \'ID\',
\'order\' => \'DESC\',
\'posts_per_page\' => 1
);
这两个查询背后的逻辑如下:每次创建新帖子时,该帖子都会在数据库中获得一个与之关联的自动递增ID值(意思是=最近添加帖子的ID+1)。要获取最新的帖子,请根据帖子ID按降序排列查询到的帖子,然后返回1篇帖子(查询的posts\\u per\\u page参数)。是的,然后添加任何额外需要的过滤器,例如标记/类别/分类法/元值/任何过滤器。清楚的