已选中自定义粘滞字段的获取术语(_T)

时间:2013-10-31 作者:Toni Michel Caubet

我可以添加一个额外的自定义字段,名为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 );
    }
}
enter image description here

现在,我正在尝试检查所有具有此值的分类法,如下所示:

$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
     ) );
知道我错过了什么吗?

2 个回复
最合适的回答,由SO网友:Toni Michel Caubet 整理而成

OK, got it

<?php
    $types = get_terms( 
        \'tvr_amenity\', 
        array(
            \'parent\'    => \'0\',
            \'hide_empty\' => 1

         )
    );
    foreach( $types as $type ) :
            $myname = trim($type->name);
            $meta = get_option(\'amenity_sticky\');
            if (empty($meta)) $meta = array();
            if (!is_array($meta)) $meta = (array) $meta;
            $meta = isset($meta[$type->term_id]) ? $meta[$type->term_id] : array();
            $value = $meta[\'is_sticky\'];
            if(!$value) continue; /* skip term if is_sticky not checked */
?>
    <p>
    <input type=\'checkbox\' name="apartment_amenity[]" value=\'<?php echo $type->term_id ?>\' class=\'tvr_amenity\'> <?php echo $myname ?>
    </p>
<?php endforeach;   ?>
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
            }

结束

相关推荐

Wp set post terms not work

我的代码不起作用,它没有添加分类法add_action(\'init\', \'post_test\', 10); function post_test() { $empty = get_page_by_title(\'Produto teste\', OBJECT, \'produtos\'); if(empty($empty)) { $insert_post = array( \'post_status\'