从 Wordpress 中自定义 post 类型的类别中获取 ACF 文本字段值

Get the ACF text field value from the category of a custom post type in Wordpress

我正在尝试使用 ACF PRO 中文本字段的值创建另一个 class。

我有一个名为“投资组合”的自定义 post 类型,它有 4 个类别,ACF 字段设置添加到“分类等于类别”。

当我编辑类别时,我填写了我想要得到的名称,然后显示如下:

<div class="grid-item-catalog catalog-project-Holder **the name need to be here**">

如何从类别中获取 ACF 字段值?

其他信息:我的页面模板是page-portfolio.php,我使用的是ACF转发器。

这是我的代码:

<!-- catalog -->
<div class="grid-catalog catalog-portfolio-wrraper">  
  <?php if ( $query->have_posts() ) {  while ( $query->have_posts() ) { $query->the_post(); ?>
     <!-- Repeater -->
     <?php if( have_rows('portfolio-projects-repeater') ){ ?>
        <?php while( have_rows('portfolio-projects-repeater') ) { the_row(); 
          // vars
          $projectDescription = get_sub_field('project-name');
          $projectImage = get_sub_field('project-image');
          $projectLinkText = get_sub_field('project-link-text');
          $projectLink = get_sub_field('project-link');                                                       
          ?> 
             <div class="grid-item-catalog catalog-project-Holder {the name need to be here}"> 
                  <div class="catalog-project-name"><?php the_title(); ?></div>
                  <div class="catalog-project-content-container">
                     <div class="catalog-project-image"><img src="<?php echo $projectImage['url']; ?>" alt="<?php echo $projectImage['alt'] ?>" /></div>
                     <div class="catalog-project-content">
                        <div class="description-link-container">
                             <div class="description"><?php echo $projectDescription; ?></div>
                             <div class="link"><a href="<?php echo $projectLink; ?>" target="_blank" rel="nofollow"><?php echo $projectLinkText; ?></a></div>
                        </div>
                     </div>
                  </div>
              </div>
        <?php } ?>
    <?php } ?>
    <!-- End Repeater --> 
    <?php } ?>
  <?php } ?>
</div>
<!-- end catalog -->

这是类别ACF字段设置的截图,希望对您有所帮助:

在您的代码中,您将名称为 project-name 的自定义字段保存在一个变量中:

$projectDescription = get_sub_field('project-name');

也许你应该把这个子字段的元键改成“project-description”,如果你也有一个项目的名称,这可能会造成混淆。

要在 php 中输出一个值,您可以使用 <?php echo $variableName; ?>

您正在访问名为 portfolio-projects-repeater 的转发器中的值。所以您的字段在其中,需要像您一样使用一段时间才能访问。

您的屏幕截图显示了一个不在转发器字段内的文本字段。

所以如果你想得到那个字段的值,你只需要使用get_field()。这不需要在一段时间内使用 get_sub_field.

调用

因此您的代码应如下所示:

    $variableName = get_field('category-name');

保存到变量后,您可以简单地输出它:

<div class="grid-item-catalog catalog-project-Holder <?php echo $variableName; ?>"> 

如果我理解正确,您有一个 自定义 post 类型“portfolio”,并且您想获取 ACF 字段值 类别 post 属于。这些类别属于 默认 Wordpress 类别(即不是自定义分类法)。这与 post 而不是中继器中的值有关。

如果是这种情况,您可以获得类别的 ACF 字段 (category-name) 的值,如下所示:

$category_name = get_field('category-name', $category_id);

这意味着我们需要在循环中获取 post 的类别 ID,您可以使用 get_the_category 执行此操作(假设它是默认类别,对于自定义分类,您需要 get_the_terms).另请注意,post 可以有多个类别,因此 get_the_categories returns 数组:

$category_classes = "";
// 1. GET THE CATEGORIES FOR THE CURRENT POST IN THE LOOP
$categories = get_the_category( get_the_ID()); 
if ( $categories ) {
    // 2. A POST CAN HAVE MULTIPLE CATEGORIES SO LOOP THROUGH THEM TO LOOK UP THE ACF FIELD
    foreach ( $categories as $category ) {
        // 3. ADD ALL ACF VALUE FOR THIS CATEGORY TO OUR $category_classes VARIABLE, SEPARATED BY SPACES
        $category_classes .= " ".get_field('category-name', $category->term_id);
    }        
}

现在 $category_classes 将有一个 space 分隔的类别 ACF 值列表,您可以直接在 class 属性中使用,即

<div class="grid-item-catalog catalog-project-Holder <?php echo $category_classes; ?>"> 

将其放入您的代码中,它应该是这样的:

<!-- catalog -->
<div class="grid-catalog catalog-portfolio-wrraper">  
<?php if ( $query->have_posts() ) {  while ( $query->have_posts() ) { $query->the_post(); ?>

    <?php
    /* YOUR CODE TO GET THE POST CATEGORIES AND LOOK UP THE ACF FIELD VALUE */
    $category_classes = "";
    // 1. GET THE CATEGORIES FOR THE CURRENT POST IN THE LOOP
    $categories = get_the_category( get_the_ID()); 
    if ( $categories ) {
        // 2. A POST CAN HAVE MULTIPLE CATEGORIES SO WE NEED TO LOOP THROUGH THEM ALL
        foreach ( $categories as $category ) {
            // 3. ADD ALL ACF VALUE FOR THIS CATEGORY TO OUR $category_classes VARIABLE, SEPARATED BY SPACES
            $category_classes .= " ".get_field('category-name', $category->term_id);
        }        
    }
    ?>

  <!-- Repeater -->
  <?php if( have_rows('portfolio-projects-repeater') ){ ?>
    <?php while( have_rows('portfolio-projects-repeater') ) { the_row(); 
      // vars
      $projectDescription = get_sub_field('project-name');
      $projectImage = get_sub_field('project-image');
      $projectLinkText = get_sub_field('project-link-text');
      $projectLink = get_sub_field('project-link');                                                       
      ?> 

         <?php 4. OUTPUT THE CLASSES FOR THE DIV ?>
         <div class="grid-item-catalog catalog-project-Holder <?php echo $category_classes; ?>"> 

         <!--   the rest of your code here   -->

          </div>
      <?php } ?>
    <?php } ?>
    <!-- End Repeater --> 
    <?php } ?>
  <?php } ?>

此代码未经测试,但它至少应该为您指明正确的方向:)

Yaaaayyy...我成功了,代码如下:

         <?php
            // load all 'category' terms for the post
            $terms = get_the_terms( get_the_ID(), 'category');
            // we will use the first term to load ACF data from
            if( !empty($terms) ) {
                $term = array_pop($terms);
                $custom_field = get_field('category-name', $term );
            }
          ?>  

无论如何,我要感谢您的所有回复,对此我深表感谢。

在主题中创建一个模板,然后使用下面的代码,只需更改 slug (banner_image)

<?php
    //Get the Post ID
    $id = get_the_ID();
?>

    <img src="<?php echo get_field('banner_image', $id); ?>" />
    <div class="col-md-4 text-center"><?php echo get_field( "banner_heading", $id); ?></div>