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.

I am trying to figure out how to add a dropdown widget to the post page. The reason I ask is because I would like to be able to have a few different post classes that the user can select while making a post that they can select.

I figure I can use post_class and define a few different classes and allow the user to use this as a post template.

Has anyone done this before and could lead me in the right direction?

share|improve this question
1  
You can use meta box to add box admin post page codex.wordpress.org/Function_Reference/add_meta_box and use post_class filter to use it in front end codex.wordpress.org/Function_Reference/post_class –  Vinod Dalvi Apr 19 '13 at 5:11
    
Awesome, that was exactly what I needed. Thank you. –  user1632018 Apr 19 '13 at 5:38
add comment

2 Answers

up vote 1 down vote accepted

Since this is very similar to post formats (see ) I would use a custom taxonomy.

That makes it easy to control the access level, and you get the meta box without writing extra code.

enter image description here

Then you insert the new post classes with a simple filter. Your theme must use the function post_class() – of course.

Example:

<?php # -*- coding: utf-8 -*-
/* Plugin Name: Post Class Taxonomy */

add_action( 'wp_loaded', 'register_post_class_taxonomy' );

function register_post_class_taxonomy()
{
    $caps = array(
        'manage_terms' => 'manage_options',
        'edit_terms'   => 'manage_options',
        'delete_terms' => 'manage_options',
        'assign_terms' => 'edit_others_posts',
    );

    $labels = array(
        'name'                       => 'Post Classes',
        'singular_name'              => 'Post Class',
        'search_items'               => 'Search Post Classes',
        'popular_items'              => 'Popular Post Classes',
        'all_items'                  => 'All Post Classes',
        'edit_item'                  => 'Edit Post Class',
        'view_item'                  => 'View Post Class',
        'update_item'                => 'Update Post Class',
        'add_new_item'               => 'Add New Post Class',
        'new_item_name'              => 'New Post Class',
        'separate_items_with_commas' => 'Separate Post Classes with commas',
        'add_or_remove_items'        => 'Add or remove Post Classes',
        'choose_from_most_used'      => 'Choose from the most used Post Classes',
    );
    $args = array (
        'rewrite'           => FALSE,
        'public'            => FALSE,
        'show_ui'           => TRUE,
        'labels'            => $labels,
        'capabilities'      => $caps,
        'show_in_nav_menus' => FALSE,
    );
    register_taxonomy( 'post_classes', 'post', $args );
}

add_filter( 'post_class', 'insert_custom_post_classes' );

function insert_custom_post_classes( $classes, $class = '', $post_ID = NULL )
{
    NULL === $post_ID && $post_ID = get_the_ID();

    $post = get_post( $post_ID );

    if ( ! is_object_in_taxonomy( $post->post_type, 'post_classes' ) )
        return $classes;

    if ( ! $post_classes = get_the_terms( $post_ID, 'post_classes' ) )
        return $classes;

    foreach ( $post_classes as $post_class )
        if ( ! empty ( $post_class->slug ) )
            $classes[] = 'post-class-' . esc_attr( $post_class->slug );

    return $classes;
}
share|improve this answer
add comment

You can use meta box to add box in admin post page and use post_class filter to use it in front end.

share|improve this answer
add comment

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.