For starters, I'm not a programmer but I'm good at cut and paste. :) I want to create a drop down list that displays each post and I've done that by putting this in my functions.php child theme file using this code:

function wpb_recentposts_dropdown() { 
    $string .= '<select id="rpdropdown">
    <option  value="" selected>Select a Post<option>';
    $args = array( 'numberposts' => '5', 'post_status' => 'publish' );
    $recent_posts = wp_get_recent_posts($args);
    foreach( $recent_posts as $recent ){
        $string .= '<option value="' . get_permalink($recent["ID"]) . '">' 
. $recent["post_title"].'</option> ';
    }
    $string .= '</select>
<script type="text/javascript"> var urlmenu =  
document.getElementById( "rpdropdown" ); urlmenu.onchange =
function() {
window.open( this.options[ this.selectedIndex ].value, "_self" );
        };
</script>';
    return $string;
} 
add_shortcode('rp_dropdown', 'wpb_recentposts_dropdown');
add_filter('widget_text','do_shortcode');

And I use the short code [rp_dropdown] in my widget and it works. The problem is it displays posts from every category. I need for it to display a specific category and I also would like it to be alphabetical. Can someone please tell me how to make this happen in layman's terms? Thank you!

  • By "specific" category you mean current category or some specific category that will be set as shortcode argument? – Krzysiek Dróżdż Apr 4 at 17:05
  • Like category with an ID, like cat 28. Categories that posts fall under. – rolandogomez Apr 5 at 17:34
  • OK, but should this catID be an argument for that shortcode, or is it constant? – Krzysiek Dróżdż Apr 5 at 17:39

Well, in a "layman's" terms, you need to add your preferred filter (specific category id and order) to the $args array, like so:

$args = array( 
    'numberposts' => '5', 
    'post_status' => 'publish', 
    'cat' => 5, 
    'order' => 'ASC',
    'orderby' => 'title'
);

The 'cat' should be the id of the "specific" category you want to show.

Hope that helps.

OK, so let's assume you want to filter always by the same category (let's say term_ID = 28), then your code should look like this:

function wpb_recentposts_dropdown() { 
    $string .= '<select id="rpdropdown">' .
        '<option  value="" selected>Select a Post<option>';

    $args = array(
        'numberposts' => '5',
        'post_status' => 'publish',
        'cat' => 28, // <- this filters by category
        'orderby' => 'title', // <- this orders by title
        'order' => 'ASC' // <- this changes order to ascending (DESC is default)
    );
    $recent_posts = wp_get_recent_posts($args);
    ... // <- rest of your code

If you'd like to pass that category ID as shortcode's attribute, then something like this should help you:

function wpb_recentposts_dropdown( $atts ) { 
    $a = shortcode_atts( array(
        'cat' => false, // <- you can put default cat ID here
    ), $atts );

    $string .= '<select id="rpdropdown">' .
        '<option  value="" selected>Select a Post<option>';

    $args = array(
        'numberposts' => '5',
        'post_status' => 'publish',
        'orderby' => 'title', // <- this orders by title
        'order' => 'ASC' // <- this changes order to ascending (DESC is default)
    );
    if ( is_numeric($a['cat']) ) {
        $args['cat'] = $a['cat'];
    }
    $recent_posts = wp_get_recent_posts($args);
    ... // <- rest of your code
  • Actually it does the first category short code just fine, but when I add a second short code with a new category, while it will display the selection menu and post titles, it will not go to the post selected title for any drop down list except the first selection drop down list. So if I add this code, [rp_dropdown cat='13'] then underneath it this short code [rp_dropdown cat='2'] it will display both drop down lists, complete with titles, but only will go to one's selected on the first drop down menu. Almost a java problem. – rolandogomez Apr 19 at 1:52
  • @rolandogomez I haven’t seen your JS code and JS is beyond the scope of this site, so I won’t be able to help you with that, I’m afraid. – Krzysiek Dróżdż Apr 19 at 3:15

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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