Question

I would like users of my theme to be able to mark some posts as more important so that my theme can draw more attention to them. I'm not sure about the best way to go about this? Is it possible to add a default category of 'featured' to a theme that will be there as an option when anybody installs my theme?

edit: Taking the approach of adding a custom meta-box in the admin screen: Should something like this work?

function featured_post() { ?>
  <label><input type="checkbox" id="featured_post"  value="featured_post">Make this a featured post</label>;
<?php }

add_action('add_meta_boxes', 'cd_meta_box_add');
function cd_meta_box_add() {
add_meta_box(1, 'Featured Post', 'featured_post');
}

function save_custom_meta_box($post_id, $post, $update)
{
if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
    return $post_id;

if(!current_user_can("edit_post", $post_id))
    return $post_id;

if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
    return $post_id;

$slug = "post";
if($slug != $post->post_type)
    return $post_id;


$featured_post_value = "";


if(isset($_POST["featured_post"]))
{
    $featured_post_value = $_POST["featured_post"];
}   
update_post_meta($post_id, "featured_post",      $featured_post_value);
}

add_action("save_post", "save_custom_meta_box", 10, 3);

function annframe_featured_class( $classes ) {
global $post;
if ( get_post_meta( $post->ID, 'featured_post' ) || '' !=    get_post_meta( $post->ID, 'featured_post' ) ) {
$classes[] = 'featured-post';
}
return $classes;
}
add_filter( 'post_class', 'annframe_featured_class' );
Was it helpful?

Solution

WordPress by default provides such feature named "Sticky Posts". you can mark any post as sticky from "Quick Edit" link. and WordPress will add a post class named sticky with all sticky posts. so you can use this class in your CSS for giving custom styles.

and another solutions is to create custom post meta and meta boxes in wp-admin. and provide your users a way to mark any post as "Featured". and then based on that meta field value you can easily alter the post class.

find more info about Meta Box and Custom fields from WordPress Codex. and then if you have added a custom field in a post named "featured_post" you can alter the post class using below function. it will add a class named 'featured-post' with all posts marked as featured.

add_action( 'load-post.php', 'annframe_meta_boxes_setup' );
add_action( 'load-post-new.php', 'annframe_meta_boxes_setup' );
add_action( 'save_post', 'annframe_save_post_meta', 10, 2 );

function annframe_meta_boxes_setup() {
  add_action( 'add_meta_boxes', 'annframe_add_meta_box' );
}

function annframe_add_meta_box() {
  add_meta_box(
      'featured_post',                    // Unique ID
      __( 'Featured Post' ),    // Title
      'annframe_display_meta_box',        // Callback function
      'post',  // Admin page (or post type)
      'side',    // Context
      'high'
    );
}

function annframe_display_meta_box( $post ) {
  wp_nonce_field( basename( __FILE__ ), 'ann_meta_boxes_nonce' );
  ?>
   <label for="meta-box-checkbox"><?php _e( 'Mark as featured'); ?></label>
   <input type="checkbox" id="meta-box-checkbox"  name="meta-box-checkbox" value="yes" <?php if ( get_post_meta( $post->ID, 'featured_post', true ) == 'yes' ) echo ' checked="checked"'; ?>>
  <?php
}

// Save meta value.
function annframe_save_post_meta( $post_id, $post ) {

  /* Verify the nonce before proceeding. */
  if ( !isset( $_POST['ann_meta_boxes_nonce'] ) || !wp_verify_nonce( $_POST['ann_meta_boxes_nonce'], basename( __FILE__ ) ) )
    return $post_id;

  /* Get the post type object. */
  $post_type = get_post_type_object( $post->post_type );

  /* Check if the current user has permission to edit the post. */
  if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
    return $post_id;

  $meta_box_checkbox_value = '';
  if( isset( $_POST["meta-box-checkbox"] ) ) {
    $meta_box_checkbox_value = $_POST["meta-box-checkbox"];
  }

  update_post_meta( $post_id, "featured_post", $meta_box_checkbox_value );
}

// add class.
function annframe_featured_class( $classes ) {
global $post;
if ( get_post_meta( $post->ID, 'featured_post' ) &&  get_post_meta( $post->ID, 'featured_post', true ) == 'yes' ) {
$classes[] = 'featured-post';
}
return $classes;
}
add_filter( 'post_class', 'annframe_featured_class' ); 

OTHER TIPS

You can do this in a number of ways, using categories, tags, plugins or ACF fields.

To use your suggested method, you can use the wp_insert_category function in combination with a hook like admin_init.

function add_featured_category() {
    $mcat = array(
        'cat_name' => 'Featured', 
        'category_description' => 'A Featured Category', 
        'category_nicename' => 'category-featured', 
        'category_parent' => ''
    );
    $my_cat_id = wp_insert_category($mcat);
}

add_action('admin_init', 'add_featured_category');
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top