我有一个页面,例如示例页面。我在那个页面上显示了3篇帖子。所有3个帖子都有2个标签,所以总共有6个标签。
现在,我想在示例页面中显示所有这6个标记,我尝试了以下代码:
<?php echo wp_get_post_tags(1 , $args ) ?>
但上面的代码打印Array 而不是post tag实际名称。上面1是我的帖子id。我还想知道如何传递我想要检索的标签的所有帖子id。我有一个页面,例如示例页面。我在那个页面上显示了3篇帖子。所有3个帖子都有2个标签,所以总共有6个标签。
现在,我想在示例页面中显示所有这6个标记,我尝试了以下代码:
<?php echo wp_get_post_tags(1 , $args ) ?>
但上面的代码打印Array 而不是post tag实际名称。上面1是我的帖子id。我还想知道如何传递我想要检索的标签的所有帖子id。首先,你必须知道你想要检索什么,你必须为你想要检索的那组帖子找到一些相同的东西。使用以下代码检索帖子,其中$args
是包含用于选择正确的post ID的参数的变量。
$posts = get_posts($args);
$terms=array();
foreach($posts as $post) {
//Get all terms for the retrieved post IDs
$terms[]=wp_get_post_tags($post->ID);
}
foreach($terms as $term) {
for($i=0;$i<=count($term);$i++;) {
print $term[$i]->name; //the output
}
}
我还没有测试代码,但这基本上就是您需要的。它将输出所有标记的名称。了解哪些帖子必须显示标签
您需要为每个帖子执行此操作,如何存档帖子ID取决于您如何获取它们(loop 或列表或。wp_get_post_tags() 只能用于一个id。因此需要调用它3次,才能调用一个解决方案
$id1=1;
$id2=2;
$id3=3;
$tag_ids = array();
$tag_ids[] = wp_get_post_tags( $id1, array( \'fields\' => \'name\' ) );
$tag_ids[] = wp_get_post_tags( $id2, array( \'fields\' => \'name\' ) );
$tag_ids[] = wp_get_post_tags( $id3, array( \'fields\' => \'name\' ) );
$tag_ids[] = array_unique($tag_ids);
将为您提供一个仅包含标记的数组。。。$result="";
foreach ( $tag_ids as $value ) {
$result=$result." ";
}
echo $result;
这将输出使用的所有标记的纯文本字符串我想您已经创建了一个页面模板来在页面上显示帖子。您可以尝试使用此代码显示您的帖子及其所有标记。将此代码粘贴到页面模板上,并从admin创建页面,然后选择已创建的模板。
<?php
$the_query = new WP_Query( \'showposts=5\' );
while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(__(\'(more…)\')); ?>
<?php the_tags(); ?>
<?php endwhile; ?>
如何获取用户标签列表,我使用以下内容获取所有标签的列表:<?php $tags = get_tags(); ?> <?php if (isset($tags)): ?> <ul> <?php foreach ($tags as $tag): ?> <li><?php echo $tag->name; ?></li>&#