Php 尝试使用当前帖子的标签查询帖子
我正在创建一个具有多个帖子关系的网站。对于帖子之间的交叉引用,我使用wordpress标签。现在,当我在一篇有多个标签的文章上时,我想查询所有有两个标签的文章。所以当前的帖子有Php 尝试使用当前帖子的标签查询帖子,php,wordpress,Php,Wordpress,我正在创建一个具有多个帖子关系的网站。对于帖子之间的交叉引用,我使用wordpress标签。现在,当我在一篇有多个标签的文章上时,我想查询所有有两个标签的文章。所以当前的帖子有'tag1'和'tag2',应该有一个只有这两个标签的帖子列表 请参阅下面的代码以查看我的非工作解决方案。我想这将是一个好主意,以获得当前标签ID的列表。为此,我使用了Wordpress codex提供的标准解决方案,然后创建了一个可用于查询的自定义函数。不幸的是,这似乎不是正确的解决方案,函数listtags会按预期输出
'tag1'
和'tag2'
,应该有一个只有这两个标签的帖子列表
请参阅下面的代码以查看我的非工作解决方案。我想这将是一个好主意,以获得当前标签ID的列表。为此,我使用了Wordpress codex提供的标准解决方案,然后创建了一个可用于查询的自定义函数。不幸的是,这似乎不是正确的解决方案,函数listtags会按预期输出ID
<?php
function list_tags(){
$posttags = get_the_tags();
foreach($posttags as $tag) {
echo $tag->term_id . ' ';
}
}
$listtags = list_tags();
echo $listtags . ' ';
$tag_query = new WP_Query( array(
'post_type' => 'les',
'order' => 'ASC',
'tag__and' => array( $post_tag ),
) );
// The Loop
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post(); ?>
<div class="les-container" style="background-color: red; height:200px;">
<div class="container">
<div class="row posts-align">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
<?php } wp_reset_postdata();
} else {
// no posts found
}?>
列表_标签()代码>函数似乎工作正常,因为它按预期输出标记。但是,在查询中插入它时,它似乎不起作用。它只输出所有贴子,而不考虑标签。tag\u slug\u,用于slug。标记_u_,用于ID。你可以在这里找到更多信息-我有一个有效的解决方案。希望这对某人有帮助
代码:
<?php
$tags = array();
$posttags = get_the_tags();
if ($posttags) {
foreach($posttags as $tag) {
$tags[] = $tag->term_id;
}
}
$tag_query = new WP_Query( array(
'post_type' => 'les',
'order' => 'ASC',
'tag__and' => $tags,
'post_parent' => 0,
) );
// The Loop
if ( $tag_query->have_posts() ) {
while ( $tag_query->have_posts() ) {
$tag_query->the_post(); ?>
<div class="les-container" style="background-color: red; height:200px;">
<div class="container">
<div class="row posts-align">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
</div>
<?php }
wp_reset_postdata();
} else {
// no posts found
}?>
此外,我不认为$post_标记是一个变量。您有$posttags=get_the_tags();嗨,谢谢!我知道标签和ID,这就是我正在使用(或试图使用)的内容。关于你的第二个评论,这个变量确实是错误的。这是我正在努力解决的问题。