wordpress 学习笔记 (二)

1.主题文件构成

存放目录:wp-content/themes

必要文件:style.css 和 index.php

主题信息:存放在style文件头部

主题信息
Theme Name: 主题的名称
Theme URI: 主题的网址
Author: 作者的名称
Author URI:作者的网址
Description: 主题的简介
Version: 1.0 版本信息

主题默认能够识别的文件名

名称 模板名称 详细说明
screenshot.png 缩略图 显示在后台主题列表页的封面
taxonomy.php 自定义分类法 通用自定义分类法的显示模板
taxonomy-XXX.php 指定分类法
author.php 作者
date.php 日历
attachment.php 附件
image.php 图片
archive.php 归档
404.php 404页面
category.php 分类目录
category-XXX.php 指定分类
archive-XXX.php 指定归档
search.php 搜索结果
tag.php 标签
single-XXX.php 指定内容页
home.php 首页
front-page.php 绝对首页
comments.php 评论
sidebar.php 侧边栏
single.php 文章内容页
singular.php 页面文章合一
page.php 页面
header.php 公用头部
footer.php 公用底部

2. wp默认查询/主查询(wp自动的)

<?php

print($wp_query);

?>

print :php查询语法

wp_query: wp的默认查询语法

3.分类目录归档页查询

[ query object ] 存储分类目录的原始/本身的(详细)信息

[ posts ] 分类目录当前页(分页)下的所有文章信息

[ post ] 分类目录文章下的第一篇文章

[ is ] 开头的函数说明:

[is_xxx] => 1,如果后面箭头=> 1,则表示xx对应类型的网页

例子: [is_archive] => 1 归档类页面 [is_catgory] => 1 分类目录的页面

4.标签归档页查询

[is_tag] => 标签归档页面

同分类目录归档页查询相同

5.作者归档页查询

[is_author] => 作者归档页 同分类目录归档页查询相同

6.日期归档页查询

[is_date] => 日期归档页

日期归档页不提供 [query_object]的属性

7.文章详情页的查询

[ queried_object ] 文章详情信息

[is_single] => 文章详情页

[is_singllar] => 详情页

[posts] / [post] / [queried_object]:

获取的都是文章详情(因为访问的是一篇文章,所以这些信息都是文章)

8.页面详情页的查询

[is_page] => 页面详情页

[posts] [post] 同文章详情页

9.附件详情页的查询

[queried_object] 附件的详细信息

[is_attachment] 附件的详情页

[post_mime_type] 附件类型:MIME

[posts] [post] 同文章详情页

10.首页的查询

默认:

[is_home] => 当前页是首页

阅读设置:为默认设置的时候

查询出最新的文章

自定义:

[is_home] => 1 ,[is_page] => 1

阅读设置:为自定义页面的时候

查询出来是设置的页面

11.搜索页的查询

[is_serch] => 当前页是搜索页

没有[query_object]的字段

12. 错误页【404】页面的查询

[is_404] => 当前页是404页

没有[query_object]的字段

[posts] [post] 里面都为空

13. 默认查询的总结

默认查询:

$wp_query -> posts -> post

被查询对象:(wp_query查询出来的)

get_queried_object()

get_queried_object_id()

14. PHP原生语法的默认查询结果展示方法

<?php foreach($posts as $pt); ?>
    <p>文章编号:<?php echo $pt ->ID; ?></p>
    <p>文章编号:<?php echo $pt ->post_title; ?></p>
    <p>文章编号:<?php echo $pt ->post_content; ?></p>
    <p>文章编号:<?php echo $pt ->post_excerpt; ?></p>
?>

<?php endforeach; ?>

15.wp循环结构语法

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

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

      <!PHP代码 >

    <?php endwhile; ?>

    <?php endif; ?>

have_posts()

解析:WordPress的have_posts() 默认是一个全局函数。

have_posts函数被调用时实际上是调用全局变量$wp_query->have_posts()成员函数,来简单检查一个全局数组(array)变量$posts的一个循环计数器,以确认是否还有post,如果有返回true(1),如果没有返回false(0)。

the_post()

