Wordpress-从自定义帖子类型获取自定义字段

时间:2018-08-03 08:30:47

标签: php wordpress

我是新手,正在创建一个主页,该循环中有4个块:

    <?php $query = new WP_Query( [
    'post_type'      => 'cases',
    'nopaging'       => false,
    'posts_per_page' => '4',
] ); ?>

<?php if ( $query->have_posts() ) : ?>
        <div class="container-fluid">
            <div class="roundedframe">
                <?php while ( $query->have_posts() ) : $query->the_post(); ?>
                    <div id="case-study-box" style="background-image: url('<?php the_post_thumbnail_url(); ?>');" class="col-lg-6 col-sm-12">
                        <a class="portfolio-box " href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                            <div class="portfolio-box-caption">
                                <div class="portfolio-box-caption-content">
                                    <div><img src="<?php echo $slideCentralImage['url']; ?>"></div>
                                        <div class="project-name">
                                        <h2><?php the_title(); ?></h2>
                                        </div>
                                        <div class="portfolio-excpert">
                                         <p><?php the_excerpt(); ?></p>
                                        </div>
                                </div>
                            </div>
                        </a>
                    </div>
                <?php endwhile; ?>
            </div>
        </div>
<?php endif; ?>

<?php wp_reset_postdata(); ?>

每个块是一个投资组合项目,其中包含许多使用“高级自定义字段”创建的自定义字段。 我需要的是像“标题”一样在悬停时显示它们。 实际上只有the_title和the_permalinkg在起作用,但是我不知道如何获得徽标和它们的摘录。

我该如何实现?

2 个答案:

答案 0 :(得分:2)

您可以使用<?php echo get_field('whateverField');?>,并且不要忘记更改返回值图像URL。

答案 1 :(得分:1)

您可以使用get_field('<field_name>')返回或使用the_field('<field_name>')打印值。您可以在此处了解更多信息-https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/

只需添加<div><img src="<?php the_field('slide_central_image'); ?>"></div>,其中slide_central_image是您的图像字段名称。不要忘记将“返回值”更改为“图片网址”。

enter image description here

相关问题