WordPress如何自定义文章详情页模板
WordPress允许用户自定义文章详情页的模板。要自定义文章详情页的模板,首先需要在主题文件夹中创建一个名为single.php
的文件。如果主题文件夹中已经有一个名为single.php
的文件,则可以直接在该文件中进行自定义。
为了确保模板能够正常工作,建议在开始自定义之前先备份原有的single.php
文件。然后,可以通过以下步骤来自定义文章详情页的模板:
- 打开
single.php
文件,然后将其中的内容替换为以下代码:
/*
Template Name: Single Post
*/
get_header();
<div id="primary" class="content-area">
<main id="main" class="site-main">
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', get_post_type() );
the_post_navigation();
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
</main><!-- #main -->
</div><!-- #primary -->
get_sidebar();
get_footer();
- 在
template-parts
文件夹中创建一个名为content-single.php
的文件。 - 将以下代码添加到
content-single.php
文件中:
<article id="post-the_ID(); " post_class(); >
<header class="entry-header">
the_title( '<h1 class="entry-title">', '</h1>' );
</header><!-- .entry-header -->
if ( has_post_thumbnail() ) :
<div class="post-thumbnail">
the_post_thumbnail();
</div><!-- .post-thumbnail -->
endif;
接下来,可以使用以下代码来显示文章内容:
<div class="entry-content">
the_content();
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'your-theme' ),
'after' => '</div>',
) );
</div><!-- .entry-content -->
最后,可以使用以下代码来显示文章元信息(例如作者、发布日期和分类):
<footer class="entry-footer">
your_theme_entry_footer();
</footer><!-- .entry-footer -->
完成上述步骤后,就可以保存文件并查看自定义的文章详情页模板。如果需要进行进一步的自定义,可以继续修改content-single.php
文件,并根据自己的需求添加或修改代码。
如果您想要添加更多的内容到文章详情页的模板中,可以继续修改content-single.php
文件。例如,可以添加文章的分类和标签,以便用户更容易地找到相关文章:
<footer class="entry-footer">
your_theme_entry_footer();
</footer><!-- .entry-footer -->
// Get the categories and tags for the post.
$categories = get_the_category();
$tags = get_the_tags();
if ( $categories || $tags ) :
<div class="entry-meta">
if ( $categories ) :
<span class="cat-links">
foreach ( $categories as $category ) {
echo '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '">' . esc_html( $category->name ) . '</a> ';
}
</span><!-- .cat-links -->
endif;
if ( $tags ) :
<span class="tag-links">
foreach ( $tags as $tag ) {
echo '<a href="' . esc_url( get_tag_link( $tag->term_id ) ) . '">' . esc_html( $tag->name ) . '</a> ';
}
</span><!-- .tag-links -->
endif;
</div><!-- .entry-meta -->
endif;
可以根据自己的需求,添加更多的内容或修改上述代码,以实现更多的自定义。例如,可以添加文章的作者信息,或者添加文章的相关文章等。
总之,自定义文章详情页的模板需要结合主题的特定代码和功能,并结合您的需求进行调整。

-
WordPress插件:All in One WP Security & Firewall 怎么样(附:AIOWPS拦截垃圾评论设置教程) 2023-01-26 21:51:51
-
在 PHP 中如何通过一行代码就交换两个变量的值 2023-01-26 00:18:31
-
WordPress主题:Pin Premium (WordPress仿Pinterest样式瀑布流主题) 2023-01-25 23:36:43
-
免费WordPress主题:Maupassant(WordPress中文博客主题) 2023-01-25 23:31:29
-
WordPress响应式图片(Responsive Image)功能关闭:取消WordPress图片宽带和高度参数 2023-01-24 10:49:32
-
wordpress报错:修复MySQL 1064错误问题 错误原因和解决方法 2023-01-24 10:42:04
-
WordPress 实现静态化搜索结果页面的方法 2023-01-24 00:41:44
-
WP Rocket – 强大的WordPress缓存加速优化插件(附:WP Rocket插件设置过程与七折优惠码) 2023-01-24 00:41:44
-
get_permalink、get_the_permalink和the_permalink有啥区别? 2023-01-24 00:41:08
-
为什么选用WordPress搭建独立站? 2023-01-23 20:25:21