如何通過自定義欄位重新排序WordPress文章(WordPress如何添加自定義小工具)
如何通過自定義欄位重新排序WordPress文章
在編輯文章頁面右側邊欄的發布元框中創建一個數字自定義欄位。欄位值將保存在postmeta表中,我們稍後將使用它來更改文章順序。
有關發布元框中的自定義欄位,請參閱下面的代碼。
add_action('post_submitbox_misc_actions','add_publish_meta_custom_field');functionadd_publish_meta_custom_field($post_obj){
global$post;
$post_type='post';
$value=get_post_meta($post_obj->ID,'post_order',true);
$val=($value)?$value:"0";
if($post_type==$post->post_type){
echo'<DIVCLASS="MISC-PUB-SECTIONMISC-PUB-SECTION-LAST">'
.'PostOrder<INPUTNAME="POST_ORDER"TYPE="NUMBER"STEP="1"MIN="0"ID="POST_ORDER"VALUE="'.$VAL.'"CLASS="SMALL-TEXT">'
.'
';
}}
以上將在發布元框中創建自定義欄位,您將看到如下圖所示的欄位。
發布元框中的自定義欄位
保存自定義欄位的值
現在,我們必須保存PostOrder欄位的值。所以我們將save_post在保存文章之前運行動作鉤子,並update_post_meta()用於保存相應文章的欄位值。
update_post_meta()操作將值與文章ID一起保存在wp_postmeta表中。
好的。請參閱代碼以保存該自定義欄位的值。
add_action('save_post','save_publish_meta_custom_field',10,3);functionsave_publish_meta_custom_field($post_id,$post,$update){
$post_type='post';
if($post_type!=$post->post_type){
return;
}
if(wp_is_post_revision($post_id)){
return;
}
if(isset($_POST['post_order'])){
update_post_meta($post_id,'post_order',$_POST['post_order']);
}
}
上面的代碼將自定義欄位值保存在資料庫中,並將顯示在該輸入數字欄位中。
自定義欄位中保存的值
按自定義欄位更改WordPress文章的順序
現在,我們將使用我們自己的順序值更改WordPress文章的默認順序。在顯示結果之前,我們將運行pre_get_posts動作鉤子來修改現有的WP_Query文章。
pre_get_posts給我們一些解決方案來修改wp_query無需編寫自定義WP_Query。
functionpre_custom_post_order_sort($query){
if(is_home()&&$query->is_main_query()){
$query->set('orderby','meta_value');
$query->set('meta_key','post_order');
$query->set('order','ASC');
}}add_action('pre_get_posts','pre_custom_post_order_sort');
上面的代碼示例將更改博客頁面或主頁上的WordPress文章順序。
我們在這篇文章中檢查了一個主要查詢和主頁,然後使用meta_key元值設置查詢順序。它將檢查文章是否具有自定義欄位值,並根據欄位值將它們置於頂部。
在模板文件中按自定義欄位排序WordPress文章
您還可以創建自己的自定義WP_Query以按順序顯示文章。
您必須創建一個WordPress自定義模板頁面,然後您可以在該模板中編寫自己的查詢。
在此處查看有關如何在WordPress中創建自定義頁面模板的分步指南。
好的。您現在有一個自定義模板文件。因此,在該模板中編寫以下代碼。
$args=array(
'post_type'=>'post',
'meta_key'=>'post_order',
'orderby'=>'meta_value',
'order'=>'ASC');
$query=newWP_query($args);
if($query->have_posts()){
while($query->have_posts()){
$query->the_post();
if(!empty(get_post_meta($post->ID,'post_order',true))){
the_title();
}
wp_reset_postdata();}
上面的代碼將按自定義欄位值顯示文章標題順序,並根據自定義欄位值將文章置於頂部。
您還可以在functions.php文件中創建上述代碼的簡碼,並在小工具、頁面或您想要顯示文章的任何地方使用該簡碼。
就是這樣。您已完成按自定義欄位值顯示WordPress文章順序。
WordPress如何添加自定義小工具
今天給大家帶來一篇WordPress添加一個自定義小工具的教程,之前wp自學筆記分享了WordPress屏蔽後台自帶默認小工具,沒有印象的朋友可以看看
創建小工具
創建一個小工具,需要使用 register_widget() 函數掛載一個繼承於 WP_Widget 類的類,下邊是一個簡單的例子,創建了一個隨機文章小工具。
注意,register_widget() 函數需要在 widgets_init 鉤子上調用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
class widget_tags extends WP_Widget{ //添加小工具 function __construct(){ $this->WP_Widget( \'random_posts\', __( \'隨機文章\', \'Bing\' ), array( \'description\' => __( \'顯示幾篇隨機文章\', \'Bing\' ) ) ); } //小工具內容 function widget( $args, $instance ){ //導入當前側邊欄設置 extract( $args, EXTR_SKIP ); //輸出小工具前代碼 echo $before_widget; //輸出小工具標題 echo $before_title . \'隨機文章\' . $after_title; //隨機文章 query_posts( \'orderby=rand&showposts=10\' ); if( have_posts() ): echo \'<ul>\'; while( have_posts() ): the_post(); printf(get_the_title()); endwhile; echo \'</ul>\'; endif; //輸出小工具後代碼 echo $after_widget; } } function Bing_add_widget_tags(){ register_widget( \'widget_tags\' ); } add_action( \'widgets_init\', \'Bing_add_widget_tags\' ); |
這樣,後台就會出現了一個隨機文章小工具,拖動到側邊欄上,會在前台顯示十篇隨機文章。
小工具設置
但我們會發現這個小工具並沒有設置選項,那如何給這個小工具添加設置選項呢?設置選項涉及類的兩個函數,update()(更新小工具設置時的處理函數)和 form()(設置表單)。
下邊的代碼添加了一個標題設置和顯示文章數量的設置:
1 |
class widget_tags extends WP_Widget{ //添加小工具 function __construct(){ $this->WP_Widget( \\\'random_posts\\\', __( \\\'隨機文章\\\', \\\'Bing\\\' ), array( \\\'description\\\' => __( \\\'顯示幾篇隨機文章\\\', \\\'Bing\\\' ) ) ); } //小工具內容 function widget( $args, $instance ){ //導入當前側邊欄設置 extract( $args, EXTR_SKIP ); //輸出小工具前代碼 echo $before_widget; //輸出小工具標題 echo $before_title . \\\'隨機文章\\\' . $after_title; //隨機文章 query_posts( \\\'orderby=rand&showposts=10\\\' ); if( have_posts() ): echo \\\'<ul>\\\'; while( have_posts() ): the_post(); printf( \\\'<li><a href=\\\"{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s\\\" title=\\\"{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s\\\">{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s</a></li>\\\', get_permalink(), get_the_title(), get_the_title() ); endwhile; echo \\\'</ul>\\\'; endif; //輸出小工具後代碼 echo $after_widget; } //更新選項 function update( $instance, $old_instance ){ $instance[\\\'title\\\'] = strip_tags( $instance[\\\'title\\\'] ); $instance[\\\'number\\\'] = (int) strip_tags( $instance[\\\'number\\\'] ); return $instance; } //選項表單 function form( $instance ){ //添加默認設置 $instance = wp_parse_args( $instance, array( \\\'title\\\' => __( \\\'隨機文章\\\', \\\'Bing\\\' ), \\\'number\\\' => 10 ) ); //設置表單?> <p> <label for=\\\"<?php echo $this->get_field_id( \\\'title\\\' ); ?>\\\"><?php _e( \\\'標題\\\', \\\'Bing\\\' ); ?>:</label> <input id=\\\"<?php echo $this->get_field_id( \\\'title\\\' ); ?>\\\" name=\\\"<?php echo $this->get_field_name( \\\'title\\\' ); ?>\\\" type=\\\"text\\\" value=\\\"<?php echo $instance[\\\'title\\\']; ?>\\\" /> </p> <p> <label for=\\\"<?php echo $this->get_field_id( \\\'number\\\' ); ?>\\\"><?php _e( \\\'文章數量\\\', \\\'Bing\\\' ); ?>:</label> <input id=\\\"<?php echo $this->get_field_id( \\\'number\\\' ); ?>\\\" name=\\\"<?php echo $this->get_field_name( \\\'number\\\' ); ?>\\\" type=\\\"number\\\" value=\\\"<?php echo $instance[\\\'number\\\']; ?>\\\" /> </p><?php } }function Bing_add_widget_tags(){ register_widget( \\\'widget_tags\\\' );}add_action( \\\'widgets_init\\\', \\\'Bing_add_widget_tags\\\' ); |
這樣再展開小工具,就能看到設置了:
然後,在生成小工具內容的時候使用選項,就能達到用戶自定義的效果。
1 |
class widget_tags extends WP_Widget{ //添加小工具 function __construct(){ $this->WP_Widget( \\\'random_posts\\\', __( \\\'隨機文章\\\', \\\'Bing\\\' ), array( \\\'description\\\' => __( \\\'顯示幾篇隨機文章\\\', \\\'Bing\\\' ) ) ); } //小工具內容 function widget( $args, $instance ){ //導入當前側邊欄設置 extract( $args, EXTR_SKIP ); //添加小工具標題過濾器 $title = apply_filters( \\\'widget_name\\\', $instance[\\\'title\\\'] ); //輸出小工具前代碼 echo $before_widget; //輸出小工具標題 echo $before_title . $title . $after_title; //隨機文章 query_posts( \\\'orderby=rand&showposts=\\\' . $instance[\\\'number\\\'] ); if( have_posts() ): echo \\\'<ul>\\\'; while( have_posts() ): the_post(); printf( \\\'<li><a href=\\\"{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s\\\" title=\\\"{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s\\\">{5cc1b29162d549a8071384de182cc9fc6e6a0fd85e7907f22fd9e18cff4269c3}s</a></li>\\\', get_permalink(), get_the_title(), get_the_title() ); endwhile; echo \\\'</ul>\\\'; endif; //輸出小工具後代碼 echo $after_widget; } //更新選項 function update( $instance, $old_instance ){ $instance[\\\'title\\\'] = strip_tags( $instance[\\\'title\\\'] ); $instance[\\\'number\\\'] = (int) strip_tags( $instance[\\\'number\\\'] ); return $instance; } //選項表單 function form( $instance ){ //添加默認設置 $instance = wp_parse_args( $instance, array( \\\'title\\\' => __( \\\'隨機文章\\\', \\\'Bing\\\' ), \\\'number\\\' => 10 ) ); //設置表單?> <p> <label for=\\\"<?php echo $this->get_field_id( \\\'title\\\' ); ?>\\\"><?php _e( \\\'標題\\\', \\\'Bing\\\' ); ?>:</label> <input id=\\\"<?php echo $this->get_field_id( \\\'title\\\' ); ?>\\\" name=\\\"<?php echo $this->get_field_name( \\\'title\\\' ); ?>\\\" type=\\\"text\\\" value=\\\"<?php echo $instance[\\\'title\\\']; ?>\\\" /> </p> <p> <label for=\\\"<?php echo $this->get_field_id( \\\'number\\\' ); ?>\\\"><?php _e( \\\'文章數量\\\', \\\'Bing\\\' ); ?>:</label> <input id=\\\"<?php echo $this->get_field_id( \\\'number\\\' ); ?>\\\" name=\\\"<?php echo $this->get_field_name( \\\'number\\\' ); ?>\\\" type=\\\"number\\\" value=\\\"<?php echo $instance[\\\'number\\\']; ?>\\\" /> </p><?php } }function Bing_add_widget_tags(){ register_widget( \\\'widget_tags\\\' );}add_action( \\\'widgets_init\\\', \\\'Bing_add_widget_tags\\\' ); |
文章到這裡就已經完成了WordPress如何添加自定義小工具的全部教程了,希望對你有些幫助。
1. 帶 [親測] 說明源碼已經被站長親測過!
2. 下載後的源碼請在24小時內刪除,僅供學慣用途!
3. 分享目的僅供大家學習和交流,請不要用於商業用途!
4. 本站資源售價只是贊助,收取費用僅維持本站的日常運營所需!
5. 本站所有資源來源於站長上傳和網路,如有侵權請郵件聯繫站長!
6. 沒帶 [親測] 代表站長時間緊促,站長會保持每天更新 [親測] 源碼 !
7. 盜版ripro用戶購買ripro美化無擔保,若設置不成功/不生效我們不支持退款!
8. 本站提供的源碼、模板、插件等等其他資源,都不包含技術服務請大家諒解!
9. 如果你也有好源碼或者教程,可以到審核區發布,分享有金幣獎勵和額外收入!
10.如果您購買了某個產品,而我們還沒來得及更新,請聯繫站長或留言催更,謝謝理解 !
GG資源網 » 如何通過自定義欄位重新排序WordPress文章(WordPress如何添加自定義小工具)