1

I'm building a custom homepage for a client where they want to have a few changeable boxes to link to specific pages, or posts, within the site. I've added custom fields to the homepage so that they need only enter the page/post ID, and then it will display the proper post or page. And I'd like it to be flexible enough that they can do a post or page.

Right now, my code for the box is

<?php query_posts('p='.$topright); ?>
<?php while (have_posts()) : the_post(); ?>
     {title and featured image}
<?php endwhile; ?>

where $topright is a variable already defined. (I've tested the variable with an echo and it is returning the proper ID number.)

Unfortunately, WordPress seems to require that I use p=ID if it is a post, and page_id=ID if it is a page. So, if I designate the ID for a post, it is working fine, but not if I desingate the ID for a page. Is there an alternative syntax I could use? Or, it there a conditional of some kind that might look a the ID and recognize if it is a post or a page so I could run the query with an IF ELSE?

2 Answers 2

6

Try this:

query_posts(array(
    'p' => $topright,
    'post_type' => array('post', 'page'),
));
1
  • Perfect. Fixed this exact problem I was having.
    – Ian
    Jun 17, 2012 at 5:55
2

you can use 'post__in' in your query and if you are creating a custom query you should use WP_Query or get_posts and not query_posts , so something like this:

$my_query = new WP_Query();
$my_query->query(array( 'post__in' => array($topright)));
while ($my_query->have_posts()) : $my_query->the_post(); ?>
     {title and featured image}
<?php endwhile; ?>

Hope this helps

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.