0

I am having one page for example sample page. I am displaying 3 posts in that page. All 3 posts having 2 tags so total 6 tags.

Now I want to display all those 6 tags in sample page , I have tried below code :

 <?php echo wp_get_post_tags(1 , $args ) ?>

But above code printing Array instead of post tag actual name. Above 1 is my post id. Also I want to know that how I can pass all post ids of which tags I want to retrieve.

3 Answers 3

0

First you've to know what you want to retrieve, you will have to find something identical for that group of post you want to retrieve. Use the following code to retrieve the post, where $args is the variable what contains the arguments to select the right post ids.

$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
    }
}

I didn't test the code yet, but this is basically what you need. It will output names of all the tags.

  1. Get to know of which posts their tags have to be shown.
  2. Store the IDs and use them to retrieve the tags of the specific posts.
  3. Put it all in one array and iterate through it.
0

You need to do it for each post, how to archive the post ids depends on how you get them (loop or list or ... ). wp_get_post_tags() only can be used for one id. therefore it needed to be called 3times one solution could be

$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);

will give you an array with just the tags ...

$result="";
foreach ( $tag_ids as $value ) {
    $result=$result." ";
}
echo $result;

this will output a plain text string of all tags used

1
  • It is not printing anything when I put code.. Commented Feb 6, 2015 at 16:50
0

I think you have created a page template to display posts on a page. You can try once this code to display your post with all its tags. Paste this code on your page template and create a page from admin and select the templates which you have created.

<?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; ?>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.