订购帖子在wordpress中按特定号码保存为“精选”

时间:2013-09-23 12:23:12

标签: php wordpress function customization

我创建了一个将帖子保存为“精选”的功能,并在主页上显示。它适用于管理面板中的自定义复选框。我在论坛的帮助下完成了这个功能,但是我想在这个功能中添加为每个特色帖子设置特定订单号的功能,并按照这个顺序在首页显示它们。 (选择哪一个是第一个,哪个是第二个,第三个等等......)任何人都可以帮忙吗?谢谢!

// FEATURED POST FUNCTION

add_action('add_meta_boxes', 'add_checkbox_featured');

function add_checkbox_featured() {
add_meta_box('is_featured', 'Featured', 'print_checkbox_featured', 'post', 'side');}

function print_checkbox_featured() {
global $post;
$checked = get_post_meta($post->ID, '_featured', true) ? 'checked="checked"' : '';
echo '<label for="checkbox_is_featured">Show at the front page <input id="checkbox_is_featured" name="is_featured" type="checkbox" value="1" '.$checked.'/</label>';
}

add_action('save_post', 'save_checkbox_featured');

function save_checkbox_featured($post_id){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

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

   if ($_POST['is_featured']){
            add_post_meta($post_id, '_featured', '1');
    }else{
            delete_post_meta($post_id, '_featured');
    }
 }

1 个答案:

答案 0 :(得分:0)

尝试使用此功能为特色帖子添加订单,但在主页上您必须使用meta_query按输入的顺序显示帖子

add_action('add_meta_boxes', 'add_checkbox_featured');
//add_action('add_meta_boxes', 'add_checkbox_featured_order');

function add_checkbox_featured() {
add_meta_box('is_featured', 'Featured', 'print_checkbox_featured', 'post', 'side');
}

/*function add_checkbox_featured_order() {
add_meta_box('is_featured_order', 'Featured Order', 'print_checkbox_featured_order', 'post', 'side');
}

function print_checkbox_featured_order() {
global $post;

}*/


function print_checkbox_featured() {
global $post;
$checked = get_post_meta($post->ID, '_featured', true) ? 'checked="checked"' : '';
echo '<label for="checkbox_is_featured">Show at the front page <input id="checkbox_is_featured" name="is_featured" type="checkbox" value="1" '.$checked.'/></label>';

$forder = get_post_meta($post->ID, '_featured_order', true);
echo '<label for="checkbox_is_featured_order">Enter featured order
<input id="checkbox_is_featured_order" name="is_featured_order" type="text" value="'.$forder.'"/></label>';
}

add_action('save_post', 'save_checkbox_featured');

function save_checkbox_featured($post_id){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;

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

   if ($_POST['is_featured']){
            add_post_meta($post_id, '_featured', '1');
    }else{
            delete_post_meta($post_id, '_featured');
    }

   if ($_POST['is_featured_order']){
            add_post_meta($post_id, '_featured_order', $_POST['is_featured_order']);
    }else{
            delete_post_meta($post_id, '_featured_order');
    }
 }