我正在尝试创建一个页面,该页面拉出一个自定义帖子类型(团队)和相应的高级自定义字段,并将它们显示在模板团队页面上。我在Genesis中按照这个tutorial合并了一段为Wordpress (非genesis)编写的代码。
我取得了一些进展,但我被高级自定义字段卡住了。例如,<?php the_title(); ?>实际上调用页面标题,而不是自定义帖子的标题。而其他字段(位置、电话等)则不起作用--它们根本没有被调用。我确信这是我如何将这段代码与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();发布于 2015-08-31 20:54:19
必须传递对全局$post变量的引用,否则像the_title()这样的函数将无法正常工作。
https://codex.wordpress.org/Function_Reference/setup_postdata#Parameters
https://stackoverflow.com/questions/31349113
复制相似问题