自定义分类排序顺序不起作用
custom taxonomy sort order not working
我创建了一个名为 product_categories 的自定义分类法。
它有三个字段:
横幅图片
类别图像和
用于排序。
我在其中添加了 10 个类别,还提供了排序顺序输入。
现在我想按排序顺序显示它,但它不起作用。
在 pt-categories 中输入的排序顺序是这样的
<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(' '); ?></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>
它显示的是类别名称和图像,但未按排序顺序显示。
您的 Categories Called
代码有误
更正代码
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);
}
}
我创建了一个名为 product_categories 的自定义分类法。
它有三个字段:
横幅图片
类别图像和
用于排序。
我在其中添加了 10 个类别,还提供了排序顺序输入。
现在我想按排序顺序显示它,但它不起作用。
在 pt-categories 中输入的排序顺序是这样的
<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(' '); ?></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>
它显示的是类别名称和图像,但未按排序顺序显示。
您的 Categories Called
代码有误更正代码
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);
}
}