ACF关系字段-其他帖子类型的get_field值 [英] ACF relationship fields - get_field values from other post type
问题描述
在我的POSTS页面(常规帖子类型)中,我设置了ACF关系字段。在其中,我可以选择公司名称,这些公司名称都在directory_listings的帖子类型下。
In my POSTS page (regular post type), I have setup a ACF relationship field. Inside this I can select company name which are all under the post type of directory_listings.
现在,我在目录列表页面上具有以下代码,因此只需使用get_field无法使用,因为这些值不在此页面上,而是在POST类型上的其他位置。
Now, I have the following code on the directory listings page, and therefore using simply get_field does not work because those values are not on this page, they are elsewhere on the POST type instead.
因此不确定如何获取信息。
So unsure how to grab the information.
其中一个页面上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; ?>
示例图,因为我不太喜欢通过文字解释。
Example diagram as I am not great with explaining via text.
目前我有而是在公司编辑页面(directory_listing)上设置关系字段。它在执行以下操作时会起作用:
1)与该企业列表相关的帖子->选择帖子->发布->现在在企业列表页面上显示列表。此处的示例: http://bit.ly/1vwydDl (页面底部)
Currently I have setup the relationship field on the company edit page (directory_listing) instead. It works when doing the following: 1) Related Posts to this business listing -> select a post -> publish - > now displays the list on the business listing page. Example here: http://bit.ly/1vwydDl (bottom of the page)
2)我想从POST编辑页面中选择要在其上显示帖子的公司。我可以毫无问题地通过ACF将该字段放到那里,但要使其真正显示我无法弄清楚的结果。
2) I'd like from the POST edit page select a business which posts will appear on. I can put the field there via ACF no problem but getting it to actually display the results I cannot figure out.
推荐答案
背景信息:
get_field()具有三个参数:
Background Info:
get_field() has three parameters:
-
$ field_name
:要检索的字段的名称。例如, page_content(必填) -
$ post_id
:在其中输入您的值的特定帖子ID。 默认为当前帖子ID(不是必需的)。这也可以是选项/分类法/用户/等 -
$ format_value
$field_name
: the name of the field to be retrieved. eg "page_content" (required)$post_id
: Specific post ID where your value was entered. Defaults to current post ID (not required). This can also be options / taxonomies / users / etc$format_value
如果您只关注抓取特定帖子(您知道ID),则该键将是第二个参数( $ post_id
)。 ACF真的没有什么神奇的。非常简单: meta_value
(即列出帖子所绑定的目录)保存到每个帖子(附加到该帖子的 $ post_id $) c $ c>)。
If you were only concerned with grabbing a specific post (of which you knew the ID), the key would be the second parameter ($post_id
). There's nothing really magical about ACF. Quite simply: the meta_value
(i.e. the directory listing the post is tied to) is saved to each post (attached to that post's $post_id
).
但是,在您的情况下,我们不知道我们要获取的帖子的ID。
However, in your case, we don't know the ID of the post(s) we want to get.
如果我们用一个简单的句子解释您想做什么,那么该句子将类似于:
If we explain what you want to do in a simple sentence, that sentence would look something like:
在
directory_listings
(自定义帖子类型)页面上显示/获取帖子,该页面的meta_value
指向该页面。
Show/get posts on a
directory_listings
(custom post type) page which have ameta_value
which points to that page.
很明显,您不能使用 get_field()
,因为您的问题与获取字段无关。而是,您需要查找具有特定字段的帖子。 ACF对此有出色的文档。
Obviously, you can't use get_field()
, because your problem has nothing to do with "getting a field." Rather, you need to "find the posts that have a specific field." ACF has great documentation on this.
幸运的是,WordPress随附了一个名为 WP_Query 的超棒类,以及一个名为 get_posts()。因此,回顾一下上面的句子并将其转换为函数,我们想要: get_posts()
其中 meta_key
具有当前 $ post_id
的值
。
Luckily for you, WordPress ships with an awesome class called a WP_Query, and a similarly awesome function called get_posts(). So looking back at our sentence above and translating it into a function, we want to: get_posts()
where the meta_key
has a value
of the current $post_id
.
或者,更具体地说,在您的 directory_listings
页面上,您将具有以下查询:
Or, more specifically, on your directory_listings
page, you'd have the following query:
$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;
这篇关于ACF关系字段-其他帖子类型的get_field值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!