主题函数部分的自定义分类部分的代码如下 :
//文章发布后台添加“文章自定义排序”的post_sortID的自定义字段
//1.创建需要的字段信息
$post_sortID_meta_boxes =
array(
"Works_post_sortID" => array(
"name" => "Works_post_sortID",
"post_sortID" => "",
"post_sortID_title" => "请在下面添加文章【自定义的排序号】"),
);
//2.创建自定义字段输入框
function post_sortID_meta_boxes() {
global $post, $post_sortID_meta_boxes;
foreach($post_sortID_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['post_sortID'];
// 自定义字段标题
echo '<h4>'.$meta_box['post_sortID_title'].'</h4>';
// 自定义字段输入框
echo '<textarea cols="90" rows="3" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br />';
}
echo '<input type="hidden" name="post_sortID_metaboxes_nonce" id="post_sortID_metaboxes_nonce" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
//3.创建自定义字段模块
function post_sortID_meta_box() {
global $theme_name;
if ( function_exists('add_meta_box') ) {
add_meta_box( 'post_sortID_meta_boxes', 'Post_sortID 码', 'post_sortID_meta_boxes', 'post', 'normal', 'high' );
}
}
//4.保存数据
function save_post_sortID( $post_id ) {
global $post_sortID_meta_boxes;
if ( !wp_verify_nonce( $_POST['post_sortID_metaboxes_nonce'], plugin_basename(__FILE__) ))
return;
if ( !current_user_can( 'edit_posts', $post_id ))
return;
foreach($post_sortID_meta_boxes as $meta_box) {
$data = $_POST[$meta_box['name'].'_value'];
if($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
else
update_post_meta($post_id, $meta_box['name'].'_value', $data);
}
}
//5.将函数连接到指定action(动作)
add_action('admin_menu', 'post_sortID_meta_box');
add_action('save_post', 'save_post_sortID');
//end
前台调用代码如下:
<?php
$args = array(
'meta_key' => 'Works_post_sortID_value',
'orderby' => 'meta_value_num',
'order' => 'DESC',
'cat' => '2',
'showposts' => '6',
'posts_per_page' => $posts_per_page,
//'nopaging' => 'False',
'paged' => $paged,
);
// 自定义查询
$the_query = new WP_Query($args);
// 判断查询的结果,检查是否有文章
if ($the_query->have_posts()) :
// 通过查询的结果,开始主循环
while ($the_query->have_posts()) :
$the_query->the_post(); //获取到特定的文章
?>
<div class="article">
<a href="<?php the_permalink();?>" class="entry-featured-image-url" title="点击查看作品【<?php the_title();?>】详情"><img src="<?php $full_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full'); echo $full_image_url[0]; ?>" alt="作品【<?php the_title();?>】<?php echo wp_trim_words( get_the_excerpt(), 40 ); ?>" style="width:400px;height:200px;" /></a>
<h2 class="entry-title"><a href="<?php the_permalink();?>" target="_blank" class="title" title="<?php the_title();?>"><?php the_title();?></a></h2>
<div id="abstract"><p class="comment"><?php echo wp_trim_words( get_the_excerpt(), 40 ); ?></p></div>
</div>
<?php endwhile;endif;?>
<?php wp_reset_postdata();
?>
样式自己去修改哈,毕竟每个主题的框架结构不一样,自己看如何调整最优!
评论