列表页处理

时间: 2023年 3月 31日 下午9:47

 

 

html转wordpress模板过程中,文章列表页怎么转换?

 

 

将 HTML 页面转换为 WordPress 模板的过程中,文章列表页可以通过以下步骤进行转换:

 

创建一个 WordPress 页面模板(page template),该模板将用于显示文章列表页。在 WordPress 主题文件夹中创建一个新的 PHP 文件,并将其命名为 page-articles.php。

 

在该文件中添加以下代码,以显示文章列表:

 

<?php

/*

Template Name: Articles Page

*/

get_header(); ?>

 

<section id=”primary” class=”content-area”>

<main id=”main” class=”site-main” role=”main”>

 

<?php

// The Query

$query_args = array(

‘post_type’ => ‘post’,

‘posts_per_page’ => 10,

);

$the_query = new WP_Query( $query_args );

 

// The Loop

if ( $the_query->have_posts() ) {

while ( $the_query->have_posts() ) {

$the_query->the_post();

get_template_part( ‘template-parts/content’, get_post_format() );

}

 

// Pagination

the_posts_pagination();

 

} else {

// No posts found

get_template_part( ‘template-parts/content’, ‘none’ );

}

 

/* Restore original Post Data */

wp_reset_postdata();

?>

 

</main><!– #main –>

</section><!– #primary –>

 

<?php get_footer(); ?>

 

 

 

这个模板使用了 WordPress 提供的 WP_Query 类来获取文章列表,并使用 get_template_part() 函数来加载文章的模板部分。

 

根据需要自定义文章列表的样式,可以通过添加自定义 CSS 或修改模板中的 HTML 代码来实现。

 

将该页面模板应用于 WordPress 中的文章列表页面,将该页面的页面模板设置为 “Articles Page” 即可。