第一步:通过meta_box在文章编辑页面的右边栏添加指定按钮。
效果:
源码:
function pickup() {
new set_pickup();
}
if ( is_admin() ) {
add_action( 'load-post.php', 'pickup' );
add_action( 'load-post-new.php', 'pickup' );
}
class set_pickup {
public function __construct() {
add_action( 'add_meta_boxes', array( $this, 'add_meta_box' ) );
add_action( 'save_post', array( $this, 'save' ) );
}
// 添加meta_box
public function add_meta_box( $post_type ) {
//假设自定义文章类型是pickup
$post_types = array( 'pickup');
if ( in_array( $post_type, $post_types ) ) {
add_meta_box(
'allowstick',
__( 'Sticky post', 'nv' ),
array( $this, 'render_allowstick' ),
$post_type,
'side',
'high'
);
}
}
// 生成容器
public function render_allowstick( $post ){
wp_nonce_field( 'myplugin_inner_custom_box', 'myplugin_inner_custom_box_nonce' );
$pickup_sticky = get_post_meta( $post->ID, '_pickup_sticky', true );
if ( $pickup_sticky ) {
if ( checked( '1', $pickup_sticky, false ) )
$active = checked( '1', $pickup_sticky, false );
}
?>
<div class="plus_items_list">
<label for="pickup_sticky">
<input id="pickup_sticky" name="pickup_sticky" type="checkbox" value="1" <?php echo $active; ?> /> Select to stick</label>
</div>
<?php
}
// 保存及更新
public function save( $post_id ) {
if ( ! isset( $_POST['myplugin_inner_custom_box_nonce'] ) ) {
return $post_id;
}
$nonce = $_POST['myplugin_inner_custom_box_nonce'];
if ( ! wp_verify_nonce( $nonce, 'myplugin_inner_custom_box' ) ) {
return $post_id;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
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;
}
}
$push_pickup_sticky = $_POST[ 'pickup_sticky' ];
update_post_meta( $post_id, '_pickup_sticky', $push_pickup_sticky );
}
}
第二步:通过meta_key区分置顶与非置顶的自定义文章。通过减法输出指定数量的文章。
源码:
$maxNum = 5;
$sticky_count = 0;
// 获取pickup这个自定义文章中置顶的文章ID,以数组的形式输出。
function post_id_by_meta_key( $meta_key ) {
$outside = array();
$args = array(
'post_type' => 'pickup',
'tax_query' => array(
'taxonomy' => 'cat-pickup',
'field' => 'slug',
'terms' => 'pick-up',
),
'meta_key' => $meta_key,
'meta_value' => '1'
);
$query = new WP_Query( $args );
while ( $query->have_posts() ): $query->the_post();
$outside[] = $query->post->ID;
endwhile;
return $outside;
}
// 循环条件
function define_loop_args( $present_post_type, $maxNum, $sticky_toggle = 0 ) {
$loop_args = array(
'post_type' => $present_post_type,
'tax_query' => array(
'taxonomy' => 'cat-pickup',
'field' => 'slug',
'terms' => 'pick-up',
),
);
if ( $sticky_toggle == 1 ) {
$loop_args[ 'meta_key' ] = '_pickup_sticky';
$loop_args[ 'meta_value' ] = '1';
$loop_args[ 'posts_per_page' ] = $maxNum;
} else {
$array = post_id_by_meta_key( '_pickup_sticky' );
$loop_args[ 'post__not_in' ] = $array;
$loop_args[ 'posts_per_page' ] = ( $maxNum );
}
return $loop_args;
}
?>
<ul class="pickup-posts">
<?php
// 置顶文章循环
$arg = define_loop_args( 'pickup', '', 1 );
$query = new WP_Query( $arg );
while ( $query->have_posts() ): $query->the_post();
?>
<!-- 被循环的内容<li></li> -->
<?php
$sticky_count++;
endwhile;
wp_reset_postdata();
// 非置顶文章循环
$arg = define_loop_args( 'pickup', ( $maxNum - $sticky_count ), 0 );
$query = new WP_Query( $arg );
while ( $query->have_posts() ): $query->the_post();
?>
<!-- 被循环的内容<li></li> -->
<?php
endwhile;
wp_reset_postdata();
?>
</ul>
评论区
发表新的留言
您可以留言提出您的疑问或建议。
您的留言得到回复时,会通过您填写的邮箱提醒您。