i have a posts tag with 'php'.....i am using this code to show all posts tag with 'php' but it not work

<?php query_posts('tag=$tag');
if ( have_posts() ) : while ( have_posts() ) : the_post();
endwhile; endif ;
  • 1
    Why you are not using WP_query or get_posts ? – Sumit Jan 23 '16 at 9:52
  • any example will be helpful for me. – Bilal Raj Jan 23 '16 at 10:04

It is not recommended to use query_posts. But this is not the problem, Problem is you are using PHP variable in single quotes. 'tag=$tag'

Consider these examples:

query_posts("tag=$tag"); //$tag should output: php

Using WP_query //Recommended

$query = new WP_Query(array( 'tag' => 'php' ));

Ref: WP_Query-Tag_Parameters | query_posts

  • Thanks for your suggestion and answer.....but it not work.It is possible you post full code. – Bilal Raj Jan 23 '16 at 10:32
  • I gave you three suggestions. If all of the three are not working then I am sorry I will go back to school to learn code :D Please try to debug your problem only then someone can help you here :) – Sumit Jan 23 '16 at 10:43

There is some changes in code try this

$query = new WP_Query(array( 'tag' => 'php' ));
if ( $query->have_posts() ) : 
  while ( $query->have_posts() ) : $query->the_post();
    the_content() ;
endif ;
  • after adding this code it makes blank screen...no out put......well i think today is my BAD DAY...simple things goes wrong..btw thanks for this effort. – Bilal Raj Jan 23 '16 at 16:07
  • ohh. just one change that was syntax error after the_content() add " ; " my bad.. – Harsh Pandya Jan 25 '16 at 6:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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