首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WordPress高级自定义字段关系单页

问WordPress高级自定义字段关系单页
EN

Stack Overflow用户
提问于 2015-08-12 17:00:26
回答 1查看 166关注 0票数 0

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

我已经找了又找,但我所能找到的只有一个循环。

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

代码语言:javascript
复制
<?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; ?>
EN

回答 1

Stack Overflow用户

发布于 2015-08-20 22:39:30

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

代码语言:javascript
复制
<?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. 如果您想链接到俱乐部页面,您可以将我的代码移到<li>标记内,然后像往常一样使用the_permalink();输出指向页面的链接(为了保持清晰,我已经在您的代码之外执行了此操作)。

如果你还是卡住了就喊我一声。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31960537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档