wordpress 主題製作-自定義文章分類頁並帶翻頁功能
阿新 • • 發佈:2019-01-22
有時候你需要自己製作一個文章分類顯示頁面,可能還需要翻頁功能,這時候就要用到wordpress的一個外掛叫:WP-PageNavi,官網地址:https://wordpress.org/plugins/wp-pagenavi/ ,wp的後臺裡面可以搜尋:WP-PageNavi
安裝好外掛後,開始製作步驟:
1、新建一個文章列表頁起名page-articlelist,引入基本結構的必要檔案
<?php /* Template Name: XXX文章列表頁 */ ?>
<?php get_header(); ?> //共用頭部檔案
<?php get_sidebar() ?> //共用側邊欄檔案
<?php get_footer(); ?> //共用尾部檔案
2、在頭部加入資料庫查詢程式碼
<?php
/** 升序還是降序,DESC表示降序,ASC表示升序 */
$order = 'DESC';
/** 每頁顯示多少篇文章 */
$posts_per_page = 3;
/**
* 只顯示或不顯示某些目錄下的文章,目錄ID用逗號分隔,排除目錄前面加-
* 例如排除目錄29和30下的文章, $cat = '-29,-30';
* 只顯示目錄29和30下的文章, $cat = '29, 30';
*/
$cat = '1';
/** 獲取該頁面的標題和內容 */
global $post;
$post_title = $post->post_title;
$post_content = apply_filters('the_content', $post->post_content);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
/** 用WP_Query獲取posts */
$post_list = new WP_Query(
"posts_per_page=" . $posts_per_page .
"&order=" . $order .
"&cat=" . $cat .
"&paged=" . $paged
);
$total_posts = $post_list->found_posts;
?>
3、在文章列表顯示處,插入程式碼
<!--中部開始-->
<div class="wrap">
<ul class="am-avg-sm-3 col3">
<!--開啟主迴圈查詢-->
<?php if ( $post_list->have_posts() ) : while ( $post_list->have_posts() ) : $post_list->the_post(); ?>
<li>
<!--呼叫縮圖開始-->
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(); ?>
<?php else: ?>
<img src="<?php bloginfo('template_url'); ?>/images/activities-pic2.jpg" width="370" height="171" alt="">
<?php endif; ?>
<!--呼叫縮圖結束-->
<h4 class="am-text-center"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
</li>
<?php endwhile; endif; ?>
<!--關閉主迴圈查詢-->
</ul>
<?php if ( function_exists('wp_pagenavi') ) wp_pagenavi( array('query' => $post_list) ); ?>
</div>
<!--中部結束-->
4、wp後臺裡的頁面中新建頁面
在後臺的頁面欄目裡新建頁面,起個名字叫XX文章列表頁,右側模板檔案選擇剛剛製作的php檔案,設定連結地址,釋出後就可以了