• Resolved ianb-1

    (@ianb-1)


    I’m trying to build a page template for a set of custom archive page templates. There’s going to be a bunch, so I am trying to write a single template that I can pass a variable set in a page custom field that sets what category_name is. I’ve got all the other pieces working, but I can’t get the $category variable to do what it needs to. I’m sure this is totally basic, but I’m stumped right now.

    <?php
           $category = get_post_meta($post->ID, "CategoryName", true);
            		if ($category){
    				query_posts('showposts=10&category_name=echo $category');
    				}
            		?>
Viewing 3 replies - 1 through 3 (of 3 total)
  • Single and double quotes work differently. Only double quotes will evaluate a variable.

    There may be other problems, but this should clean up that one:
    query_posts("showposts=10&category_name=$category");

    query_posts('showposts=10&category_name=' . $category); would also work.

    The if ($category) coding also caught my eye. If it doesn’t work, try
    if ($category != "")

    You might want to check the category exists before you pass it into the query_posts, else query_posts will just grab posts that don’t belong to the category given (it will ignore the category parameter if not given a valid category), it’ll proberly end up giving you all posts..

    if ($category != "")

    Not sure how that will help..

    if ($category)

    Is fine for the usage above..

    Thread Starter ianb-1

    (@ianb-1)

    Thank you both. I’ve got my issue sorted out now. I knew it was something simple like that – but it was Friday and my brain had stopped.

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

The topic ‘Passing a variable to query_posts’ is closed to new replies.