Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Im trying to get the category ID from wp_dropdown_categories();and then pass the ID into a loop to have it output posts titles from that particular category.

I currently have this:

   <form action="<?php bloginfo('url'); ?>" method="get">
        <?php wp_dropdown_categories(); ?>
    </form>
        <div class="cat-list">
                <ul>
                <?php 
                global $post;
                $args = array( 'posts_per_page' => 5, 'category' => $_GET['value']);
                $myposts = get_posts( $args );
                foreach ( $myposts as $post ) : 
                  setup_postdata( $post ); ?>
                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                <?php endforeach;
                wp_reset_postdata(); ?>
                </ul>

        </div>

But it's not grabbing the category ID from the $_GET['value'] and I'm not sure what I need to do to get it to pass that ID to the array.

Eventually, I'd like to have it so that when you select a different category that it will show the posts associated with that selected category. But I figured that I'd try to get the select to work first and then move on from there.

share|improve this question

3 Answers 3

You're on the right track with using a form, the only thing you're missing, you have to submit what you have selected. For example like this:

<form method="GET" action="">
    <div>
        <?php
            $select = wp_dropdown_categories('show_option_none=Select category&orderby=name&echo=0');
            $select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select);
            echo $select;
        ?>
        <noscript>
            <div>
                <input type="submit" value="View" />
            </div>
        </noscript>
    </div>
</form>

Code adapted from the example section of the wp_dropdown_categories() codex page, there are other examples, take a look. I changed one thing, the form action is empty, because we want to stay on the page.
The variable you want would be $_GET['cat']; additionally you can visually see that by looking to the browser address bar, there is now something like this ?cat=1, a query string, after you selected a category. If you don't want the query string to show, you actually can change the form method to POST and the variable to $_POST['cat'].


Edit:

On the home page this will redirect to the category archive, because wordpress reacts to the query string cat. In this case you have to add the name parameter to wp_dropdown_categories() and choose a unique name, in above example like this:

wp_dropdown_categories('show_option_none=Select category&orderby=name&echo=0&name=uniquename');

which leads to the variable name being $_GET['uniquename'] or $_POST['uniquename'].


2nd Edit:

On first page load there is no $_GET['uniquename'] variable set, so your get_posts query will, will operate with an empty value, which leads to all post been shown. To prevent this setup a variable like shown below and use it in your arguments array.

$gp_cat = (isset($_GET['uniquename']) ? $_GET['uniquename'] : '5');
$args = array( 'posts_per_page' => 5, 'category' => $gp_cat);
share|improve this answer
    
I did see that example from the codex, but that's not what I'm trying to accomplish with this. I'm basically trying to use wp_dropdown_categories() to show the posts associated with the selected category. And then when another category is selected from the drop down menu, it will change to show the posts from that category. The example from the codex works if I want to re-direct to the archive page, but I'm trying to get it to load the posts within the div. –  ultraloveninja Dec 3 '13 at 22:35
    
That's actually what this achieves. I changed the code a bit, the action is empty. I gave it a quick test with the rest of your code, it's working fine. @ultraloveninja –  ialocin Dec 3 '13 at 22:40
    
Hmm, that's strange. When I click on a category in the drop down select menu it re-directs me to the category archive page. –  ultraloveninja Dec 3 '13 at 22:58
    
I know that in the HTML output is does give the value of each category as the ID: <option class="level-0" value="1">General News</option> <option class="level-0" value="5" selected="selected">Other Category</option> So, I'm not sure if there is a way to pull those values in or not. –  ultraloveninja Dec 3 '13 at 23:51
    
Hmm, well either way it's not filtering out the results when a particular category is selected as default wp_dropdown_categories('selected=5&echo=0');. It's still showing all of the posts and not just the one post that is associated with the category id of 5. –  ultraloveninja Dec 4 '13 at 0:19

The $_GET['value'] is related to the NAME attribute from your SELECT. The select name can be changed using the name parameter in the wp_dropdown_categories() function too. Use print_r($_GET); to see what values your form is submitting and then use them in your get_posts() query.

share|improve this answer
    
Hmm, when I do a print_r($_GET); it returns an array with values from the URL. So, yeah. Maybe I'm going about this all wrong. –  ultraloveninja Dec 3 '13 at 22:33

You can print your id echo $termss->term_id in drop down

    $terms = get_terms( 'category', array(
    'hide_empty' => false,
    'order_by' => 'name',
    'order' => 'ASC',
    'number' => 0,
    'taxonomy' => 'category'
) );

     //print_r($terms);

     foreach($terms as  $termss)
    {
    //echo "<br/>";
     //echo $termss->term_id;
        //echo "<br/>";
        ?>
    <h1> echo $termss->term_id</h1>
     <?php 
        //echo "<br/>";
      //echo $termss->slug;
      }
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.