2015-06-23 80 views
4

所以,我有一個選項頁,並創建了下面的代碼:排序和顯示自定義文章類型的WordPress

add_action('admin_menu', 'add_global_custom_options'); 
add_theme_support('post-formats', array('Random', 'Order', 'Category')); 
function add_global_custom_options() 
{ 
    add_options_page('Sort Registrar List', 'Sort Registrar List', 'manage_options', 'functions','global_custom_options'); 
} 

function global_custom_options() 
{ 
    $options = array (
    'random' => array(
     'post_type' => 'registrar', 
     'posts_per_page' => -1, 
     'orderby' => 'rand' 
    ), 
    'category' => array(
     'post_type' => 'registrar', 
     'posts_per_page' => -1, 
     'orderby' => 'ASC' 
    ), 
    'menu_order' => array(
     'post_type' => 'registrar', 
     'posts_per_page' => -1, 
     'orderby' => 'DESC' 
    ) 
     ); 

?> 
    <div class="wrap"> 
     <h2>Sort Registrar List</h2> 
     <form method="post" action=""> 
      <?php wp_nonce_field('update-options') ?> 
      <p><strong>Display Randomly:</strong><br /> 
       <input type="radio" name="random" size="45" value="<?php echo get_option('random'); ?>" /> 
      </p> 
      <p><strong>Display by Category:</strong><br /> 
       <input type="radio" name="category" size="45" value="<?php echo get_option('category'); ?>" /> 
      </p> 
      <p><strong>Display By Order:</strong><br /> 
       <input type="radio" name="order" size="45" value="<?php echo get_option('menu_order'); ?>" /> 
      </p> 
      <p><input type="submit" name="Submit" value="Store Options" /></p> 
      <input type="hidden" name="action" value="update" /> 
      <input type="hidden" name="page_options" value="random,category,menu_order" /> 
     </form> 
    </div> 
     <?php 
} 
?> 

但是我在即時得到我的網頁如何顯示選中的值有點丟失。 它應該隨機顯示自定義帖子類型 - 按順序或通過catogory,取決於管理面板中選擇頁面中選擇的內容。

有沒有人有辦法做到這一點?

回答

2

設置不是100%正確的,你從來沒有擊中options.php來處理更新,你忘了esc_attr;這個示例正在工作,但它需要更多的更改才能做到100%所需,但在這裏您可以找到一些線索,以瞭解如何做到這一點How can I set and get the values of a multiple select with the Wordpress settings API for a theme options page?;

function add_global_custom_options(){ 
    add_options_page(
     'Sort Registrar List', 
     'Sort Registrar List', 
     'manage_options', 
     'order_by_options', 
     'global_custom_options' 
    ); 

    add_action('admin_init', 'register_new_options'); 
} 
add_action('admin_menu', 'add_global_custom_options'); 

function register_new_options(){ 
    register_setting('global_custom_option', 'register_new_option_random'); 
    register_setting('global_custom_option', 'register_new_option_category'); 
    register_setting('global_custom_option', 'register_new_option_menu_order'); 
} 
add_action('admin_init', 'register_new_option'); 

function global_custom_options(){ 


    //this look unsed.... for debugger it is ;) 
    $options = array (
     'random' => array(
      'post_type' => 'download', 
      'posts_per_page' => -1, 
      'orderby' => 'rand' 
     ), 
     'category' => array(
      'post_type' => 'download', 
      'posts_per_page' => -1, 
      'orderby' => 'ASC' 
     ), 
     'menu_order' => array(
      'post_type' => 'download', 
      'posts_per_page' => -1, 
      'orderby' => 'DESC' 
     ) 
    ); 

    ?> 
    <div class="wrap"> 
     <h2>Sort Registrar List</h2> 

     <form method="post" action="options.php"> 
      <?php settings_fields('global_custom_option'); ?> 
      <?php do_settings_sections('global_custom_option'); ?> 
      <table class="form-table"> 
       <tr valign="top"> 
        <th scope="row">Display Randomly</th> 
        <td><input type="text" name="register_new_option_random" value="<?php echo esc_attr(get_option('register_new_option_random')); ?>" /></td> 
       </tr> 

       <tr valign="top"> 
        <th scope="row">Display by Category:</th> 
        <td><input type="text" name="register_new_option_category" value="<?php echo esc_attr(get_option('register_new_option_category')); ?>" /></td> 
       </tr> 

       <tr valign="top"> 
        <th scope="row">Display By Order:</th> 
        <td><input type="text" name="register_new_option_menu_order" value="<?php echo esc_attr(get_option('register_new_option_menu_order')); ?>" /></td> 
       </tr> 
      </table> 
      <?php submit_button(); ?> 
     </form> 
    </div> 
    <?php 
} 
+0

我無法弄清楚如何使用上面的工作。 –

+0

@PatrickZiebellThøgersen仍然卡住隊友?嗯你能弄清楚什麼?如何獲取數值? – jycr753

+0

是的。取決於選擇的選項,我不能讓它隨機顯示。由命令或貓 –

相關問題