首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP/Genesis : Genesis循环中的高级自定义字段

PHP/Genesis : Genesis循环中的高级自定义字段
EN

Stack Overflow用户
提问于 2015-07-11 03:48:18
回答 1查看 333关注 0票数 0

我正在尝试创建一个页面,该页面拉出一个自定义帖子类型(团队)和相应的高级自定义字段,并将它们显示在模板团队页面上。我在Genesis中按照这个tutorial合并了一段为Wordpress (非genesis)编写的代码。

我取得了一些进展,但我被高级自定义字段卡住了。例如,<?php the_title(); ?>实际上调用页面标题,而不是自定义帖子的标题。而其他字段(位置、电话等)则不起作用--它们根本没有被调用。我确信这是我如何将这段代码与Genesis合并的一个问题。

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

回答 1

Stack Overflow用户

发布于 2015-08-31 20:54:19

必须传递对全局$post变量的引用,否则像the_title()这样的函数将无法正常工作。

https://codex.wordpress.org/Function_Reference/setup_postdata#Parameters

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

https://stackoverflow.com/questions/31349113

复制
相关文章

相似问题

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