从wp-post中获取以特定字母开头的标签片段,然后在qp_query中使用它们



我正在尝试创建一个标签列表,可以在wordpress wp_query中的数组中使用。我已经在模板中显示了标记(通过echo(,所以我知道我有输出,但我不知道如何将输出移动/使用到单独的wp_query的数组中。我通常可以通过搜索找到如何做事,但我不知道我想做的事情叫什么名字。我自学成才。

$commatext = ",";
$blankcat = "blankcategory";
$tags = wp_get_post_tags( $post->ID );
if ( !empty( $tags ) && !is_wp_error( $tags ) ):
foreach ( $tags as $tag ): if(strpos($tag->slug,'sim-') !== false): echo $tag->slug; echo $commatext; endif; endforeach;
echo $blankcat;
endif;

该输出:

sim-simname1,sim-simname2,blankcategory

我需要弄清楚如何将其放入同一篇文章的wp_query中:

'tag' =>  array( sim-simname1,sim-simname2,blankcategory )

我现在确实有它的工作,但我必须手动键入256个可能的标签与模拟。。。插入我的wp_query。自动化会很好。。。

在一个普通数组中构建一个收集的模拟程序列表,并将其在一个关联数组(映射(中传递给WP_Query。

未经测试但类似。。。

if ( !empty( $tags ) && !is_wp_error( $tags ) ):
$simarr = array();
foreach ( $tags as $tag ):
if(strpos($tag->slug,'sim-') !== false):
$simarr[] = $tag->slug;  // push 'sim-' into array
endif;
endforeach;
$simarr[] = "blankcategory";     // add to end of array
$query = new WP_Query( array('tag' => $simarr) );
endif;

希望很接近。

Dave

相关内容

最新更新