【发布时间】:2018-12-11 14:57:37
【问题描述】:
我在帖子底部显示具有相同标签的相关帖子,但我还想添加一个没有标签的随机帖子,例如“尝试这个完全不同的东西”。
我已尝试使用 tag__not_in,但我的代码不起作用:
$tag_id = get_queried_object()->term_id;
$args = [
'post__not_in' => array( get_queried_object_id() ),
'tag__not_in' => array( $tag_id ),
'posts_per_page' => 1,
'orderby' => 'rand'
];
$my_query = new wp_query( $args );
if( $my_query->have_posts() ) {
while( $my_query->have_posts() ) {
$my_query->the_post(); ?>
对不起,我真的不知道如何编码,我只是尝试理解 Wordpress 的代码以及如何修改它。
如何将帖子标签 id 放入数组中?
更新>这有效:
global $post;
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
// just the test echo $tag->slug;
$tag = get_term_by('name', $tag->slug, 'post_tag');
$args = array(
'posts_per_page' => 1,
'tag__not_in' => array($tag->term_id),
'orderby' => 'rand',
);
}
}
$query = new WP_Query($args);
【问题讨论】:
标签: wordpress