2014-09-29 131 views
0

我在我的网站上使用高级自定义字段。 我创建了一个自定义字段“关系”,以显示在我的所有产品类别页面上(我正在使用woocommerce,这就是为什么我使用product_cat_而不是category在我的php中)。显示产品类别页面上的acf自定义字段的关系

当使用一个基本的文本字段,我abble显示我的分类页面上的文本,使用此代码:尝试使用这一类页面内的关系功能时

<?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)) 

有人可以帮我吗?

非常感谢,

+0

你能否澄清:1 - 你说你没有得到正确的标题和固定链接。你的意思是你得到不正确的标题或没有标题? 2.你可以'print_r($ posts)',这样我们就可以看到它是在检索 – Dre 2014-09-29 19:18:57

+0

@Dre,谢谢你的回复。我得到一个标题,但不是我在我的自定义字段中选择的一个... – mmdwc 2014-09-29 19:36:06

+0

这里是我在print_r – mmdwc 2014-09-29 19:37:16

回答

0

我有类似的问题,也许这对你有帮助。

<?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

相关问题