I want to display the last say 6 pieces of work from my portfolio. So i have written this query

<?php $the_query = new WP_Query array( 'category_name' => 'portfolio', 'showposts=6' ); ?>

<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>

<?php the_post_thumbnail(); ?> 

<?php endwhile;?>

Can someone advise if im missing something as it doesnt seem to be working. thanks

<?php $the_query = new WP_Query array( 'category_name' => 'portfolio', 'posts_per_page' => 6, ); ?>

This now comes up with a T Array error


There are some errors in your code, you have to change:

  • $the_query -> have_posts() to $the_query->have_posts()
  • $the_query -> the_post() to $the_query->the_post()
  • WP_Query array(...) to WP_Query(array(...))
  • 'showposts=6' to 'posts_per_page' => 6

Try this instead

$the_query = new WP_Query( array( 'category_name' => 'portfolio', 'posts_per_page' => 6 ) ); 
while ($the_query->have_posts()) : $the_query->the_post(); 
| improve this answer | |
  • Hi Bigire, thankyou so much for that assistance :) – Kirsty Marks Jun 5 '13 at 9:05
  • Hi there, This completely wiped everything :S....now no image or text unlike my first query that was showing this – Kirsty Marks Jun 5 '13 at 9:07
  • Hi Kirsty, remember to change WP_Query array(...) to WP_Query(array(...)), otherwise you will get a PHP parse error. – birgire Jun 5 '13 at 9:14
  • Hi Birgire, I did this and it still didnt work :S – Kirsty Marks Jun 5 '13 at 9:20
  • actually think i know why XD – Kirsty Marks Jun 5 '13 at 9:21

You're missing the enclosing brackets when you instantiate the new WP_Query class! It should be $the_query = new WP_Query( array('category_name' => 'portfolio', ... ) );

See the usage section of the documentation for Class_Reference/WP_Query on the WordPress codex for more information, and birgire's answer for the correct code solution!

Also please note that the arguments array passed in to the class is in an associative structure and requires the appropriate syntax (e.g. array( 'arg_name_1' => 'arg_val_1', arg_name_2' => 2 );

In this particular case though I also think that 'showposts' is incorrect, you should be using the 'posts_per_page' argument (e.g. 'posts_per_page' => 6) -- see the source file reference on the gist.github page

| improve this answer | |
  • Hi Chris, thanks for this, sorry first time at writing queries so im sketchy XD – Kirsty Marks Jun 5 '13 at 9:04
  • Hey no worries, (sorry) mine was a bit of a sketchy answer ;) -- at least between you me and the awesome Mr @birgire here we should have your problem all straightened out! – Chris Jun 5 '13 at 9:11
  • <?php $the_query = new WP_Query array( 'category_name' => 'portfolio', 'posts_per_page' => 6, ); ?> this came up with a T Array error? – Kirsty Marks Jun 5 '13 at 9:11
  • Still missing the WP_Query enclosing brackets!! -- new WP_Query( array( ... ) ); ... seriously @birgire got it right on the nose for you, but hopefully the reference links I provided should offer some more insight and clarity... if you study them! XD – Chris Jun 5 '13 at 9:12
  • there needs to be more user friendly dumb tutorials for people like me on wordpress lol! – Kirsty Marks Jun 5 '13 at 9:12

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.