dougan6982 2018-10-11 21:14
浏览 99
已采纳

get_category_parents到数组wordpress

Ok So I'm using wordpress and PHP.

Basicly I get a chosen category from a dropdown menu. I then find it's parents.

But then comes the part where I seem to fail. The output of $parents is a string with the names instead of the ID's.

So I try to get each word from the string into an array. And loop through them and convert them to their ID number.

I commented out where it doesnt seem to work. What am I doing wrong?

//Create array
$categoriesArray = [];

//Get choice from wp-dropdown
$selected_val = $_POST['cat'];

//Get parents from choice divided by (this seems to output a string)
$parents = get_category_parents( $selected_val, true, ',' );

/*
//Make array from string
$categoriesArray = explode(",",$parents);

for ($i = 0; $i < count($categoriesArray); $i++) {
{
    $categoriesArray[$i] = get_cat_ID($categoriesArray[$i]);
}
*/

//the array should look something like this.
//$categoriesArray = ["21","44"];
  • 写回答

2条回答 默认 最新

  • douxian7117 2018-10-12 13:45
    关注

    It had to do with the name of the category. I needed to use the slug instead of the name.

    Here is my code that works. I can now make a new post and give it a category with their parents included!

    //Create array
    $categoriesArray = [];
    
    //Get choice from wp-dropdown
    $selected_val = $_POST['cat'];
    
    //Get parents from choice divided by ,
    $parents = get_category_parents( $selected_val, true, ',',false);
    
    //Make array from string
    $categoriesArray = explode(",",$parents);
    for ($i = 0; $i < count($categoriesArray); $i++)
    {
        $categoriesArray[$i] = get_cat_ID($categoriesArray[$i]);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题