1

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' );

2 Answers 2

2

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' ); 
7
  • Hi Abdul. The category method from codee.io above does work. What makes you say using categories is a bad thing? Don't sticky post stick to the top? I want featured posts that are interspersed with the rest of the content.
    – olliew
    Commented Feb 2, 2017 at 13:21
  • 1
    The category method has no problem at all if you are working on a specific project. but if you are planning to release your theme commercially or in wp theme directory then probably the category method is not a good idea. because you will have large number of users using your theme and wp category is designed used for categorizing posts. so respect the wp standards & let it work on its way and create another method to make featured posts. I've mentioned about meta fields in my answer and this is the appropriate method.
    – Anwer AR
    Commented Feb 2, 2017 at 14:28
  • I've tried this approach and added the code to my original question. At the moment all my posts have a class of featured_post, possibly left over from my previous use of the category method, even though I've now deleted that category! This is my first attempt at using Wordpress hooks, meta-boxes, etc
    – olliew
    Commented Feb 2, 2017 at 17:30
  • i see. now this is the good approach. a minor note about meta field. you've added meta field named "meta-box-checkbox" and in my example function i have used a meta field named "featured_post". so these two values should match. i am modifying original source from your question
    – Anwer AR
    Commented Feb 2, 2017 at 18:54
  • Thanks Abdul. I have changed my code so that the values match. The class is being applied to absolutely all of my posts, even those where I haven't ticked the box for them to become featured
    – olliew
    Commented Feb 2, 2017 at 19:25
1

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');

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.