1

For some reason the following code is displaying both types of posts instead of just the specified post using query_posts. I am not quite sure what is going on, but it appears that the loop is ignoring my condition of is_page('news') or is_page('othernews'). Does anyone have an idea why this might be the case?

<?php 
    if (is_page('news')) :
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts('news');
    endif;

    if (is_page('othernews')) :
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        query_posts('my-other-news'); ?>
    endif;

    while (have_posts()) : the_post();
        get_template_part( 'part-post');
    endwhile;
?>
Ravi Patel
  • 5,121
  • 2
  • 25
  • 44
Pdnell
  • 321
  • 4
  • 16
  • In which page are you getting the problem.? That is whether your page slug is "news" or "othernews"..? Or else are you listing this any other pages..? If so the problem might be it is returning from the default query post arguments. – Harish Kanakarajan Apr 04 '14 at 11:36
  • take a look on the link to detect he current page, this might be helpful http://stackoverflow.com/questions/4837006/how-to-get-the-current-page-name-in-wordpress – jogesh_pi Apr 04 '14 at 11:44

4 Answers4

1

Your conditional statement is incorrect. If you need one or the other, you need to do something like this

if(is_page('news')) {
<--- do something for news --->
}elseif(is_page('othernews')) {
<---do something for othernews--->
}

And PLEASE PLEASE PLEASE, don't use query_posts, it is evil. Rather use WP_Query

Pieter Goosen
  • 9,768
  • 5
  • 35
  • 55
1

Try this whether this works for you,

<?php 
    if (is_page('news')) :
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
        $query = WP_query(array('post_type' => 'news')); 
    else if (is_page('othernews')) :
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
       $query = WP_query(array('post_type' => 'other-news')); 
    else 
        $query = WP_query(array('post_type' => 'post')); 
    endif;

    while ($query->have_posts()) : $query->the_post();
        get_template_part( 'part-post');
    endwhile;
?>

The last else condition is to handle if none of the first 2 condition satisfies. So you can remove it if you no need that.

Hope this helps you.

  • This does help... I think it's the right direction, however when I paste this into my code it breaks the page - I just get a blank page. I'm assuming this is a syntax error in your code, because the page works fine when I take it out. I have pasted over the existing loop btw. – Pdnell Apr 04 '14 at 22:56
  • 1
    Oh Sorry, I have forgot to close the brackets at the end of WP_query. I have updated the new code above. Please use it. – Harish Kanakarajan Apr 05 '14 at 08:59
  • It's still not working :( Am I placing this before ` ` – Pdnell Apr 05 '14 at 16:26
  • 1
    There's still an unclosed parenthesis around the first WP_Query. – Tim Sheehan Jun 10 '21 at 13:51
0
<?php 

if (is_page('news')) {
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  query_posts('news'); 
}

if (is_page('othernews')) {
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  query_posts('my-other-news'); }

?>

    <?php while (have_posts()) : the_post(); ?>             
    <?php get_template_part( 'part-post'); ?>   
<?php endwhile; ?>

If you simplify the code like above, does that help? Seems to be a lot of unnecessary php tags there.

Charlie74
  • 2,883
  • 15
  • 23
0

If You need to query a particular page post alone means you need to specify post id in the query and the post type name

query_posts('p=postid&&post_type=posttypename'); 

Or if You need to query all post under a post type use this

 if (is_page('research-2')) :
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     query_posts('post_type=research');
    endif;

    if (is_page('issues')) :
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     query_posts('post_type=studies'); 
    endif;

    while (have_posts()) : the_post();
     the_title();
    endwhile;
ManoharSingh
  • 487
  • 4
  • 27