自定义分类排序顺序不起作用


custom taxonomy sort order not working

我创建了一个名为product_categories的自定义分类法。

它有三个字段:

  1. 用于横幅图像

  2. 用于类别图像和

  3. 用于排序。

我在其中添加了10个类别,给出了排序顺序输入。

现在我想按排序顺序显示它,但它不起作用。

在pt类别中,排序顺序的输入是这个

<tr class="form-field">
<th scope="row" valign="top"><label for="cat_sort_order"><?php _e('Product Sort Order'); ?></label></th>
<td>
    <input id="banner-url" name="term_meta[sort_order]" type="text" style="width: 100%;" value="<?php echo $term_meta['sort_order'] ? $term_meta['sort_order'] : ''; ?>" />
    <span class="description"><?php _e('&nbsp;'); ?></span>
</td>

保存功能是这个

    function save_product_categories_custom_fields($term_id)
    {
    if (isset($_POST['term_meta'])) {
        $t_id = $term_id;
        $term_meta = get_option("taxonomy_term_$t_id");
        $cat_keys = array_keys($_POST['term_meta']);
        foreach ($cat_keys as $key) {
            if (isset($_POST['term_meta'][$key])) {
                $term_meta[$key] = $_POST['term_meta'][$key];
            }
        }
     //save the option array
        update_option("taxonomy_term_$t_id", $term_meta);
    }
}

以下是被称为的类别

function getLatestProducts() { 
$args = array(
  'post_status' => 'publish',
  'post_type' => 'products', 
  'posts_per_page' => 12,
  'meta_key'        => '_cus_sort_order',
  'orderby'         => 'meta_value_num', 
  'order' => 'ASC'
);
?>
<?php
                $args = array(
                'orderby' => 'name',
                );
                $terms = get_terms('product_categories', $args);
                foreach($terms as $term) {      
               $prod_meta = get_option("taxonomy_term_".$term->term_id);
                    ?>
                <a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
                <?php
                    echo '<img src="'.$prod_meta['img'].'" title="" alt=""></a>';    ?>
                </div>
                <div class="product-name">
                <h5>
                <a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
                <?php echo $term->name;?>
                </a>
                </h5>

它显示的是类别名称和图像,但不是按排序顺序。

调用的类别的代码中有一个错误

已更正的代码

function getLatestProducts() { 
    $args = array(
    'post_status' => 'publish',
    'post_type' => 'products', 
    'posts_per_page' => 12,
    'meta_key'        => '_cus_sort_order',
    'orderby'         => 'meta_value_num, name', 
    'order' => 'ASC'
);
            $terms = get_terms('product_categories', $args);
            foreach($terms as $term) {      
           $prod_meta = get_option("taxonomy_term_".$term->term_id);
                ?>
            <a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
            <?php
                echo '<img src="'.$prod_meta['img'].'" title="" alt=""></a>';    ?>
            </div>
            <div class="product-name">
            <h5>
            <a href="<?php echo get_term_link($term->slug, 'product_categories') ?>">
            <?php echo $term->name;?>
            </a>
            </h5>

我已经删除了一个在主参数数组之后调用的参数数组。由于下面提到的参数数组覆盖了上面提到的参数阵列。

删除的参数数组

$args = array(
    'orderby' => 'name',
);

希望这能有所帮助!

扩展Mehul的答案,您的保存函数也有一些错误。

您是否检查了以前保存的分类排序顺序?

"taxonomy_term_$t_id"应该是"taxonomy_term_" . $t_id。否则,您将所有内容保存为taxonomy_term_$t_id选项,而不是动态术语id。

function save_product_categories_custom_fields($term_id)
{
if (isset($_POST['term_meta'])) {
    $t_id = $term_id;
    $term_meta = get_option("taxonomy_term_" . $t_id);
    $cat_keys = array_keys($_POST['term_meta']);
    foreach ($cat_keys as $key) {
        if (isset($_POST['term_meta'][$key])) {
            $term_meta[$key] = $_POST['term_meta'][$key];
        }
    }
 //save the option array
    update_option("taxonomy_term_" . $t_id, $term_meta);
}

}