解析:the_post()函数则调用$wp_query->the_post()成员函数前移循环计数器,并且创建一个全局变量$post(不是$posts),把当前的post的所有信息都填进这个$post变量中,以备接下来使用。

the_xxx 可以直接输出, get_the_xxx 不能直接输出

WordPress的函数中,the开头的基本都是直接输出内容,get开头是获取内容。

两种输出方式:

① 纯PHP输出文章

while(have_posts()):the_post();
       echo '<a href="'.get_the_permalink().'">'.get_the_title().'</a><br/>';
       $i++;
endwhile; 

② PHP与HTML混合输出文章

<?php while(have_posts()):the_post(); ?>
       <a href="<?php the_permalink();?>"><?php the_title();?></a><br/>';
<?php endwhile;?> 

循环中常用的函数

直接输出 获取值 作用 备注
the_title() get_the_title() 文章标题
the_excerpt() get_the_excerpt() 文章摘要
the_ID() get_the_ID() 文章ID
the_content() get_the_content() 文章内容/正文 只能在循环中使用
the_permalink() get_ the_permalink() 文章链接
the_category() get_the_category() 文章分类
the_comments_munber() get_comments_munber() 获取评论数量
the_author() get_the_author() 文章作者 只能在循环中使用
edit_post_link() get_edit_post_link() 编辑链接
the_tags() get_the_tags() 文章标签
the_time() get_the_time() 文章时间
the_date() get_the_date() 文章日期
the_modified_author () get_the_modified_author() 修改作者
the_modified_time() get_the_modified_time() 修改时间
the_modified_date() get_the_modified_date() 修改日期
next_image_link() 当前文章附件中的下一幅图片生成链接
next_post_link() get_next_post_link()
next_posts_link() get_ next_posts_link()
previous_post_link() previous_post_link()
previous_posts_link() get_previous_posts_link()

16.获取文章所属的分类目录信息(the_category)

wp模板标签the_category用于在文章页或归档页的文章列表中输出当前文章所属分类, 即使有多个分类也会一并输出。

the_category( string $separator = '', string $parents = '', int $post_id = false )

1).函数参数

$separator

字符串值,默认为空

默认以无序列表输出分类链接,当文章指定了多个分类时,提供一个字符用于分隔这些分类链接。

$parents

字符串值,默认为空

指定父分类的显示方式,可选值如下:

空值:不输出父分类;

multiple:父分类和子分类都是单独的链接,但处于同一个li元素中;

single:不输出父分类链接,但父分类的名称会出现在子分类链接锚文本中;

$post_id

整数型,默认值:false

文章的ID,默认使用当前文章ID。

2).函数使用示例

用 > 分隔分类链接,并且输出父分类链接,非常适合用于制作面包屑导航:

<?php the_category( ' > ', 'multiple' ); ?>

17.文章所属分类目录信息:the_category 和 get_the_category_list

the_category : 可以直接输出

get_the_category_list: 不能直接输出,参数用法两者相同

18. 获取文章分类目录纯数据:get_the_category

wp模板标签get_the_category用于获取当前分类信息:

包含 分类ID分类名称分类别名分类描述父分类ID分类下文章数量 等。

get_the_category( int $id = false )

函数参数: $id

整数型,默认值:当前文章的ID

指定文章的ID,将返回该文章所属分类的信息。

函数使用示例

<?php
	$categorys = get_the_category();
	foreach ($categorys as $category) {
		echo $category->name;
	}
?>

19. 获取文章所属的标签信息(tags)

wp模板标签the_tags用于在文章页输出标签链接

函数参数

$before

字符串值,默认值:null

在标签链接 显示的文本。

$sep

字符串值,默认值:,

在每个标签链接 之间 显示的文本。

$after

字符串值,默认为空

在标签链接 显示的文本。

the_tags 和 get_the_tag_list 区别:

前者可以直接输出,后者不能直接输出,两者用法和参数基本相同

get_the_tags: 输出文章所属标签的纯数据

 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:青梅博客 » wordpress 学习笔记 (二)

分享到: 更多 (0)
avatar

评论 抢沙发

  • QQ号
  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
切换注册

登录

忘记密码 ?

切换登录

注册

我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活