如何在WordPress自定义模板中展示realisations自定义文章类型内容
问题原因
你之前使用的代码逻辑是获取站点内所有公开自定义文章类型的归档页链接,没有针对realisations文章类型查询具体的文章内容,因此无法输出对应文章的详情。
完整可用的自定义页面模板代码
<?php /* Template Name: Réalisations */ ?> <?php materialis_get_header(); ?> <div class="top-page"> <div class="title"> <i class="fas fa-camera"></i> <h2>Réalisations</h2> </div> <div class="description-page"> <p>Le Lorem Ipsum est le faux texte standard de l'imprimerie depuis les années 1500, quand un imprimeur anonyme assembla ensemble des morceaux de texte pour réaliser un livre spécimen</p> </div> <div class="realisations-list"> <?php // 自定义查询:获取所有realisations类型的文章 $args = [ 'post_type' => 'realisations', 'posts_per_page' => -1, // -1表示获取全部,可自行修改为数字指定展示数量 'post_status' => 'publish', // 只查询已发布的文章 ]; $realisations_query = new WP_Query($args); if ($realisations_query->have_posts()) : while ($realisations_query->have_posts()) : $realisations_query->the_post(); ?> <div class="realisation-item"> <!-- 输出文章标题 --> <h3><?php the_title(); ?></h3> <!-- 输出特色图(如果有设置的话) --> <?php if (has_post_thumbnail()) : ?> <div class="realisation-thumbnail"> <?php the_post_thumbnail('medium'); ?> </div> <?php endif; ?> <!-- 输出文章正文内容 --> <div class="realisation-content"> <?php the_content(); ?> </div> </div> <?php endwhile; // 重置文章数据,避免影响页面后续其他查询逻辑 wp_reset_postdata(); else : ?> <p>暂无已发布的作品内容</p> <?php endif; ?> </div> </div> <?php get_footer(); ?>
补充说明
- 代码中已经内置了你注册自定义文章类型时支持的标题、编辑器内容、特色图的输出,你可以根据自己的样式需求调整HTML结构和CSS类名
- 如果后续需要输出自定义字段内容,可在循环中使用
get_post_meta(get_the_ID(), '字段名', true)调用对应字段值
内容的提问来源于stack exchange,提问作者Teva
相关产品推荐
相关产品推荐

