A-A+

wordpress主题模版制作

2015年12月18日 WordPress 暂无评论 阅读 2,629 views 次

参考资料:

1.wordpress的WP_Query文档:http://codex.wordpress.org/Class_Reference/WP_Query

2.wordpress的所有模板标签:http://codex.wordpress.org/Template_Tags

 

1、基础样式文件style.css

/*

Theme Name: 主题名

Theme URI: http://www.imdupeng.cn/

Version: 1.0

Description: 主题描述

Author: dupeng

Author URI: http://www.imdupeng.cn

*/

 

2、必用标签

模版文件在</head>标签前添加wp_head();

在</body>之前添加wp_footer();

引入头部文件:get_header();

引入脚部文件:get_footer();

模版地址绝对路径:bloginfo('templete_url');

 

3、默认标签一般有两种形式:

the_tag();  //直接把标签内容打印到html

get_the_tag();    //把标签内容保存到一个变量中,以供稍后的使用

注意:the_permalink()对应的是get_permalink(),没有 'the';

 

4、常用标签

wordpress模版标签一般是指一篇文章的字段,比如一篇文章的标题,内容,作者,发布日期,评论数等。

the_title();

the_content();

the_author();

the_permalink();

the_ID();

the_category();

edit_post_link();

next_post_link('%link');

previous_post_link('%link');

 

 

5、自定义循环:

<?php

$args=array(

post_type'=>'page',//查找出所有页面(多个结果集,复数)

//'page_id'=>x//仅仅查询id号为x(x是数字,你自己系统中‘现有’的某个文章的id)的页面,只有一个结果,单数,如果page_id不指定,则返回所有page

);

// 实例化wp_query

$the_query = new WP_Query( $args );

// 开始循环

if ( $the_query->have_posts() ) {//如果找到了结果,便输出以下内容

echo '<ul>';

while ( $the_query->have_posts() ) {//再次判断是否有结果

$the_query->the_post();//不用问为什么,每次都要写这个;

echo '<li>' . get_the_title() . '</li>';//这里开始输出你想要的模板标签

}

echo '</ul>';

} else {

// 如果没有找到任何结果,就输出这个

}

wp_reset_postdata();//不用问为什么,每次都记得写就好

?>

 

6、默认循环

默认循环无法指定需要查询的参数。而wordpress会根据“链接地址参数”来进行数据的查询,所以我们才需要创建多个php文件,比如single.php,page.php,category.php之类的,每一种不同的链接结构,会触发默认的查询。

<!--?php if ( have_posts() ) : ?-->

<!--?php while ( have_posts() ) : the_post(); ?-->

 

<!--?php endwhile; ?-->

一些HTML

<!--?php else : ?-->

另一些HTML

<!--?php get_template_part( 'content', 'none' ); ?-->

<!--?php endif; ?-->

 

7、创建多个page页面模版

需要在模版顶部加入以下申明:

<?php

/*

*Template Name : New Template

*/

?>

或通过id直接定温page模版:page.$id.php

 

8、文章日志模版single.php

可以添加single-type.php指定特定的文章类型使用特定模版。

 

标签:

给我留言