【问题标题】:WordPress Advanced Custom Fields Relationship Single PageWordPress 高级自定义字段关系单页
【发布时间】:2015-08-20 22:39:30
【问题描述】:

我有“俱乐部”的自定义帖子类型和“俱乐部活动”的自定义帖子类型。如何在我的单个活动页面(而不是 while 循环)上获取俱乐部的名称?

我找了又找,但我只能找到一个循环。

以下是单个事件页面的代码。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <h1><?php the_title(); ?></h1>
    <section class="entry-content">     
    <ul>
        <li>Club Name: { get Club Name from Club post type relationship } </li>
        <li>Other detail: <?php the_field('blah_blah'); ?>
        <li>.....other details.....</li>
    </ul>
    <?php the_content(); ?>

    </section>
    <?php endwhile; endif; ?>

【问题讨论】:

  • 您的活动如何通过自定义字段与您的俱乐部相关联?
  • 通过高级自定义字段关系链接。所以当我去添加一个事件(帖子类型)时,我可以从俱乐部(帖子类型)中选择一个名称。

标签: php wordpress advanced-custom-fields


【解决方案1】:

您需要为 $club post 对象设置 postdata 才能访问它:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post();

    $club = get_field('club_field_name');
    if($club):
      $post = $club;
      setup_postdata($post);
      $club_name = get_the_title();
      wp_reset_postdata();
    endif; ?>

  <h1><?php the_title(); ?></h1>
  <section class="entry-content">
    <ul>
      <li>Club Name: <?php echo $club_name; ?> </li>
      <li>Other detail: <?php the_field('blah_blah'); ?>
      <li>.....other details.....</li>
    </ul>
    <?php the_content(); ?>
  </section>
<?php endwhile; endif; ?>

几点:

  1. 您必须记住在调用setup_postdata($post); 后调用wp_reset_postdata(); 函数,否则页面的其他位将无法工作(因为您正在覆盖主页面循环)

    李>
  2. 如果你想链接到俱乐部页面,你可以将我的一段代码移动到&lt;li&gt; 标签内,然后使用 the_permalink();像往常一样输出到页面的链接(我已经在您的代码之外完成了它以保持清晰)。

如果你仍然卡住,请大声告诉我。

【讨论】:

  • 对不起,我花了一段时间才看到这个,但我会尝试一下,让你知道它是怎么回事。谢谢。
猜你喜欢
  • 2018-06-07
  • 2017-08-28
  • 2012-08-10
  • 2015-01-21
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
相关资源
最近更新 更多