• 博客园logo
  • 会员
  • 周边
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
翩翩
业精于勤,荒于嬉,行成于思,毁于随
博客园    首页    新随笔    联系   管理    订阅  订阅
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'
);

  
 
 
 
posted on 2013-07-18 18:07  翩翩  阅读(5705)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3