wha*_*ale 7 wordpress advanced-custom-fields
在我的POSTS页面(常规帖子类型)中,我设置了一个ACF关系字段.在这里我可以选择公司名称,这些名称都在directory_listings的post类型下.
现在,我在目录列表页面上有以下代码,因此使用简单的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; ?>
Run Code Online (Sandbox Code Playgroud)
示例图,因为我不太通过文本解释.

目前,我已在公司编辑页面(directory_listing)上设置了关系字段.它执行以下操作时:1)此商家列表的相关帖子 - >选择帖子 - >发布 - >现在显示商家列表页面上的列表.示例:http://bit.ly/1vwydDl(页面底部)
2)我想从POST编辑页面选择一个将发布帖子的商家.我可以通过ACF将该字段放在那里没有问题,但让它实际显示我无法弄清楚的结果.
rne*_*ius 12
get_field()有三个参数:
$field_name:要检索的字段的名称.例如"page_content"(必填)$post_id:输入值的特定帖子ID. 默认为当前帖子ID(不是必需的).这也可以是选项/分类/用户/等$format_value如果你只关心抓取一个特定的帖子(你知道ID),那么键就是第二个参数($post_id).ACF并没有什么神奇之处.很简单:(meta_value即列出帖子的目录)将保存到每个帖子(附加到该帖子$post_id).
但是,在您的情况下,我们不知道我们想要获得的帖子的ID.
如果我们用简单的句子解释你想要做什么,那句话看起来像:
在
directory_listings(自定义帖子类型)页面上显示/获取具有meta_value指向该页面的帖子.
显然,你不能使用get_field(),因为你的问题与"获得一个领域"无关.相反,您需要"找到具有特定字段的帖子".ACF有很好的文档.
幸运的是,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;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17028 次 |
| 最近记录: |