Pass form values to WordPress query_posts(category name / category id) function to retrieve posts from a specific category

I was taking a look at WordPress Support Forum and found this doubt, about how to pass form values to the query_posts function to retrieve posts from a specific category.
Eu etava dando uma olhada no Forum de Suporte WordPress e achei esta dúvida, sobre como passar valores de formulários para a função query_posts, para retornar posts de uma categoria específica.

First of all, there’s a difference between working with the category name and the category id as parameters:
Primeiramente, há uma diferença entre trabalhar com o nome da categoria e o id da categoria como parâmetros:

//working with the category name
$catnam = $_GET["catnam"]; // or $_POST["catnam"]; // using GET or POST depends on your form submit method
query_posts("category_name=$catnam");

//working with the category id
$catid = $_GET["catid"]; // or $_POST["catid"]; //using GET or POST depends on your form submit method
query_posts("cat=$catid");

Well, now let’s see the complete code.
Bem, agora vamos ver o código completo.

<?
//category name or id
$catid = $_GET["catid"];

// passing the parameters to query_posts
query_posts("cat=$catid");

if (have_posts()) :
     while (have_posts()) : the_post();				 	
?>
     <div>
       <h3><a href="<?php the_permalink();?>"><?=get_the_title();?></a></h3>
     </div>
     <div>
       <p><?=get_the_content();?></p>
     </div>
<?
    endwhile;     
else:
?>
     <div>
       <p>No registers found.</p>								
     </div>
<?
endif;
?> 

Maybe you will enjoy taking a look on another post, where I talk about page navigation on Category Pages.
Talvez você curta dar uma olhada em outro post, onde eu falo sobre navegação de página em Páginas de Categoria.

,

  1. #1 by Freelance Web developer on April 14, 2011 - 8:00 am

    I was searching for a post about displaying all post of a certain category in a page an it led me here. Thanks for sharing!

    • #2 by Alvaro Neto on April 14, 2011 - 12:40 pm

      Thank you for your comment Allan. Great to know my blog was useful for you🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: