Wordpress主題設定投影片功能設計?

來源:互聯網
上載者:User
開發一個主題,首頁有一個投影片展示,#1、#2、#3 三張。
發布文章的時候,需要有一個勾選選項,勾選1、2或者3,會在首頁的相應投影片顯示;勾選4(預設)則不會顯示。
嘗試過wordpress的自訂欄位(Custom Fields)但是好像不能實現,求提供思路和解決辦法。

回複內容:

開發一個主題,首頁有一個投影片展示,#1、#2、#3 三張。
發布文章的時候,需要有一個勾選選項,勾選1、2或者3,會在首頁的相應投影片顯示;勾選4(預設)則不會顯示。
嘗試過wordpress的自訂欄位(Custom Fields)但是好像不能實現,求提供思路和解決辦法。

     __('Use as slide'),    'desc' => 'Check this box and make the post a slider',    'id' => 'sola-post-slider',    'type' => 'checkbox',    'default' => ''));/* Meta box setup function. */function sola_post_meta_boxes_setup() {/* Add meta boxes on the 'add_meta_boxes' hook. */add_action( 'add_meta_boxes', 'sola_add_post_meta_boxes' );add_action( 'save_post', 'sola_save_post_meta_boxes', 10, 2 );}/* Create one or more meta boxes to be displayed on the post editor screen. *//* 這裡也需要改一下,設定需要建立的Post Meta Box叫什麼名字,顯示在什麼位置 */function sola_add_post_meta_boxes() {add_meta_box(    'sola-post-slider-class',   // Unique ID    __('Slideshow'),        // Title    'sola_seo_box_format',      // Callback function    'post',             // Admin page (or post type)    'side',             // Context    'default'           // Priority);}function sola_seo_box_format(){    global $fields,$post;  // Use nonce for verification  echo '';  echo '
  
   '; foreach ($fields as $field) { // get current post meta data $meta = get_post_meta($post->ID, $field['id'], true); echo '
   
     '. '
     '. '
     '; } echo '
   
'. $field['name']. ''; switch ($field['type']) { case 'text': echo ''. ''. $field['desc']; break; case 'textarea': echo ''. ($meta ? $meta : $field['default']) . ''. ''. $field['desc']; break; case 'select': echo ''; foreach ($field['options'] as $option) { echo ''. $option . ''; } echo ''; break; case 'radio': foreach ($field['options'] as $option) { echo '' . $option['name']; } break; case 'checkbox': echo ''; break; } echo ' '.'
';}function sola_save_post_meta_boxes($post_id) { global $fields, $post; //Verify nonce if (!wp_verify_nonce($_POST['sola_meta_box_nonce'], basename(__FILE__))) { return $post_id; } //Check autosave if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return $post_id; }//Get the post type object. $post_type = get_post_type_object( $post->post_type ); //Check permissions if ( !current_user_can( $post_type->cap->edit_post, $post_id ) ) return $post_id; foreach ($fields as $field) { $old = get_post_meta($post_id, $field['id'], true); $new = $_POST[$field['id']]; if ($new && $new != $old) { update_post_meta($post_id, $field['id'], $new); } elseif ('' == $new && $old) { delete_post_meta($post_id, $field['id'], $old); } }}?>

這段代碼會在文章建立和編輯頁面建立如下所示的Post Meta Box,如

讀取投影片文章

接下來修改slider.php,過去只需要查詢custom post type,現在使用post meta box實現,就需要根據post的meta資訊搜尋投影片,代碼如下

$args = array('meta_query' => array(    array(        'key' => 'sola-post-slider',        'value' => 'on',    )));$slides = get_posts($args);

用get_posts()和meta_query參數結合,就可以達到目的,有了資料,直接迴圈輸出就行了

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.