几周来一直在为解决我的问题而苦苦挣扎。
案件:
我有一个名为:design的定制post类型。这个CPT有一个自定义字段(由ACF插件生成),名为thematique。我为的类别创建了相同的自定义字段(主题)。
预期行为:
我希望,每当我们制作一个get_posts()或WP_Query时,如果一个设计的主题字段是空的,那么它应该继承它的分类的主题。
我已经调查了pre_get_posts钩子,但我不太确定如何处理它。
有人有主意吗?
谢谢你,我真的很感谢你的帮助!
发布于 2019-05-24 07:58:39
您可以简单地这样做,在WP查询中,您可以对每个返回的项进行格式化,添加以下内容:
<?php $thematique = get_field('thematique'); //Gets current posts value of fiels
<?php if (empty($thematique)){ //Checks if the field is empty, if so do the following
$postCat = get_the_category(); //Get current posts category ID
$catID = 'category_' . $postCat; //Merge category ID and needed string for ACF
$thematique = get_field('thematique', $catID); //Updated the value of $thematique with categories value
}?>虽然没有经过测试,但这确实应该像ACF所说的那样起作用,从类别中获取值。找出更多在这里。
https://stackoverflow.com/questions/56263206
复制相似问题