若你想在 WordPress 主题的首页中调用 Advanced Custom Fields(ACF)设置的文章详情页字段,你可以按照以下步骤进行:
目录
- 编辑主题模板文件:进入你的 WordPress 主题文件夹,找到用于显示首页内容的模板文件,通常是 index.php或类似的文件。
- 在模板文件中调用字段值:在模板文件中,你可以使用 ACF 提供的函数来调用字段值。使用 get_field()函数来获取字段的值。你需要传入字段的名称或标识符作为参数。例如,如果你的自定义字段名称为custom_field_name,你可以在模板文件中像这样调用:
<?php $custom_field_value = get_field('custom_field_name'); ?>
然后你可以根据需要使用 $custom_field_value 变量来显示字段的值。
如果你希望在首页显示多个文章的字段,你需要在循环中重复这个过程。通常,WordPress 主题的首页内容是通过循环来显示的,你可以在循环内调用 get_field() 函数来获取每篇文章的字段值。
注意事项:确保在调用字段之前检查字段是否存在,以避免出现错误。你可以使用 have_rows() 函数来检查是否存在某个字段,并在使用 get_field() 函数之前进行检查。
实际案例:
编辑首页模板 index-tmp.php,该页面设置为首页的模板页面 Template Name: index Template 。
<div class="......">
    
    <?php query_posts('cat=22&ignore_sticky_posts=1&showposts=3'); ?>
    <?php while (have_posts()) : the_post(); ?>
    <div class="......">
    ......
                    <div class="wp-block-column is-layout-flow wp-block-column-is-layout-flow">
                        <p><strong><?php the_title(); ?></strong></p>
                        ......
                        <?php echo $exhibition_hall = get_field('exhibition_hall'); ?><br/>
                        <?php echo $exhibition_date = get_field('exhibition_date'); ?><br/>
                        <?php echo $position_number = get_field('position_number'); ?><br/>
                        </p>
                    </div>
                </div>
    ......
    </div>
    <?php endwhile;?>
    <?php wp_reset_query(); ?>  
</div>


 
