我可以添加一个额外的自定义字段,名为sticky
, 使用wp taxonomy meta 插件,如下所示:
function YOUR_PREFIX_register_taxonomy_meta_boxes()
{
// Make sure there\'s no errors when the plugin is deactivated or during upgrade
if ( !class_exists( \'RW_Taxonomy_Meta\' ) )
return;
$meta_sections = array();
// First meta section
$meta_sections[] = array(
\'title\' => \'Sticky\', // section title
\'taxonomies\' => array(\'tvr_amenity\'), // list of taxonomies. Default is array(\'category\', \'post_tag\'). Optional
\'id\' => \'sticky\', // ID of each section, will be the option name
\'fields\' => array( // List of meta fields
array(
\'name\' => \'Show in home filters\',
\'id\' => \'sticky\',
\'type\' => \'checkbox\',
),
),
);
foreach ( $meta_sections as $meta_section )
{
new RW_Taxonomy_Meta( $meta_section );
}
}
现在,我正在尝试检查所有具有此值的分类法,如下所示:
$types = $types = get_terms( \'tvr_amenity\', array(
\'parent\' => \'0\',
\'hide_empty\' => 1,
\'sticky\' => 1
) );
但过滤器被忽略(显示所有父分类),它返回的值与以下值完全相同:
$types = $types = get_terms( \'tvr_amenity\', array(
\'parent\' => \'0\',
\'hide_empty\' => 1
) );
知道我错过了什么吗?
SO网友:lonchbox
首先更改此项:
// First meta section
$meta_sections[] = array(
\'title\' => \'Sticky\', // section title
\'taxonomies\' => array(\'tvr_amenity\'), // list of taxonomies. Default is array(\'category\', \'post_tag\'). Optional
\'id\' => \'amenity_sticky\', // ID of each section, will be the option name
\'fields\' => array( // List of meta fields
array(
\'name\' => \'Show in home filters\',
\'id\' => \'is_sticky\',
\'type\' => \'checkbox\',
),
),
);
foreach ( $meta_sections as $meta_section )
{
new RW_Taxonomy_Meta( $meta_section );
}
要在前端获取信息,我认为这可能是解决方案:
$terms = get_terms( \'tvr_amenity\', array(
\'hide_empty\' => 0,
) );
foreach( (array) $terms as $term) {
$meta = get_option(\'tvr_amenity\');
if (empty($meta)) $meta = array();
if (!is_array($meta)) $meta = (array) $meta;
$meta = isset($meta[$term->term_id]) ? $meta[$term->term_id] : array();
$term_link = get_term_link( $term, \'tvr_amenity\' );
$is_sticky = $meta[\'is_aticky\'];
$is_sticky_id = false;
if (is_array($is_sticky)) {
foreach ($is_sticky as $att) {
$is_sticky_id = $att;
}
} ?>
<a class="term-link" href="<?php echo $term_link; ?>" class="<?php echo $is_sticky; ?>">
<?php echo sprintf(__(\'%s\', \'udla\'), $term->name); ?>
</a>
<p><?php echo $term->description; ?></p>
<?php
}