简体   繁体   English

Wordpress ACF-如何从类别页面获取字段

[英]Wordpress ACF - How to get field from category page

I have a website with several categories and now I'm implementing the languages and I need to add a custom field to category page but I can't get the field. 我有一个包含多个类别的网站,现在我正在实现这些语言,我需要在类别页面上添加一个自定义字段,但我无法获取该字段。

So this is my custom field: 这是我的自定义字段: 在此处输入图片说明

Next I go to category page and introduce the text in each language. 接下来,我进入类别页面并介绍每种语言的文本。

In my code I do this: 在我的代码中,我这样做:

<?php
      $obj = get_queried_object();
      $ar = array('child_of' => $obj->term_id);

      $categories = get_categories( $ar );

      foreach($categories as $category) {
        $custom_field = get_field('descricao_traducoes', $obj->term_id);
         var_dump($custom_field);
      }
?>

But returns me null . 但是返回我为null

How can I do that? 我怎样才能做到这一点?

I think you're just passing the wrong variable as a second parameter in get_field ! 我认为您只是在get_field错误的变量作为第二个参数get_field

Do $category->term_id instead. 改用$category->term_id

(Unless you meant to dump the descricao_traducoes of the queried object for as many times as there are categories) (除非您打算将查询对象的descricao_traducoes转储为类别的次数)

<?php

$obj        = get_queried_object();
$ar         = array('child_of' => $obj->term_id);
$categories = get_categories( $ar );

foreach($categories as $category) {
    $custom_field = get_field('descricao_traducoes', $category->term_id);

    var_dump($custom_field);
}

From the ACF docs : ACF文档

get_field($selector, [$post_id], [$format_value]);

  • $selector (string) (Required) The field name or field key. $selector (字符串)(必需)字段名称或字段键。
  • $post_id (mixed) (Optional) The post ID where the value is saved. $post_id (混合)(可选)保存值的帖子ID。 Defaults to the current post. 默认为当前帖子。
  • $format_value (bool) (Optional) Whether to apply formatting logic. $format_value (bool)(可选)是否应用格式化逻辑。 Defaults to true. 默认为true。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM