【问题标题】:Post values from Wordpress Metabox to custom taxonomy将值从 Wordpress Metabox 发布到自定义分类
【发布时间】:2011-02-13 15:51:00
【问题描述】:

我想知道是否可以从 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>';
                                    }
                                ?>

但是我想知道是否有办法将值(只是成分)推入自定义分类?

【问题讨论】:

    标签: php wordpress metadata meta-tags


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      请参阅此 WPAlchemy 评论和一些关注它的 cmets。

      【讨论】:

      • 您好,很高兴看到您的网站已恢复正常运行 :)。周末我一直在看那些 cmets,似乎他们想做与我想做的相反的事情。似乎他们想从自定义分类中获取术语并将其拉入元框。我想将元框中的特定字段中的术语推送到自定义分类法。我也无法正确保存操作功能,它一直在破坏我的网站。
      【解决方案3】:

      您可以查看http://farinspace.com/wpalchemy-metabox/#comment-3941 了解更多信息,它包含您真正需要的主题下的一些很好的论据!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-15
        • 1970-01-01
        • 2013-04-21
        • 1970-01-01
        • 2020-01-04
        • 2016-10-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多