在WordPress中,使用Advanced Custom Fields(ACF)插件可以创建自定义字段,这些字段可以用于各种内容类型,包括自定义分类模板。如果在自定义分类模板上未显示ACF自定义字段,可能是由于以下几个原因:
ACF(Advanced Custom Fields)是一个WordPress插件,允许开发者为帖子、页面、分类法等添加额外的字段。这些字段可以是文本、数字、日期、文件上传等多种类型。
taxonomy-{taxonomy}.php
)中包含了调用ACF字段的代码。以下是一个简单的示例,展示如何在自定义分类模板中显示ACF字段:
<?php get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<?php if ( have_posts() ) : ?>
<header class="page-header">
<?php
the_archive_title( '<h1 class="page-title">', '</h1>' );
the_archive_description( '<div class="taxonomy-description">', '</div>' );
?>
</header><!-- .page-header -->
<?php
// Start the Loop.
while ( have_posts() ) :
the_post();
// Include the Post-Format-specific template for the content.
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
the_posts_pagination(
array(
'prev_text' => __( 'Previous page', 'twentytwenty' ),
'next_text' => __( 'Next page', 'twentytwenty' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentytwenty' ) . ' </span>',
)
);
else :
// If no content, include the "No posts found" template.
get_template_part( 'content', 'none' );
endif;
?>
</main><!-- #main -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<!-- 在分类模板中显示ACF字段 -->
<?php if( function_exists('get_field') ): ?>
<?php $custom_field = get_field('your_custom_field_name'); ?>
<?php if($custom_field): ?>
<p><?php echo esc_html($custom_field); ?></p>
<?php endif; ?>
<?php endif; ?>
ACF自定义字段广泛应用于需要为内容添加额外信息的场景,如:
如果按照上述步骤操作后仍然无法显示ACF字段,建议检查WordPress错误日志或联系ACF支持获取进一步帮助。
领取专属 10元无门槛券
手把手带您无忧上云