You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用ACF和WooCommerce实现作者页关联书籍及产品页作者预览?

解决方案:ACF + WooCommerce 实现作者独立页面与产品页作者预览

1. 创建「作者」自定义帖子类型

先搭建专门的作者内容管理入口,方便维护作者头像、简介等信息。在主题的functions.php中添加以下代码:

function create_author_cpt() {
    $args = array(
        'labels' => array(
            'name' => '作者',
            'singular_name' => '作者'
        ),
        'public' => true,
        'has_archive' => true,
        'rewrite' => array('slug' => 'authors'),
        'supports' => array('title', 'editor', 'thumbnail', 'custom-fields'),
        'show_in_rest' => true
    );
    register_post_type('author', $args);
}
add_action('init', 'create_author_cpt');

激活后后台会新增「作者」菜单,可直接添加每位作者的详情页面。

2. 用ACF建立产品与作者的关联字段

在ACF后台新建字段组,绑定到WooCommerce「产品」类型:

  • 字段类型选择「帖子对象」
  • 筛选帖子类型为「作者」
  • 允许多选(适配多作者书籍场景)
  • 字段键名设为book_author(后续代码将依赖此键名)

3. 在产品页显示作者简短预览

找到主题的产品模板文件(如single-product.php或content-single-product.php),在产品标题下方/描述上方插入以下代码:

<?php
// 获取关联作者数据
$authors = get_field('book_author');
if($authors):
    foreach($authors as $author):
        $author_name = get_the_title($author->ID);
        $author_excerpt = get_the_excerpt($author->ID);
        $author_avatar = get_the_post_thumbnail($author->ID, 'thumbnail');
        $author_link = get_permalink($author->ID);
?>
<div class="book-author-preview">
    <a href="<?php echo esc_url($author_link); ?>">
        <?php echo $author_avatar; ?>
        <h3><?php echo esc_html($author_name); ?></h3>
    </a>
    <p><?php echo esc_html($author_excerpt); ?></p>
</div>
<?php
    endforeach;
endif;
?>

可自行添加CSS样式美化预览模块。

4. 制作作者独立页面模板,展示关联书籍

在主题文件夹中新建single-author.php文件,作为作者页面专属模板:

<?php get_header(); ?>

<div class="author-page-container">
    <!-- 作者信息区 -->
    <div class="author-bio">
        <?php the_post_thumbnail('large'); ?>
        <h1><?php the_title(); ?></h1>
        <div class="author-description"><?php the_content(); ?></div>
    </div>

    <!-- 作者作品集 -->
    <div class="author-books">
        <h2>这位作者的作品</h2>
        <?php
        // 查询关联当前作者的所有产品
        $books_query = new WP_Query(array(
            'post_type' => 'product',
            'meta_query' => array(
                array(
                    'key' => 'book_author',
                    'value' => get_the_ID(),
                    'compare' => 'LIKE'
                )
            ),
            'posts_per_page' => -1
        ));

        if($books_query->have_posts()):
            echo '<ul class="books-list">';
            while($books_query->have_posts()): $books_query->the_post();
        ?>
                <li>
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail('medium'); ?>
                        <h3><?php the_title(); ?></h3>
                        <span class="price"><?php echo wc_get_price_html(get_the_ID()); ?></span>
                    </a>
                </li>
        <?php
            endwhile;
            echo '</ul>';
            wp_reset_postdata();
        else:
            echo '<p>暂无作品</p>';
        endif;
        ?>
    </div>
</div>

<?php get_footer(); ?>

此后每个作者页面会自动展示其所有关联书籍,点击书籍可直接跳转至产品页。

常见问题排查

  • 关联失效:检查ACF字段键名是否与代码中的book_author完全一致
  • 作者菜单未显示:确认自定义帖子类型代码已正确添加到functions.php
  • 产品页无预览:检查模板文件是否选对,代码是否插入到了正确位置

内容的提问来源于stack exchange,提问作者Christoph Mikard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 18:15:10