0

I'm currently finishing up my first proper wordpress theme for my uni assignment. When I select a category in the sidebar the full list of posts from all categories are still shown. I've tried searching online but can't seem to find how to fix this.

The site I'm currently building is http://bluepenguindesigns.co.uk/. Here is the current category.php contents - http://pastebin.com/89jiFB5i

0

1 Answer 1

0

The variable $theTitle is not interpolated in a single quote string.

query_posts('category_name=.$theTitle&orderby=rand');

Will ask for exactly that string. $theTitle will not be replaced with a title and the dot (.) will remain in the query. The result will be unpredictable.

So, use a double quoted string:

query_posts( "category_name={$theTitle}&orderby=rand" );

Or change the order of the arguments and concatenate the title.

query_posts( 'orderby=rand&category_name=' . $theTitle );

Are you sure you want to use query_posts() in an assignment? Isn't kind of like telling the teacher you didn't read the documentation which pretty much says "don't use query_posts()".


EDIT:

Apparently, the query_posts() function uses the parameters for WP_Query which makes sense.

The instructions say that you should use the category slug, not category name: category_name (string) - use category slug (NOT name). I have no idea why they would not name the paramteter category_slug

So first we need to get the category slug.

function wpse_100126_get_category_slug() {

    $current_cat = get_category( get_query_var( 'cat' ) );

    return $current_cat->slug;
}

Then plug that into the query:

query_posts( 'orderby=rand&category_name=' . wpse_100126_get_category_slug() );
3
  • This helped but all I get now is ALL of the posts including those in other categories than the selected one.
    – Marpo
    Commented May 21, 2013 at 0:05
  • See the edit above. Commented May 21, 2013 at 14:05
  • Thanks. This worked although I had to remove the wpse_100126 from the function.
    – Marpo
    Commented May 21, 2013 at 23:13

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.