php - 如何在 get_option 函数中添加类别 ID
问题描述
更新 我做了一些研究并了解到我可以将字段保存到数据库中的术语元,而不是像我一开始所做的那样保存到选项中并轻松输出它。但是我在保存时做错了,因为术语元表中没有条目。你能看看我的代码并指出我的错误吗?
<?php
function realized_category_field( $term ){
$term_id = $term->term_id;
$term_meta = get_term_meta('realized');
?>
<tr class="form-field">
<th scope="row">
<label for="term_meta[realized]"><?php echo _e('Has the project been implemented?') ?></label>
<td>
<select name="term_meta[realized]" id="term_meta[realized]">
<option value="0" <?=($term_meta['realized'] === 0) ? 'selected': ''?>><?php echo _e('NO'); ?></option>
<option value="1" <?=($term_meta['realized'] === 1) ? 'selected': ''?>><?php echo _e('YES'); ?></option>
</select>
</td>
</th>
</tr>
<?php
}
// Add the dropdown to the Edit form
add_action( 'category_edit_form_fields', 'realized_category_field' );
// Save the field
function realized_save_term_meta() {
$term_id = $term->term_id;
$old = get_term_meta($term_id, 'realized', true);
$new = $_POST['realized'];
if ( $new && $new !== $old ) {
update_term_meta( $term_id, 'realized', $new );
} elseif ( '' === $new && $old ) {
delete_term_meta( $term_id, 'realized', $old );
}
}
add_action('edited_category', 'category_edit_form_fields');
function realized_category_columns_values( $deprecated, $column_name, $term_id) {
if($column_name === 'realized'){
$term_meta = get_term_meta('realized');
if($term_meta['realized'] === 1){
echo _e('YES');
}else{
echo _e('NO');
}
}
}
add_action( 'manage_category_custom_column' , 'realized_category_columns_values', 10, 3 );
// Add column to Category list
function realized_category_columns($columns)
{
return array_merge($columns, array('realized' => __('Has the project been implemented?')));
}
add_filter('manage_edit-category_columns' , 'realized_category_columns');
?>
我想创建一个下拉列表来定义一个类别是否已关闭,并稍后在查询中检查它并仅打印打开的类别。
我暂停了本教程以实现我的代码的第一部分,但我一直坚持输出数据并检查类别是打开还是关闭。
我的问题是我不知道如何传递term_id
to $terms
,而且我不确定我写的 if 语句是否正确。
有人可以帮我吗?
<?php
$args = array(
'show_option_all' => '',
'orderby' => 'ID',
'order' => 'ASC',
);
$categories = get_categories($args);
$terms = get_option('taxonomy_'.$categories['term_id']);
echo '<ul class="realized-projects-category-list">';
foreach($categories as $category) {
if ($terms['taxonomy_'.$category->term_id] === '1') {
echo '<li>';
echo '<a href="' . get_category_link($category->term_id) . '">' . $category->name . '</a>';
echo '</li>';
}
}
echo '</ul>';
?>
解决方案
推荐阅读
- swift - Swift - 更新现有核心数据实体的最佳实践
- angular - 类型对象的角度参数不可分配给参数 Never
- python - 需要一些帮助来创建有效的搜索功能
- python - 如何画出keras CNN架构?
- sql - 使用池连接 SQL 服务器数据库,查询错误,字段使用重音
- c# - MSBuild - 根据构建配置复制一个或另一个文件
- elasticsearch - 根据字段值匹配、顺序和有限时间范围生成 Elasticsearch 查询输出
- mongodb - Docker Mongo 实例的身份验证不正确
- rust - 没有为 Crate 结构实现调试
- ios - 如何动态改变 UICollectionView 的高度以适应内容大小