WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Quick PHP Question (4 posts)

  1. Ralph23
    Member
    Posted 5 years ago #

    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.

  2. MichaelH
    Member
    Posted 5 years ago #

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

  3. Ralph23
    Member
    Posted 5 years ago #

    Oh so thats what those annoying little periods are for...

    Thanks Michael.

  4. vanishdesign
    Member
    Posted 5 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.