文章编辑页面的效果如下,被放置在了右侧底部。
/**
* Calls the class on the post edit screen.
*/
function set_recommend() {
new recommended();
}
if ( is_admin() ) {
add_action( 'load-post.php', 'set_recommend' );
add_action( 'load-post-new.php', 'set_recommend' );
}
/**
* The Class.
*/
class recommended {
/**
* Hook into the appropriate actions when the class is constructed.
*/
public
function __construct() {
add_action( 'add_meta_boxes', array( $this, 'recommend_box' ) );
add_action( 'save_post', array( $this, 'save_recommend' ) );
}
/**
* Adds the meta box container.
*/
public
function recommend_box( $post_type ) {
// Limit meta box to certain post types.
$post_types = array( 'the_post_types' );
if ( in_array( $post_type, $post_types ) ) {
add_meta_box(
'recommend_option',
__( 'Recommend option', 'textdomain' ),
array( $this, 'render_recommend_box' ),
$post_type,
'side',
'low'
);
}
}
/**
* Save the meta when the post is saved.
*
* @param int $post_id The ID of the post being saved.
*/
public
function save_recommend( $post_id ) {
/*
* We need to verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times.
*/
// Check if our nonce is set.
if ( !isset( $_POST[ 'myplugin_inner_custom_box_nonce' ] ) ) {
return $post_id;
}
$nonce = $_POST[ 'myplugin_inner_custom_box_nonce' ];
// Verify that the nonce is valid.
if ( !wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
return $post_id;
}
/*
* If this is an autosave, our form has not been submitted,
* so we don't want to do anything.
*/
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
// Check the user's permissions.
if ( 'page' == $_POST[ 'post_type' ] ) {
if ( !current_user_can( 'edit_page', $post_id ) ) {
return $post_id;
}
} else {
if ( !current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
}
/* OK, it's safe for us to save the data now. */
// Sanitize the user input.
$rec_data = sanitize_text_field( $_POST[ 'rec_item' ] );
// Update the meta field.
update_post_meta( $post_id, '_my_meta_value_key_rec', $rec_data );
}
/**
* Render Meta Box content.
*
* @param WP_Post $post The post object.
*/
public
function render_recommend_box( $post ) {
// Add an nonce field so we can check for it later.
wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
// Use get_post_meta to retrieve an existing value from the database.
$value_rec = get_post_meta( $post->ID, '_my_meta_value_key_rec', true );
// Checking for coupon active option.
if ( $value_rec ) {
if ( checked( '1', $value_rec, false ) )
$active = checked( '1', $value_rec, false );
}
?>
<ul class="rec_items">
<li>
<label for="rec_item">
<input type="checkbox" id="rec_item" name="rec_item" value="1" <?php echo $active ?> /> Recommend it.
</label>
</li>
</ul>
<?php
}
}
需要进行查询的地方添加下列两个条件即可:
如:
meta_key=_my_meta_value_key_rec
meta_value=1
query_posts($query_string . '&post_type=the_post_type&orderby=date&showposts=5&meta_key=_my_meta_value_key_rec&meta_value=1')
评论区
发表新的留言
您可以留言提出您的疑问或建议。
您的留言得到回复时,会通过您填写的邮箱提醒您。