index.php文件算是一个最普遍使用的模板页面了,如果你的主题没有home.php、且后台设置首页显示最新文章,那么index.php文件就是首页模板了,如果你的主题没有文章也模板(single.php)、没有单页面模板(page.php)、没有分类页模板(category.php)、没有标签页(tag.php)……没有404页面(404.php)等的,都将会使用index.php文件代替。
如果你还是主题开发的小白,可以练习做一个简单地练习。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title><?php bloginfo('name'); ?></title>
<?php wp_head(); ?>
</head>
<body>
<?php if (have_posts()):
while (have_posts()): the_post();
the_title('<h3>','</h3>');
the_content();
endwhile;
endif;
?>
<?php wp_footer(); ?>
</body>
</html>
bloginfo()函数的说明可以详见:https://blog.csdn.net/lsmxx/article/details/104191763
wp_head()函数和wp_footer()函数的使用参见:https://blog.csdn.net/lsmxx/article/details/104191950
循环的基本使用方法如下。
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// 显示文章内容
endwhile;
endif;
?>
上面的代码首先判断是否有循环,如果有,则逐个显示循环中的内容。
- have_posts() 函数检查当前页面是否有文章。
- 只要括号中的条件逻辑为真,
while
循环就会继一直执行下去。
可以在循环中显示什么
您可以在循环中使用模板标签显示各种文章内容、文章自定义字段,下面是一些常用的模板标签:
next_post_link()
:显示在当前文章之后 发布的文章的链接previous_post_link()
– 显示在当前文章之前发布的文章的链接the_category()
:显示当前文章所在的分类the_author()
:显示文章作者the_content()
:显示文章正文内容the_excerpt()
:显示文章摘要the_ID()
:显示文章或页面 IDthe_meta()
:显示文章或页面的自定义字段the_shortlink()
:显示文章的短链接the_tags()
:显示文章所包含的标签the_title()
:显示文章标题the_time()
:显示文章发布时间
显示文章的时候,我们可以使用条件标签控制文章是否在满足某种条件的情况下显示。例如:
is_home()
–如果当前页面是主页,则返回trueis_admin()
–如果在管理后台返回true,否则返回falseis_single()
–如果页面当前仅显示单个文章,则返回trueis_page()
–如果页面当前仅显示单个页面,则返回trueis_page_template()
–可用于确定页面是否使用特定模板,例如:is_page_template('about-page.php')
is_category()
–如果页面或文章具有指定的类别,则返回true,例如:is_category('news')
is_tag()
–如果页面或文章具有指定的标签,则返回trueis_author()
–如果在作者的存档页面内,则返回trueis_search()
–如果当前页面是搜索结果页面,则返回trueis_404()
–如果当前页面不存在,则返回truehas_excerpt()
–如果文章或页面有摘要,则返回true