【问题标题】:displaying a relationship acf custom field on product category page在产品类别页面上显示关系 acf 自定义字段
【发布时间】: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


【解决方案1】:

我也遇到过类似的问题,或许对你有帮助。

<?php while ( have_posts() ) : the_post(); ?>
                <?php 
                    $auth_name = get_field('first_last_name');
                    $auth_office = get_field('position_of_author');
                    $auth_photo = get_field('author_photo');
                ?>
                    <img class="alignleft" src="<?php echo $auth_photo['url']; ?>" alt="<?php echo $auth_photo['alt']; ?>" />
                     <p><?php echo $auth_name; ?></p>
                                <p><?php echo $auth_office ?></p>

                <?php $postid = get_the_ID(); ?> 
                <ul class="products">
                    <?php
                        $args = array(
                            'post_type' => 'product'
                            );
                        $loop = new WP_Query( $args );
                        if ( $loop->have_posts() ) {
                            while ( $loop->have_posts() ) : $loop->the_post(); ?>
                                <?php 
                                    $locations = get_field('custom_product_author');
                                    $to_obj = $locations[0];
                                    $to_id = $to_obj->ID;
                                    $to_id_str = (string)$to_id;
                                ?>
                                <?php if(  $postid == $to_id_str ): ?>
                                    <ul>
                                    <?php foreach( $locations as $location ): ?>
                                        <li>
                                            <?php wc_get_template_part( 'content', 'product' ); ?>
                                        </li>
                                    <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>   
                            <? endwhile;
                        } else {
                            echo __( 'No products found' );
                        }
                        wp_reset_postdata();
                    ?>
                </ul>
            <?php endwhile; // end of the loop. ?>

ACF | Querying relationship fields

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    • 2021-04-12
    • 2020-07-06
    • 2020-07-14
    • 2021-04-01
    • 1970-01-01
    • 2020-02-22
    相关资源
    最近更新 更多