Support » Fixing WordPress » Quick PHP Question

  • Resolved Ralph23

    (@ralph23)


    Hey.

    I’m using a query to show the latest post in the “Featured” category by using this code:

    <?php if (! is_paged()):
    	$my_query = new WP_Query('category_name=Featured&showposts=1');
    	$wp_query->in_the_loop = true;
    	while ($my_query->have_posts()) : $my_query->the_post();
    	$do_not_duplicate = $post->ID;
    ?>

    Thing is, I want the user to be able to choose which category they want to be their “Featured” category. I have a variable (via Theme Options) with the correct category, thing is, I don’t know how to pass this variable into the query above, particularly this line right here:

    $my_query = new WP_Query('category_name=Featured&showposts=1');

    I can’t put a variable in there because it’s surrounded with ‘ ‘. IF I remove the ‘ ‘, I get an error.

    Any ideas?

    Thanks.

Viewing 3 replies - 1 through 3 (of 3 total)
  • $my_query = new WP_Query('category_name=' . $featured_cat . '&showposts=1');

    Oh so thats what those annoying little periods are for…

    Thanks Michael.

    Using a variable for category_name in the WP_Query function call will shut down with two word category names. Your WP_Query will return no content if the category name is, say, “Web Design.”

    I’m not sure how to get around it if you’re using the category name as the identifier.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Quick PHP Question’ is closed to new replies.