PHP /创世纪:创世纪循环中的高级自定义字段
问题描述:
我正在尝试创建一个页面来提取自定义发布类型(团队)和相应的高级自定义字段,并将它们显示在模板团队页面上。我在Genesis中通过合并一段为WordPress开发的代码(非创世),在tutorial之后合并。PHP /创世纪:创世纪循环中的高级自定义字段
我取得了一些进展,但我被卡在高级自定义字段。例如,<?php the_title(); ?>
实际上调用PAGE标题,而不是自定义帖子的标题。而其他领域(职位,电话等)不起作用 - 他们根本没有被调用。我确信这是我将代码与Genesis合并的一个问题。
<?php
/**
* This file adds the city team template to any Genesis 2.0+ Theme.
*
* @author Jim Thornton
* @package InboundFound
* @subpackage Customizations
*/
/*
Template Name: Team
*/
?>
<?php
remove_action('genesis_loop', 'genesis_do_loop');
add_action('genesis_loop', 'your_custom_loop');
function your_custom_loop() {
// Get 'team' posts
$team_posts = get_posts(array(
'post_type' => 'team',
'posts_per_page' => -1, // Unlimited posts
'orderby' => 'title', // Order alphabetically by name
));
if ($team_posts):
?>
<section class="row profiles">
<div class="intro">
<h2>Meet The Team</h2>
<p class="lead"></p>
</div>
<?php
foreach ($team_posts as $post):
setup_postdata($post);
// Resize and CDNize thumbnails using Automattic Photon service
$thumb_src = null;
if (has_post_thumbnail($post->ID)) {
$src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'team-thumb');
$thumb_src = $src[0];
}
?>
<article class="col-sm-6 profile">
<div class="profile-header">
<?php if ($thumb_src): ?>
<img src="<?php echo $thumb_src; ?>" alt="<?php the_title(); ?>, <?php the_field('team_position'); ?>" class="img-circle">
<?php endif; ?>
</div>
<div class="profile-content">
<h3><?php the_title(); ?></h3>
<p class="lead position"><?php the_field('team_position'); ?></p>
<?php the_content(); ?>
</div>
<div class="profile-footer">
<a href="tel:<?php the_field('team_phone'); ?>"><i class="icon-mobile-phone"></i></a>
<a href="mailto:<?php echo antispambot(get_field('team_email')); ?>"><i class="icon-envelope"></i></a>
<?php if ($twitter = get_field('team_twitter')): ?>
<a href="<?php echo $twitter; ?>"><i class="icon-twitter"></i></a>
<?php endif; ?>
<?php if ($linkedin = get_field('team_linkedin')): ?>
<a href="<?php echo $linkedin; ?>"><i class="icon-linkedin"></i></a>
<?php endif; ?>
</div>
</article><!-- /.profile -->
<?php endforeach; ?>
</section><!-- /.row -->
<?php endif;
}
genesis();
答
“您必须传递对全局$ post变量的引用,否则像the_title()这样的函数将无法正常工作。”
https://codex.wordpress.org/Function_Reference/setup_postdata#Parameters