WordPress怎么在頁面上添加目錄


要實現的如下功能,在頁面上添加一個文章目錄:

   

步驟:

 
1)在wordpress中,在Posts----Categories中建立目錄,
2)
3) add new post,指定post所屬的category。
  
 
4)添加Categories至頁面左側邊欄,這一步在外觀-小工具里設置,把文章目錄添加到左側欄,有的主題沒有左側欄,那么需要修改代碼:
     獲取左側欄內容的代碼在當前主題的index.php頁面上
              

<?php get_header(); ?>   --------這里是頭部文件,獲取菜單等

<div id="container">         ---------這里包含的是主頁面內容

<div id="content" role="main">     

<?php get_template_part( 'loop', 'index' ); ?>  -------------獲取文章內容
</div><!-- #content -->
</div><!-- #container -->
<?php get_sidebar(); ?>    -------這句用來獲取你放在側邊欄的內容,如果你上一步把文章目錄放在了側邊欄里,這里就能獲取到

<?php get_footer(); ?>     ----------頁腳內容

      我們如果想調整文章目錄在整個頁面是居左還是居右,在style.css文件中,查找 container的樣式, 得到如下:

/*
LAYOUT: Two columns
DESCRIPTION: Two-column fixed layout with one sidebar right of content
*/

#container {
float: right;       ----------------這里寫明container是居右
margin: 0 -240px 0 0;
width: 100%;
}
#content {
margin: 0 280px 0 20px;
}
#primary,
#secondary {    
float: left;      --------------------------這里說明文章目錄所在的居左,如果需要改變,left/right互換
overflow: hidden;
width: 220px;
}
#secondary {
clear: left;      -------------------------這里說明文章目錄所在的居左,如果需要改變,left/right互換
}
#footer {
clear: both;
width: 100%;
}

5)再換個其他主題之后,效果如下,點擊相應的文章目錄,可以看到對應的文章:
 
   
 
6)如果要目錄第一次出現的時候,只顯示一級目錄:
     wp-includes-----category-template.php下,找到這個方法:
      

function wp_list_categories( $args = '' ) {
$defaults = array(
'show_option_all' => '', 'show_option_none' => __('No categories'),
'orderby' => 'name', 'order' => 'ASC',
'style' => 'list',
'show_count' => 0, 'hide_empty' => 1,
'use_desc_for_title' => 1, 'child_of' => 0,
'feed' => '', 'feed_type' => '',
'feed_image' => '', 'exclude' => '',
'exclude_tree' => '', 'current_category' => 0,
'hierarchical' => true, 'title_li' => __( 'Categories' ),
'echo' => 1, 'depth' => 0,   -----------------------------這里設置為1,可以默認顯示為1級目錄
'taxonomy' => 'category'
);

  
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM