Create dropdown list post

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

Related posts

Leave a Reply

2 comments

  1. 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.