【发布时间】:2015-07-02 08:32:36
【问题描述】:
我在我的网站上使用高级自定义字段。
我创建了一个自定义字段“关系”以显示在我的所有产品类别页面上(我使用的是 woocommerce,这就是为什么我在我的 php 中使用product_cat_ 而不是category)。
使用基本文本字段时,我可以使用以下代码在我的类别页面上显示文本:
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
?>
<div><?php the_field('text', $post_id); ?></div>
<?php ?>
但是现在尝试在此类别页面中使用关系功能时,我没有从我选择的帖子中获得正确的标题和永久链接,并且我找不到如何修改我的代码...
这是我的代码,我的自定义字段名为mise_en_avant_produit,它返回一个帖子对象。
<?php
$term_id = get_queried_object()->term_id;
$post_id = 'product_cat_'.$term_id;
$posts = get_field('mise_en_avant_produit', $post_id);
if( $posts ):
?>
<?php foreach( $posts as $post): ?>
<?php setup_postdata($post); ?>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php ?>
我已经使用过这种代码,例如显示来自另一个页面的关系字段,但是在这里我找不到解决方案,
这是我在print_r时得到的结果
Array ( [0] => WP_Post Object ( [ID] => 42 [post_author] => 1 [post_date] => 2014-09-16 17:22:07 [post_date_gmt] => 2014-09-16 16:22:07 [post_content] => . [post_title] => Green tea [post_excerpt] => [post_status] => publish [comment_status] => open [ping_status] => closed [post_password] => [post_name] => green-tea [to_ping] => [pinged] => [post_modified] => 2014-09-25 08:36:41 [post_modified_gmt] => 2014-09-25 07:36:41 [post_content_filtered] =>
[post_parent] => 0 [guid] => http://localhost:8888/bemygift/?product=green-tea [menu_order] => 0 [post_type] => product [post_mime_type] => [comment_count] => 0 [filter] => raw ) )
谁能帮我解决这个问题?
非常感谢,
【问题讨论】:
-
你能澄清一下吗: 1 - 你说你没有得到正确的标题和永久链接。你的意思是你得到不正确的标题或没有标题? 2. 你能
print_r($posts)让我们看看它在检索什么 -
@Dre,感谢您的回复。我得到了一个标题,但不是我在自定义字段中选择的那个...
-
这是我在 print_r 时得到的结果
-
@Dre print_r 在我上面的消息中
-
我找到了解决方案,但是添加了 ...
标签: php advanced-custom-fields wordpress