将值从Wordpress Metabox发布到自定义分类

时间:2011-02-13 15:51:01

标签: php wordpress metadata meta-tags

我想知道是否还有从wordpress元数据发布vales并将其添加到自定义非分层分类标签(标签)。

以下是自定义分类法的代码

// Custom taxonomy for Ingredients
register_taxonomy(  
   'ingredients',  
   'mysite_tax',  
    array(  
        'hierarchical' => false,  
        'label' => 'Ingredients',  
        'query_var' => true,  
        'rewrite' => true  
    )  
);

我目前正在使用wpalchemy元数据库,元数据的代码是

<div class="my_meta_control">

    <h4>Ingredients</h4>

    <?php while($mb->have_fields_and_multi('recipe_ingredients')): ?>
    <?php $mb->the_group_open(); ?>

        <?php $mb->the_field('quantity'); ?>
        <label>Quantity and Ingredients</label>
        <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

        <?php $mb->the_field('ingredients'); ?>
        <p><input type="text" name="<?php $mb->the_name(); ?>" value="<?php $mb->the_value(); ?>"/></p>

        <?php $selected = ' selected="selected"'; ?>

        <br/><?php $metabox->the_field('units',1); ?>
        <select name="<?php $metabox->the_name(); ?>">
        <option value="unit"<?php if ($metabox->get_the_value() == 'unit') echo $selected; ?>>--Select Unit--</option>
                    <option value="Test"<?php if ($metabox->get_the_value() == 'Test') echo $selected; ?>>Test</option>
                    <option value="Test2"<?php if ($metabox->get_the_value() == 'Test2') echo $selected; ?>>Test 2</option>
                    <option value="Test3"<?php if ($metabox->get_the_value() == 'Test3') echo $selected; ?>>Test 3</option>
        </select>

        <a href="#" class="dodelete button">Remove Document</a>
        </p>

    <?php $mb->the_group_close(); ?>
    <?php endwhile; ?>

    <p style="margin-bottom:15px; padding-top:5px;"><a href="#" class="docopy-ingredients button">Add Document</a></p>
    <br /><p><a style="float:right; margin:0 10px;" href="#" class="dodelete-ingredients button">Remove All</a></p>

</div>

我想将配料字段中的值仅传递给自定义分类。我是一个PHP新手,所以它可能很容易,我没有意识到:)

我可以使用此代码

获取页面模板中的值
<?php
                                    // loop a set of field groups
                                    while($ingredients_metabox->have_fields('recipe_ingredients'))
                                    {
                                        echo '<li>';
                                        $ingredients_metabox->the_value('quantity');

                                        $ingredients_metabox->the_value('ingredients');

                                        $ingredients_metabox->the_value('units');
                                        echo '</li>';
                                    }
                                ?>

然而,我想知道是否有办法将价值观(只是成分)推入自定义分类中?

3 个答案:

答案 0 :(得分:0)

请参阅此WPAlchemy评论及其后的一些评论。

答案 1 :(得分:0)

你可以看到http://farinspace.com/wpalchemy-metabox/#comment-3941更多的想法,它包含你真正需要的主题下的一些好的论点!

答案 2 :(得分:0)

听起来你需要使用save_filter,不过我想知道WPAlchemy是否可行。学习WPAlchemy所花费的时间可以花在学习核心WordPress API和PHP上,这在长期内会更有用。也就是说,如果你选择坚持使用WPAlchemy,下面就是如何实现* save_filter *。

<?php

    $foo_meta_box = new WPAlchemy_MetaBox(array(
        'id' => '_custom_meta',
        'title' => 'My Custom Meta',
        'template' => TEMPLATEPATH . '/custom/meta.php',
        'save_filter' => 'foo_alchemy_save_filter'
    ));

    function foo_alchemy_save_filter($meta, $post_id) {
        unset($meta['ingredients']; // remove ingredients meta data before saving
        // do your custom taxonomy stuff using taxonomy API such as wp_set_object_terms() 
        return $meta;
    }

?>
相关问题