排序和显示自定义帖子类型 Wordpress
Posted
技术标签:
【中文标题】排序和显示自定义帖子类型 Wordpress【英文标题】:Sort and display custom post types Wordpress 【发布时间】:2015-09-08 23:56:30 【问题描述】:所以我有一个选项页面,并创建了以下代码:
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
?>
但我有点迷失了如何让我的页面显示检查值。 它应该随机显示自定义帖子类型 - 按顺序或按类别,具体取决于在管理面板的选项页面中选择的内容。
有没有人有办法做到这一点?
【问题讨论】:
【参考方案1】:设置不是 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
【讨论】:
我无法弄清楚如何使用上面的方法来让它工作。 @PatrickZiebellThøgersen 还是卡住了伙伴?嗯,你想不通什么?如何获取值? 是的。根据选择的选项,我无法让它显示任意一个 random 。按订单或按猫以上是关于排序和显示自定义帖子类型 Wordpress的主要内容,如果未能解决你的问题,请参考以下文章
WordPress WP_Query:根据自定义元值显示自定义帖子类型,并按另一个自定义元值排序
在 Wordpress 中,如何将自定义帖子类型的默认管理员排序顺序设置为自定义列?
用于显示距当前日期超过 8 个月的 WordPress 自定义帖子类型的自定义查询