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

时间:2020-07-05 16:32:46

标签: wordpress advanced-custom-fields

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

我有一个名为“ portfolio”的自定义帖子类型,它有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字段设置的屏幕截图,希望对您有所帮助:

setup of the field

4 个答案:

答案 0 :(得分:0)

在代码中,您将名称为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; ?>"> 

答案 1 :(得分:0)

如果我理解正确,则您有一个自定义帖子类型“投资组合” ,并且您希望获取该帖子所属的类别 ACF字段值。这些类别位于默认的Wordpress类别中(即不是自定义分类法)。这与帖子有关,而不与中继器中的值有关。

在这种情况下,您可以为以下类别获取ACF字段的值(category-name):

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

这意味着我们需要在循环中获取帖子的类别ID,您可以使用get_the_category(假设这是默认类别,对于自定义分类法则需要get_the_terms)。另请注意,帖子可以具有多个类别,因此get_the_categories返回一个数组:

$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将有一个空格分隔的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 } ?>

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

答案 2 :(得分:0)

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 );
            }
          ?>  

无论如何,我要感谢您的所有答复,对此我感到非常感谢。

答案 3 :(得分:0)

在主题中创建一个模板,然后使用下面的代码,只需更改 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>
相关问题