hiflora

要实现的如下功能,在页面上添加一个文章目录:

   

步骤:

 
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\'
);

  
 
 
 

分类:

技术点:

相关文章: