1

I want to create a drop down list that will list all the posts in the category. I want the drop down list to appear on every post. So for example if I view a post that belongs to category Apples it should list all posts in category Apples

2

Hey @Ruriko ... its a 3 step action.

  1. get the current category ID
  2. get the posts for this specific category
  3. get the posts into a select menu

Please note that this code would use the first
category id altough you might assign several
categories to the same post. .

<?php 
    // FIRST CATEGORY NAME
    $category = get_the_category(); 
    $catID = $category[0]->term_id;
    $args = array(
        'numberposts' => 5, 
        'category' => $catID            
    );
    $catPosts = get_posts( $args );

    echo '<form method="POST">';
    echo '<select name="goToPost" onchange="document.location=this.value">';
    echo '<option value="">'.__('Relevent Posts', 'your_text_domain').'</option>';  
    foreach( $catPosts as $singlePost ) {
    echo '<option value="'.get_bloginfo('url').'/index.php?p='.$singlePost->ID.'">'.$singlePost->post_title.'</option>';
    };
    echo '</select>';
    echo '</form>';
?>  

Hope this helps.
Cheers, Sagive.

  • It sort of works but when I select a post in the drop down list it doesn't go to that post. Is there a way to show all posts instead 5 only? since each category has different amounts of posts – Ruriko Oct 1 '12 at 11:03
  • aplogies... i left my testing code in there.. see my revised code - should work now (tested) – Sagive SEO Oct 1 '12 at 11:59
0

To show all posts, using WP Query with posts_per_page value equals to -1.

Not exactly the answer but here's an example of using wp query

More on WP Query here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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