在某些帖子中添加“特色”类

时间:2017-02-02 作者:olliew

我希望我的主题的用户能够标记一些更重要的帖子,以便我的主题能够吸引更多的关注。我不知道最好的方法是什么?是否可以将默认类别“特色”添加到主题中,当任何人安装我的主题时,该主题将作为选项存在?

编辑:采用在管理屏幕中添加自定义元框的方法:应该这样做吗?

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 个回复
最合适的回答,由SO网友:Anwer AR 整理而成

WordPress默认提供名为“粘性帖子”的功能。您可以通过“快速编辑”链接将任何帖子标记为粘滞。WordPress将添加一个名为sticky 所有粘帖。因此,您可以在CSS中使用该类来提供自定义样式。

另一个解决方案是在wp admin中创建自定义post meta和meta框。并为用户提供一种将任何帖子标记为“特色”的方法。然后,基于该元字段值,您可以轻松地更改post类。

查找有关的更多信息Meta BoxCustom fields 来自WordPress Codex。如果您添加了custom field 在名为“的帖子中”featured_post“您可以使用下面的函数更改post类。它将添加一个名为\'featured-post\' 所有帖子都标记为特色。

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

SO网友:Twentyonehundred

您可以使用类别、标记、插件或ACF字段以多种方式实现这一点。

要使用建议的方法,可以使用wp_insert_category 与钩状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\');

相关推荐