• I’m trying to get query_posts to refer to a category slug that I’ve got stored in $mycatslug. So I insert the following code:

    print $mycatslug;
    query_posts( 'category_name = $mycatslug' . '&paged=' . get_query_var('paged'));

    The first line works correctly — I see that it has printed the category slug on the site. But the second line does not — this should restrict it to only the category slug mentioned on the first line. Did I do something wrong in its syntax?

Viewing 6 replies - 1 through 6 (of 6 total)
  • $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    query_posts( 'category_name ='. $mycatslug . '&paged=' . $paged);

    Try once it

    Chinmoy

    You need to use double quotes when you have a PHP variable within a quoted string.

    query_posts("category_name = $mycatslug&paged=". get_query_var('paged'));

    Thread Starter altoidboy

    (@altoidboy)

    Thanks very much for the help. Unfortunately, neither worked.

    I guess I should mention that I’m trying to mod a template (JournalCrunch) that doesn’t seem to support pagination properly, so I’m trying to write the correct query_post so that it does. My full relevant portion of code is this:

    <?php
    $catslug = get_query_var('cat');
    $mycatslug = get_category ($cat)->slug;
    print $mycatslug;  // this is just here as a temporary diagnostic and it does correctly return the category slug
    query_posts("category_name = $mycatslug&paged=". get_query_var('paged'));
    ?>

    When I put this in the code, the resultant page displays ALL posts, not posts of this specific category.

    However, if I replace the last line with a hardcoded category, like this:

    query_posts( 'cat=4' . '&paged=' . get_query_var('paged'));

    then the same resultant page does correctly display posts from category #4.

    Any thoughts?

    Moderator keesiemeijer

    (@keesiemeijer)

    Try it with:

    <?php
    $catID = get_query_var('cat');
    $paged = get_query_var('paged') ? get_query_var('paged') : 1;
    query_posts('cat='. $catID . '&paged='. $paged);
    ?>

    Thread Starter altoidboy

    (@altoidboy)

    keesiemeijer, that appears to work!! I can’t thank you enough for helping me figure that out — this was a major stumbling block to getting this site finished.

    I’ve tested it successfully on the category-specific pages so far; next I’ll try and migrate the same solution to the home page.

    Moderator keesiemeijer

    (@keesiemeijer)

    You’re welcome. Success on trying it on the home page.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Simple query_posts question’ is closed to new replies.