doukou4066 2019-02-27 10:22
浏览 26

WordPress:获取帖子条款并按关系/孩子订购?

At the moment I am writing a little PHP script for myself and my WordPress website. However, I am struggling to set and read the terms for my posts.

For example (this is just a short excerpt) those are my terms:

- Term1
 - Sub-term1
  - Sub-Sub-term1
   - Sub-Sub-Sub-term1
 - Sub-term2
 - Sub-term3
  - Sub-Sub-term1
- Term2
 - Sub-term4
  - Sub-Sub-term2

And those two cases I have which I have to cover:

  1. I want to set the term e.g. Sub-Sub-Sub-term1 by slug. This is pretty easy and already working for me. I just do it by executing this line:

    wp_set_object_terms($post_id, $my_term_id, $my_term_field, true);

  2. And this is the most difficult part: I want to read the terms but in the order they are selected. So e.g. the term Sub-Sub-Sub-term1 is selected, all the superterms above are selected too. So, I want to get an array returned, where the first index is the most superterm, in this case it would be Term1, on index 1 in the array should be the term Sub-term1 and so on and so on. But in addition to that I want the term id, the term name and the term slug to be returned..

    The problem I am facing now is that I don't know how do order the terms... However, I already know how to get all the terms set for a post:

    wp_get_object_terms($post_id, $my_term_field);

    This returns an array with all terms as an object but also with all the stuff I don't need. So, for e.g. description and all the other fields. I just need the term id, the term name and the term slug.

    All in all, I don't know how to order the terms I get returned.. Do you guys have an idea on how to do this? I mean, there has to be a relation somehow cause WordPress is showing them as children of each superterm and they are also indented in the frontend!

    Here you can see that the terms are indented: enter image description here

I would appreciate any kind of help! Thank you!

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号