我有一个自定义的“俱乐部”的帖子类型和“俱乐部的活动”的自定义帖子类型。我如何才能在我的单一活动页面(不是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; ?>发布于 2015-08-20 22:39:30
您需要为$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; ?>有几点:
setup_postdata($post);之后调用wp_reset_postdata();函数,否则页面的其他部分将无法工作(因为您将覆盖主页面循环)<li>标记内,然后像往常一样使用the_permalink();输出指向页面的链接(为了保持清晰,我已经在您的代码之外执行了此操作)。如果你还是卡住了就喊我一声。
https://stackoverflow.com/questions/31960537
复制相似问题