dqyhj2014 2015-06-23 09:07
浏览 35
已采纳

排序和显示自定义帖子类型Wordpress

So I have an option page, and created the following code:

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
}
?>

But Im kinda lost in how im getting my page to display the checked value. Its supposed to display custom post types either randomly - by order or by catogory depending on what is chosen in the option page in admin panel.

Does anyone have a way of doing this?

  • 写回答

1条回答 默认 最新

  • doujiu3768 2015-06-23 12:04
    关注

    The setup is not 100% correct, you never hit the options.php to process the update, and you forgot to esc_attr; this sample is working but it will need more changes to do what you need at 100% but here you can find some clues in how to do it 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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来