3

I've set a default post category under Settings -> Writing. I've got a front end page that allows user to create their own post. I'm using the following code to display the categories in a drop down:

<?php 
$args = array(
    'type' => 'post',
    'hide_empty' => 0   
    );
$categories = get_categories( $args );
?> 
<select name="category_name">
<?php
foreach($categories as $category){
    if ($category->name != 'Uncategorized') {
        if($action=="edit"){
            $selected_category = get_the_category($post_id); 
            $selected_category = $selected_category[0]->cat_name;
            if($category->name==$selected_category){
                echo '<option value="'.$category->term_id.'" selected> '.$category->name.'</option>';
            }else{
                echo '<option value="'.$category->term_id.'"> '.$category->name.'</option>';
            }
        }else{
            echo '<option value="'.$category->term_id.'"> '.$category->name.'</option>';
        }
    }
}
?>

This works fine but I'd like to set the category->term_id as selected when that ID equals the category selected in my settings page. Is this possible?

Thanks!

1 Answer 1

8

You can access to default category ID via default_category option:

$default_category = get_option('default_category');

Now you can use selected() function to generate the selected attribute:

<?php 
$args = array(
    'type' => 'post',
    'hide_empty' => 0   
    );
$categories = get_categories( $args );
$default_category = get_option('default_category');
?> 
<select name="category_name">
<?php
foreach($categories as $category){
    if ($category->name != 'Uncategorized') {
        if($action=="edit"){
            $selected_category = get_the_category($post_id); 
            $selected_category = $selected_category[0]->cat_name;
            if($category->name==$selected_category){
                echo '<option value="'.$category->term_id.'" selected> '.$category->name.'</option>';
            }else{
                echo '<option value="'.$category->term_id.'"> '.$category->name.'</option>';
            }
        }else{
            echo '<option value="'.$category->term_id.'" '. selected( $category->term_id, $default_category, false ) .'> '.$category->name.'</option>';
        }
    }
}
?>

Maybe you want to use wp_dropdown_categories, which can simplify your code:

<?php

if( $action=="edit" ){

    $selected_category = get_the_category( $post_id );
    $selected_category = $selected_category[0]->term_id;

} else {

    $selected_category = get_option('default_category');

}

$args = array(
    'hide_empty' => 0,
    'name'       => 'category_name',
    'selected'   => $selected_category
);

wp_dropdown_categories( $args );

?> 
1
  • 1
    Great solution. I was also in the midst of wp_dropdown_categories() and just needed the first part, but your overall advice is great :)
    – jerclarke
    Commented Jan 10, 2017 at 19:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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