高级自定义字段Post对象图像字段不在自定义Post类型Single.php中显示

时间:2017-08-15 作者:user125852

我想知道是否有人能帮我。我有一个自定义的post类型,称为associations。我使用高级自定义字段将一些自定义字段与post类型关联,其中2个是post对象字段。这允许我显示来自其他帖子类型的帖子内容。我想在单个关联中显示此信息。php,我遵循了ACF网站上的文档,但我遇到了一个问题,即post对象available\\u products中的图像未显示此字段标记为“product\\u icon”,post对象字段如下(coverage\\u highlights,available\\u products)请参阅下面的代码:

    <?php

$post_objects = get_field(\'coverage_highlights\');

if( $post_objects ): ?>

  <div class="association-products-side-bar">

    <h3 class="text-center">Coverage Highlights</h3>


    <ul>
      <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
          <li>


            <a href="<?php the_permalink(); ?>">
              <?php the_title(); ?>
            </a>


          </li>
          <?php endforeach; ?>
    </ul>
  </div>
  <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;

/*
*  Loop through post objects (assuming this is a multi-select field) ( don\'t setup postdata )
*  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
*/

$post_objects = get_field(\'available_products\');

if( $post_objects ): ?>
      <div class="association-products-side-bar">
        <h3 class="text-center">Available Products</h3>
        <ul>
          <?php foreach( $post_objects as $post_object): ?>
            <li>



    <?php

                        $image = get_field(\'product_icon\');

                        if( !empty($image) ): ?>

                                        <img src="<?php echo $image[\'url\']; ?>" alt="<?php echo $image[\'alt\']; ?>" />

                                        <?php endif; ?>





                              <?php endforeach; ?>


              <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>







            </li>
            <?php endforeach; ?>
        </ul>
      </div>
      <?php endif;

?>

1 个回复
SO网友:Milo

如果您没有将帖子ID传递给get_field, 它默认使用全局$post. 您需要传递每个available_products 职位:

$image = get_field( \'product_icon\', $post_object->ID );

结束

相关推荐

高级自定义字段Post对象图像字段不在自定义Post类型Single.php中显示 - 小码农CODE - 行之有效找到问题解决它

高级自定义字段Post对象图像字段不在自定义Post类型Single.php中显示

时间:2017-08-15 作者:user125852

我想知道是否有人能帮我。我有一个自定义的post类型,称为associations。我使用高级自定义字段将一些自定义字段与post类型关联,其中2个是post对象字段。这允许我显示来自其他帖子类型的帖子内容。我想在单个关联中显示此信息。php,我遵循了ACF网站上的文档,但我遇到了一个问题,即post对象available\\u products中的图像未显示此字段标记为“product\\u icon”,post对象字段如下(coverage\\u highlights,available\\u products)请参阅下面的代码:

    <?php

$post_objects = get_field(\'coverage_highlights\');

if( $post_objects ): ?>

  <div class="association-products-side-bar">

    <h3 class="text-center">Coverage Highlights</h3>


    <ul>
      <?php foreach( $post_objects as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
          <li>


            <a href="<?php the_permalink(); ?>">
              <?php the_title(); ?>
            </a>


          </li>
          <?php endforeach; ?>
    </ul>
  </div>
  <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
    <?php endif;

/*
*  Loop through post objects (assuming this is a multi-select field) ( don\'t setup postdata )
*  Using this method, the $post object is never changed so all functions need a seccond parameter of the post ID in question.
*/

$post_objects = get_field(\'available_products\');

if( $post_objects ): ?>
      <div class="association-products-side-bar">
        <h3 class="text-center">Available Products</h3>
        <ul>
          <?php foreach( $post_objects as $post_object): ?>
            <li>



    <?php

                        $image = get_field(\'product_icon\');

                        if( !empty($image) ): ?>

                                        <img src="<?php echo $image[\'url\']; ?>" alt="<?php echo $image[\'alt\']; ?>" />

                                        <?php endif; ?>





                              <?php endforeach; ?>


              <a href="<?php echo get_permalink($post_object->ID); ?>"><?php echo get_the_title($post_object->ID); ?></a>







            </li>
            <?php endforeach; ?>
        </ul>
      </div>
      <?php endif;

?>

1 个回复
SO网友:Milo

如果您没有将帖子ID传递给get_field, 它默认使用全局$post. 您需要传递每个available_products 职位:

$image = get_field( \'product_icon\', $post_object->ID );

相关推荐