如何在 wordpress 中设置文章模板?创建一个名为 "template-name.php" 的新文件,其中 "template-name" 是您想要的模板名称。添加代码 "Template Name: My Custom Template",其中 "My Custom Template" 是您想要的模板名称。自定义模板内容,添加您想要显示的代码。编辑文章,在 "属性" 部分将模板分配给文章。
步骤 1:创建文章模板文件
步骤 2:添加模板代码
- 在您创建的文件中,添加以下代码:
<?php /* Template Name: My Custom Template */ ?>登录后复制
替换 "My Custom Template" 为您想要的模板名称。
步骤 3:自定义模板
步骤 4:将模板分配给文章
示例
要创建一个名为 "Blog 模板" 的模板,请遵循以下步骤:
- 创建一个名为 "template-blog.php" 的文件。
- 添加以下代码:
<?php /* Template Name: Blog Template */ ?>登录后复制
- 在 和 之间添加以下代码以显示文章列表:
<?php $PAged = ( get_qUEry_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1; $args = array( 'post_type' => 'post', 'paged' => $paged, ); $query = new wp_Query( $args ); while ( $query->have_posts() ) : $query->the_post(); get_template_part( 'template-parts/content', get_post_format() ); endwhile; wp_reset_posTData(); ?>登录后复制