繁体   English   中英

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

[英]Post values from Wordpress Metabox to custom taxonomy

我想知道是否有从wordpress metabox发布值并将其添加到自定义非分层分类法(标签)的方法。

这是自定义分类法的代码

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

我目前正在使用wpalchemy metabox,该metabox的代码是

<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>';
                                    }
                                ?>

但是我想知道是否有一种方法可以将值(只是成分)推入自定义分类法中?

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

您可以访问http://farinspace.com/wpalchemy-metabox/#comment-3941了解更多信息,它由您真正需要的主题下的一些很好的参数组成!

听起来您需要使用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;
    }

?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM