我正在使用以下内容:
$catargs = array(
\'meta_key\'=> \'category_custom_order\',
\'orderby\' => \'meta_value_num\',
\'order\'=> \'ASC\',
);
但是我可以得到按元值键排序的类别列表
不幸的是,它仍然按标题列出类别。
有没有更好的方法?谢谢你的帮助。
完整代码:
<?php
// Query by category and group
$catargs = array(
\'meta_key\'=> \'category_custom_order\',
\'orderby\' => \'meta_value_num\',
\'order\'=> \'DESC\',
);
$categories = get_categories( $catargs );
foreach ($categories as $category) { ?>
<section id="<?php echo $category->slug; ?>" class="entry-category">
<header class="entry-header">
<h2 class="category-header"><?php echo $category->name;// Category title ?></h2>
</header>
<div class="entry-desc"><?php echo $category->description; ?></div>
<?php // WP_Query arguments
$args = array (
\'post_type\' => \'post\',
\'cat\' => $category->cat_ID,
\'posts_per_page\' => \'-1\',
\'order\' => \'DESC\',
\'orderby\' => \'title\',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h3 class="entry-title">
<?php the_title(); ?>
</h3>
</header>
<div class="entry-summary">
<?php the_content(); ?>
</div>
</article>
<?php }
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata(); ?>
</section>
<!-- #<?php echo $category->slug; ?> -->
我只想根据包含的图像中的元键值对类别进行排序。
我正在使用下面的函数为仪表板下的类别创建自定义字段:
/*
* Add Custom Field To Category Form
*/
add_action( \'category_add_form_fields\', \'category_form_custom_field_add\', 10 );
add_action( \'category_edit_form_fields\', \'category_form_custom_field_edit\', 10, 2 );
function category_form_custom_field_add( $taxonomy ) {
?>
<div class="form-field">
<label for="category_custom_order">Custom Order</label>
<input name="category_custom_order" id="category_custom_order" type="text" value="" size="10" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</div>
<?php
}
function category_form_custom_field_edit( $tag, $taxonomy ) {
$option_name = \'category_custom_order_\' . $tag->term_id;
$category_custom_order = get_option( $option_name );
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="category_custom_order">Custom Order</label></th>
<td>
<input type="text" name="category_custom_order" id="category_custom_order" value="<?php echo esc_attr( $category_custom_order ) ? esc_attr( $category_custom_order ) : \'\'; ?>" size="10" aria-required="true" />
<p class="description">Enter a custom order value.</p>
</td>
</tr>
<?php
}
/** Save Custom Field Of Category Form */
add_action( \'created_category\', \'category_form_custom_field_save\', 10, 2 );
add_action( \'edited_category\', \'category_form_custom_field_save\', 10, 2 );
function category_form_custom_field_save( $term_id, $tt_id ) {
if ( isset( $_POST[\'category_custom_order\'] ) ) {
$option_name = \'category_custom_order_\' . $term_id;
update_option( $option_name, $_POST[\'category_custom_order\'] );
}
}//end
现在,我要寻找的是如何按自定义字段值从1到5按asc顺序排列类别列表?