Forums

[resolved] "get_posts(array" troubles... (9 posts)

  1. relaysignal
    Member
    Posted 1 year ago #

    I'm trying to get my Category Template to be able to not only show the posts from that category, but to also show a single post from the 'featured' category as well. But it's not just any post from the featured category, it's the most recent post that happens to be in both the featured category and the category the page is on. Does that make sense? Here's my code for the single (Featured + Category) post:

    [Code moderated as per the Forum Rules. Please use the pastebin]

    I'm not real great with php, and I keep getting serrors that I'm missing a bracket, but I can't see where. Any help? :)

  2. relaysignal
    Member
    Posted 1 year ago #

  3. alchymyth
    The Sweeper
    Posted 1 year ago #

    try to put all arguments into the array; for instance:

    $news =array(
    'category__and' => array(47,3),
    'posts_per_page' => 1
    );
    $my_query = new WP_Query($news);
    while ($my_query->have_posts()) : $my_query->the_post();
  4. relaysignal
    Member
    Posted 1 year ago #

    I fixed the code but I still get a "Parse error: syntax error, unexpected '}' in .../wp-content/themes/travelertheme/category.php on line 34" error.

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    Can we have the full code on the category template?

  6. relaysignal
    Member
    Posted 1 year ago #

  7. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this: http://wordpress.pastebin.com/nR1sh4VG

    I think it needs an endif; before the '}' and thats why it doesn't work

  8. alchymyth
    The Sweeper
    Posted 1 year ago #

    as far as i know, you can't interrupt a 'while'-loop with a closing bracket of an unrelated conditional statement.

    the line while ($my_query->have_posts()) : $my_query->the_post(); is common to all queries and need only be inserted once, at the start of the actual loop.

    all conditional statements to set the arguments and the query should imho be processed before the start of the loop.

    also inserted a 'wp_reset_query();' before the second loop, to bring the query back to the original category posts.

    for instance:
    http://wordpress.pastebin.com/2u0JUn29

    (untested)

  9. relaysignal
    Member
    Posted 1 year ago #

    Thank you very much, everyone! The code Alchymyth wrote works like a charm.

Topic Closed

This topic has been closed to new replies.

About this Topic