wordpress代码自定义分类字段面板并筛选指定规则排行榜文章

IT资讯
IT资讯
IT资讯
1166
文章
239
评论
2019-08-1805:41:31
评论
1,779 2749字

主题函数部分的自定义分类部分的代码如下 :

//文章发布后台添加“文章自定义排序”的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(); 
?>

 

 

样式自己去修改哈,毕竟每个主题的框架结构不一样,自己看如何调整最优!

 

  • Copyright ©  PC在线云端  版权所有.
  • 转载请务必保留本文链接:https://nrcs.xyz/web/wordperss_code/8410.html
WordPress 使用 Github & JsDelivr 加速静态文件 WEB技术

WordPress 使用 Github & JsDelivr 加速静态文件

一个网站的打开速度至关重要,会直接影响搜索引擎排名和用户体验。如果您的网站加载越慢,用户离去的就会越快,而用户又是我们生存的根本。 当然网站加载速度受很多因素影响,今天我们使用 Github &...
Orgorg速率最高可达1000Mbps,流畅观看Youtube 4K、TikTok,支持 Windows、Android、iOS、Mac,支持 微信、支付宝 付款!
全球数据中心,多点BGP保证速度,无视晚高峰,全天4K秒开,IPLC专线无惧封锁
全IEPL /青云跨境,高峰时期稳定8K播放,流媒体影视, ChatGPT 解锁保障,客户端无日志保护您的隐私安全,稳定运行5年+
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: