【问题标题】:ACF relationship fields - get_field values from other post typeACF 关系字段 - 来自其他帖子类型的 get_field 值
【发布时间】:2015-02-08 18:29:22
【问题描述】:

在我的帖子页面(常规帖子类型)中,我设置了一个 ACF 关系字段。在这里面我可以选择公司名称,它们都在 directory_listings 的帖子类型下。

现在,我在目录列表页面上有以下代码,因此仅使用 get_field 不起作用,因为这些值不在此页面上,而是在 POST 类型的其他位置。

所以不确定如何获取信息。

在 DIRECTORY_LISTINGS 帖子类型下的其中一个页面上的代码:

$posts = get_field('related_articles');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts 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>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

示例图,因为我不擅长通过文本进行解释。

目前我已经在公司编辑页面(directory_listing)上设置了关系字段。它在执行以下操作时起作用: 1) 与此商家列表相关的帖子 -> 选择一个帖子 -> 发布 -> 现在在商家列表页面上显示列表。此处示例:http://bit.ly/1vwydDl(页面底部)

2) 我想从 POST 编辑页面中选择一个将出现帖子的公司。我可以通过 ACF 将字段放在那里没问题,但让它实际显示我无法弄清楚的结果。

【问题讨论】:

  • 每个帖子都有一个ID,所以当你在foreach中调用get_field时,你需要传递$post->ID。我想这就是你的意思,对吧?
  • 好吧,get_field 现在什么也得不到,因为在 'post' 类型而不是 'directory_listings' 帖子类型上找到了 'related_directories' 字段。所以 var_dump($posts) 目前没有显示任何内容/NULL,这是预期的。它需要进入不同的帖子类型,抓取信息并将其带回 directory_listings 帖子类型以输出信息。
  • 是的,所以在您获取帖子的文件中,您调用了关系字段。然后你遍历你的 $posts 变量。 &lt;?php foreach( $posts as $post): $some_field = get_field('field_name', $post-&gt;ID); ?&gt; 这样的东西应该可以工作。

标签: wordpress advanced-custom-fields


【解决方案1】:

背景信息:

get_field() 有三个参数:

  1. $field_name:要检索的字段名称。例如“page_content”(必填)
  2. $post_id输入值的特定帖子 ID。 默认为当前帖子 ID(非必需)。这也可以是选项/分类法/用户/等
  3. $format_value

如果您只关心获取特定帖子(您知道其中的 ID),则关键将是第二个参数 ($post_id)。 ACF 并没有什么神奇之处。很简单:meta_value(即帖子绑定到的目录)保存到每个帖子(附加到该帖子的$post_id)。

但是,在您的情况下,我们不知道我们想要获取的帖子的 ID。

解决方案:

如果我们用一个简单的句子解释你想做什么,那句话看起来像:

directory_listings(自定义帖子类型)页面上显示/获取帖子,该页面有一个指向该页面的meta_value

显然,您不能使用get_field(),因为您的问题与“获取字段”无关。相反,您需要“找到具有特定领域的帖子”。 ACF 有great documentation on this

幸运的是,WordPress 附带了一个很棒的类 WP_Query,以及一个类似的很棒的函数 get_posts()。所以回顾我们上面的句子并将其翻译成一个函数,我们想要:get_posts() 其中meta_key 有一个value 的当前$post_id

或者,更具体地说,在您的 directory_listings 页面上,您将有以下查询:

$related_articles = get_posts(array(
    'post_type' => 'post',
    'meta_query' => array(
        array(
            'key' => 'related_articles', // name of custom field
            'value' => '"' . get_the_ID() . '"',
            'compare' => 'LIKE'
        )
    )
));

if( $related_articles ): 
    foreach( $related_articles as $article ): 

    // Do something to display the articles. Each article is a WP_Post object.
    // Example:

    echo $article->post_title;  // The post title
    echo $article->post_excerpt;  // The excerpt
    echo get_the_post_thumbnail( $article->ID );  // The thumbnail

    endforeach;
endif;

【讨论】:

  • 好问题,好答案。如果所有 SO 问题和答案都是这样的。
  • 我已经把它放在我的页面(single.php)中。从 admin 的 POSTS 页面(Design Locker)中选择相关公司并发布。现在,在该公司页面上,出现了 Design Locker 标题。不是帖子标题。我把 the_title;我想可能是因为这个页面已经在一个循环中。我尝试将内容更改为新的 WP_Query,但它只是返回了 10 个最近的帖子,忽略了 args。
  • 您不能使用the_title() 访问$related_articles 的标题,除非您使用setup_postdata()(查看我为get_posts() 发布的codex 链接)。由于您已经在循环中,您最好使用echo $article-&gt;post_title
  • 就是这样!完美配合,谢谢!现在我必须先做 $article->,标签会改变吗?我试过 $article->the_excerpt;但没有结果。与尝试获取缩略图相同。
  • 如果你查看return value of get_posts(),你会看到它返回一个WP_Post object $article->post_excerpt 访问摘录。缩略图将是echo get_the_post_thumbnail( $article-&gt;ID )。如果我的回答解决了问题,请关闭此问题。
【解决方案2】:

如果它是您在该查询中查找的自定义字段,那么您可以这样做:

<?php
$posts = get_field('related_articles');

if( $posts ): ?>
    <ul>
    <?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
        <?php setup_postdata($post); ?>
        <li>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
            <?php the_field('your_custom_field',$post); ?>
        </li>
    <?php endforeach; ?>
    </ul>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>

【讨论】:

    【解决方案3】:

    如果我理解正确,这与我遇到的问题相同。 就我而言,我更喜欢使用简码。 将 [acf field="person"] 与字段作为关系使用仅给了我此人的 ID,但我无法访问他们的字段。 另一方面,使用[acf field="last_name", post_id=[acf field="person"],这将是一个理想的解决方案不起作用,因为 wordpress 解析器不允许嵌套短代码。

    这就是我想出这个非常小的 php 解决方案的原因:

    function import_cf_from_cpt( $atts ) {
        // import custom fields from other custom post types that are connected via a relationship to the calling custom post type
        // shortcode: [import <field from calling CPT of type relationship> <field name from field from other CPT>]
        // ex [import person last_name] --> Doe
    
        $postID = do_shortcode("[acf field={$atts[0]}]");
        // we use the first attribute to get the ID of the object
        $postField = get_field($atts[1], $postID);
        // next, using the ID we look for the field of the second attribute.
    return $postField;
    }
    add_shortcode( 'import', 'import_cf_from_cpt' );
    

    将它放在functions.php文件中允许使用短代码

    例如[import person last_name],或任何其他组合以从其他帖子导入字段值。

    【讨论】:

      猜你喜欢
      • 2016-03-07
      • 2020-06-17
      • 2019-11-30
      • 2023-03-12
      • 1970-01-01
      • 2019-08-23
      • 2016-11-04
      • 1970-01-01
      • 2017-02-20
      相关资源
      最近更新 更多