根据自定义字段值将自定义帖子分配给自定义分类

时间:2013-04-24 作者:chris

This answer 做我需要做的事情,但唯一的事情是我需要它选择多个类别。

因此,我已将代码更改为“基于薪资工作”,以便将薪资输入到自定义字段,然后它会自动选择工作正常的薪资级别类别,但现在我有一个“薪资来自”自定义字段和一个“薪资至”自定义字段,因此我有一个“薪资至”值,那么如果这两个值都有意义的话,我如何获得将职位添加到多个类别的代码?

下面是我的代码,用于基于salaryband的值进行类别选择,因此我还需要它来选择基于salaryband2的类别(自定义字段)

function add_my_taxonomy($post_ID) {
    $salary = array(
\'1015\' => \'10 - 15 ph\',
\'100140\' => \'100K - 140K\',
\'101120\' => \'101K - 120K pa\',
\'121150\' => \'121K – 150K pa\',
\'151170\' => \'151K – 170K pa\',
\'171200\' => \'171K – 200K pa\',
\'2025\' => \'20 – 25 ph\',
\'2025k\' => \'20K – 25K pa\',
\'2630\' => \'26 – 30 ph\',
\'2630k\' => \'26K – 30K pa\',
\'3040\' => \'30 – 40 ph\',
\'3140k\' => \'31K – 40K pa\',
\'4150\' => \'41K – 50K pa\',
\'5160\' => \'51K – 60K pa\',
\'6170\' => \'61K – 70K pa\',
\'7180\' => \'71K – 80K pa\',
\'8190\' => \'81K – 90K pa\',
\'91100\' => \'91K – 100K pa\'
    );
    $zip = array(
\'1015\' => \'10, 11, 12, 13, 14, 15,\',
\'100140\' => \'101000, 102000, 103000, 104000, 105000, 106000, 107000, 108000, 109000, 110000, 111000, 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 120000, 121000, 122000, 123000, 124000, 125000, 126000, 127000, 128000, 129000, 130000, 131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000, 140000,\',
\'101120\' => \'101000, 102000, 103000, 104000, 105000, 106000, 107000, 108000, 109000, 110000, 111000, 112000, 113000, 114000, 115000, 116000, 117000, 118000, 119000, 120000,\',
\'121150\' => \'121000, 122000, 123000, 124000, 125000, 126000, 127000, 128000, 129000, 130000, 131000, 132000, 133000, 134000, 135000, 136000, 137000, 138000, 139000, 140000, 141000, 142000, 143000, 144000, 145000, 146000, 147000, 148000, 149000, 150000,\',
\'151170\' => \'151000, 152000, 153000, 154000, 155000, 156000, 157000, 158000, 159000, 160000, 161000, 162000, 163000, 164000, 165000, 166000, 167000, 168000, 169000, 170000,\',
\'171200\' => \'171000, 172000, 173000, 174000, 175000, 176000, 177000, 178000, 179000, 180000, 181000, 182000, 183000, 184000, 185000, 186000, 187000, 188000, 189000, 190000, 191000, 192000, 193000, 194000, 195000, 196000, 197000, 198000, 199000, 200000,\',
\'2025\' => \'20, 21, 22, 23, 24, 25,\',
\'2630\' => \'26, 27, 28, 29, 30,\',
\'2025k\' => \'20000, 21000, 22000, 23000, 24000, 25000,\',
\'2630k\' => \'26000, 27000, 28000, 29000, 30000,\',
\'3040\' => \'31, 32, 33, 34, 35, 36, 37, 38, 39, 40,\',
\'2630k\' => \'26000, 27000, 28000, 29000, 30000,\',
\'3140k\' => \'31000, 32000, 33000, 34000, 35000, 36000, 37000, 38000, 39000, 40000,\',
\'4150\' => \'41000, 42000, 43000, 44000, 45000, 46000, 47000, 48000, 49000, 50000,\',
\'5160\' => \'51000, 52000, 53000, 54000, 55000, 56000, 57000, 58000, 59000, 60000,\',
\'6170\' => \'61000, 62000, 63000, 64000, 65000, 66000, 67000, 68000, 69000, 70000,\',
\'7180\' => \'71000, 72000, 73000, 74000, 75000, 76000, 77000, 78000, 79000, 80000,\',
\'8190\' => \'81000, 82000, 83000, 84000, 85000, 86000, 87000, 88000, 89000, 90000,\',
\'91100\' => \'91000, 92000, 93000, 94000, 95000, 96000, 97000, 98000, 99000, 100000,\'
    );
    foreach ($zip as $code => $salaryband){
        $salaryband = explode(\',\', $salaryband);
        $customField = get_post_meta($post_ID,\'salaryband\',true);
        if (in_array($customField,$salaryband)){
            wp_set_object_terms( $post_ID, $salary[$code], \'salary\' );
        }
    } 

1 个回复
SO网友:chris

您好,谢谢,现在问题解决了,我得到了一位开发人员的帮助,我知道他为我修复了wp\\u set\\u object\\u terms()的第四个参数,名为append。在通话期间将其设置为true应添加术语,而不会取消已设置的术语。

因此,如果其他人有这个问题,再次感谢所有评论,谢谢

结束

相关推荐

是否使用wp_Dropdown_Categories设置选定的Term_id?

我使用这段代码来获取自定义分类法的term\\u id。下拉列表确实列出了分类法类型的术语。当我打印$terms变量时,我会在前端看到我正在编辑的文章的术语ID。为什么没有将术语ID设置为所选选项?$terms = get_the_terms( $current_post, \'Type\' ); print_r($terms); wp_dropdown_categories( \'taxonomy=Type&hierarchical=1&hide_empty=0&