Wordpress主题进阶

自定义内容模板层级

归档页

1
2
3
archive-$posttype.php
archive.php
index.php

详情页

1
2
3
4
5
$custom.php
single-$posttype-$slug.php
single-$posttype.php
singular.php
index.php

自定义分类项目归档页

1
2
3
4
5
taxonomy-$taxonomy-$term.php
taxonomy-$taxonomy.php
taxonomy.php
archive.php
index.php

自定义内容详情页默认查询

  • queried_object 当前内容信息
  • queried_object_id 当前内容id
  • posts 数组里面只有一个内容信息
  • post 当前内容信息
  • is_single = 1 内容详情页
  • is_singular = 1 详情页

自定义分类项目归档页默认查询

  • queried_object 分类项目信息
  • queried_object_id 分类项目id
  • posts 内容列表数组
  • post 当前内容信息
  • is_archive = 1 内容详情页
  • is_tax = 1 自定义的分类方式

自定义内容类型归档页

  • queried_object 自定义内容类型的信息(如:product)
  • queried_object_id 空 (因为自定义的内容类型是保存在wp_post_type的全局变量中)
  • posts 内容列表数组
  • post 当前内容信息
  • is_archive = 1 内容详情页
  • is_post_type_archive = 1 自定义内容类型归档页

文章添加自定义字段和自定义分类

调取分类方式数据

  • get_the_taxonomies
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
/**
 * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post.
 * @param array       $args {
 *           Optional. Arguments about how to format the list of taxonomies. Default empty array.
 *
 *     @type string $template      Template for displaying a taxonomy label and list of terms.
 *                                 Default is "Label: Terms."
 *     @type string $term_template Template for displaying a single term in the list. Default is the term name
 *                                 linked to its archive.
 * }
 * @return string[] List of taxonomies.
 */
function get_the_taxonomies( $post = 0, $args = array() );


//数据返回示例
Array(
	[category] => 分类目录:<a href="http://xxx/category/uncategorized">未分类</a>
    [post_copy] => 文章版权:<a href="http://xxx/post-copy/yuanchuang">原创</a>
);
  • the_category(参考主题核心笔记)
  • the_tags(参考主题核心笔记)
  • the_terms
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
 * @param int    $post_id  Post ID.
 * @param string $taxonomy Taxonomy name.
 * @param string $before   Optional. String to use before the terms. Default empty.
 * @param string $sep      Optional. String to use between the terms. Default ', '.
 * @param string $after    Optional. String to use after the terms. Default empty.
 * @return void|false Void on success, false on failure.
 */
function the_terms( $post_id, $taxonomy, $before = '', $sep = ', ', $after = '' ) {
	$term_list = get_the_term_list( $post_id, $taxonomy, $before, $sep, $after );

	if ( is_wp_error( $term_list ) ) {
		return false;
	}

	/**
	 * @param string $term_list List of terms to display.
	 * @param string $taxonomy  The taxonomy name.
	 * @param string $before    String to use before the terms.
	 * @param string $sep       String to use between the terms.
	 * @param string $after     String to use after the terms.
	 */
	echo apply_filters( 'the_terms', $term_list, $taxonomy, $before, $sep, $after );
}


//使用举例
the_terms(get_the_ID(), 'post_copy');
  • get_the_terms(纯数据,不带html标签)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/**
* WP_Term Object
*
* term_id 分类ID
* name 分类名称
* slug 分类别名
* description 分类描述
* taxonomy 分类项目的分类方式 post_tag
* ......
*/

function get_the_terms( $post, $taxonomy );

//使用举例
$terms = get_the_terms(get_the_ID(), 'post_copy');
forearch($terms as $term) {
    echo '分类项目ID:' . $term->term_id;
    echo '<br />分类项目名称:' . $term->name;
    echo '<br />分类项目别名:' . $term->slug;
    echo '<br />分类项目描述:' . $term->description;
    echo '<br />分类项目描述:' . term_description($term->term_id);
    echo '<br />分类项目分类方式:' . $term->taxonomy;
    echo '<br />分类项目下内容数量:' . $term->count;
    echo '<br />分类项目网址:' . get_term_link($term);
}

获取分类方式扩展数据

  • get_term_meta
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
/**
 * @param int    $term_id Term ID.
 * @param string $key     Optional. The meta key to retrieve. By default,
 *                        returns data for all keys. Default empty.
 * @param bool   $single  Optional. Whether to return a single value.
 *                        This parameter has no effect if `$key` is not specified.
 *                        Default false.
 * @return mixed An array of values if `$single` is false.
 *               The value of the meta field if `$single` is true.
 *               False for an invalid `$term_id` (non-numeric, zero, or negative value).
 *               An empty string if a valid but non-existing term ID is passed.
 */
function get_term_meta( $term_id, $key = '', $single = false ) {
	return get_metadata( 'term', $term_id, $key, $single );
}
  • 使用示例
1
2
3
4
5
6
7
8
9
$terms = get_the_terms(get_the_ID(), 'post_copy');
forearch($terms as $term) {
    echo '分类项目ID:' . $term->term_id;
    echo '<br />分类项目扩展字段seo_key:' . get_term_meta($term->term_id, 'seo_key', true);
    // 获取图片id
    $pic_id = get_term_meta($term->term_id, 'seo_pic', true);
    echo '<br />分类项目扩展的图片:' . wp_get_attachment_image($pic_id, 'thumbnail');
    
}

获取文章扩展数据

  • get_post_meta(参考主题核心笔记)
1
2
3
4
5
6
7
8
9
// 在while(have_posts())循环中使用
get_post_meta(get_the_ID(), 'seo_key', true);//返回单个数据
get_post_meta(get_the_ID(), 'seo_key', false);//返回数组

// 获取文章扩展的配图字段
$post_img_ids = get_post_meta(get_the_ID(), 'post_img', false);
foreach($post_img_ids as $img_id) {
    echo wp_get_attachment_image($img_id, 'thumbnail');
}

自定义内容类型数据查询

1
2
3
4
5
6
7
$qb = get_queried_object();
// 内容类型的标记名
$qb->label;
// 内容类型的名称
$qb->name;
// 内容类型的描述
$qb->description;

自定义分类项目归档页数据查询

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$term = get_queried_object();
// ID
$term->term_id;
// 别名
$term->slug;
// 名称
$term->name;
// 图像描述
$term->description;
// 图像描述
term_description($term->term_id);
// 获取扩展的数据
get_term_meta($term->term_id, 'key', true);

附件和分类方式相关数据的函数

附件相关

  • wp_get_attachment_image($img_id, $size = 'thumbnail')
1
2
// 返回结果示例
<img src="http://xxx/xxx.jpg" class="attachment-thumbnail" alt="" />
  • wp_get_attachment_image_src($img_id, $size = 'thumbnail')
1
2
3
4
5
6
7
// 返回结果示例
Array(
	[0] => http://xxx/xxx.jpg,//图片url
    [1] => 150,//width
    [2] => 150,//heigth
    [3] => 1
);
  • wp_get_attachment_url($img_id, $size = 'thumbnail')
1
2
// 返回结果示例
http://xxx/xxx.jpg
  • get_post_meta($img_id, '_wp_attachment_image_alt', true)
1
2
// 返回结果示例
图片的替代文本alt的信息

分类方式相关

  • 获取某个分类方式本身的数据对象 get_taxonomy()
1
2
3
4
5
6
// 获取分类方式信息
get_taxonomy('category');
// 获取标签信息
get_taxonomy('post_tag');
// 获取产品分类
get_taxonomy('product_cat');
  • 获取指定内容/内容类型关联的分类方式 get_object_taxonomies()
1
2
3
4
5
6
// 使用方式1,参数传内容类型的字符串
get_object_taxonomies('post');
get_object_taxonomies('product');

// 使用方式2,参数传内容的对象
get_object_taxonomies(get_post(1));
  • 判断某个分类方式是否存在
1
2
taxonomy_exists('product');//true
taxonomy_exists('demo');//false

获取附件相关信息的函数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// 根据附件ID获取附件网址
wp_get_attachment_url($attachment_id);
// 根据附件ID获取附件Caption(说明)
wp_get_attachment_caption($attachment_id);
// 根据附件ID获取附件路径
get_attached_file($attachment_id);
// 根据附件ID获取附件meta数据
wp_get_attachment_metadata($attachment_id);
// 根据附件ID判断附件是否为图片
wp_attachment_is_image($attachment_id);
// 根据附件ID获取图片html标签
wp_get_attachment_image($attachment_id);
// 根据附件ID获取图片url/width/height
wp_get_attachment_src($attachment_id);
// 根据附件ID获取图片的URL地址
wp_get_attachment_url($attachment_id);
// 根据附件ID获取图片的alt信息
get_post_meta($attachment_id, '_wp_attachment_image_alt', true);

自主查询

WP_Query完整示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
//获取特定分类目录下的文章
$query = new WP_Query([
    'post_type' => 'post',
    'category__in' => [12],
]);

// 循环输出文章信息
if ($query->have_posts()) while($query->have_posts()) {
    $query->the_post();
    echo "文章标题:" . the_title();
}

// 恢复现场
wp_reset_postdata();

WP_Query作者相关查询

  • author 根据用户ID查询
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'author' => 3,
]);
  • author_name 根据用户nicename查询
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'author_name' => 'hollis',
]);
  • author__in 根据多个用户ID查询
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'author__in' => [3,5],
]);
  • author__not_in 排除某些用户ID
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'author__not_in' => [1],
]);

WP_Query分类目录相关查询

  • cat 子级的分类目录也会查询出来
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'cat' => '10,11',//-10,-11前面加负号表示排除
]);
  • category__in
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'category__in' => [12,13],
]);
  • category_name 根据分类目录别名查询
1
2
3
4
5
6
7
8
9
$query = new WP_Query([
    'post_type' => 'post',
    'category_name ' => 'iphone,xiaomi',
]);

$query = new WP_Query([
    'post_type' => 'post',
    'category_name ' => 'iphone+xiaomi', // 查询多个分类目录共有的数据
]);
  • category__and
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'category__and' => '12,13', // 查询多个分类目录共有的数据,类似category_name中的+号
]);
  • category__not_in
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'category__not_in' => [14],
]);

WP_Query标签相关查询

  • tag 根据标签别名查询
1
2
3
4
5
6
7
8
9
$query = new WP_Query([
    'post_type' => 'post',
    'tag' => 'wordpress,php',
]);

$query = new WP_Query([
    'post_type' => 'post',
    'tag' => 'wordpress+php',// 查询多个标签共有的数据
]);
  • tag_id 根据标签id查询
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'tag_id' => '70', //如果填写多个标签,自动获取第一个标签的数据
]);
  • tag__in 根据标签的ID编号,从单个或多个标签中获取数据
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'tag__in' => [79,81],
]);
  • tag__and 根据标签的ID编号,获取多个标签中共有的数据
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'tag__and' => [79,81],
]);
  • tag__not_in 排除指定标签的数据
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'tag__not_in' => [80],
]);
  • tag_slug__and 根据标签的别名,获取多个标签中共有的数据
  • tag_slug__in 根据标签的别名,从单个或多个标签中获取数据

WP_Query分类方式相关查询

  • tax_query
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// 根据分类方式别名查询
$query = new WP_Query([
    'post_type' => 'post',
    'tax_query' => [
        [
            'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
            'field' => 'slug',   //告诉查询工具,根据什么字段查询数据
            'terms' => ['book'],  //告诉查询工具,上面指定的字段的值是什么
            'include_children' => true, //是否包含子分类。默认值:true
        ]
    ]
]);

// 根据分类方式ID编号查询
$query = new WP_Query([
    'post_type' => 'post',
    'tax_query' => [
        [
            'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
            'field' => 'term_id',   //告诉查询工具,根据什么字段查询数据
            'terms' => [20],  //告诉查询工具,上面指定的字段的值是什么
        ]
    ]
]);

// 根据分类方式别名查询,但不包含子分类
$query = new WP_Query([
    'post_type' => 'post',
    'tax_query' => [
        [
            'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
            'field' => 'slug',   //告诉查询工具,根据什么字段查询数据
            'terms' => ['book'],  //告诉查询工具,上面指定的字段的值是什么
            'include_children' => false, //是否包含子分类。默认值:true
        ]
    ]
]);

// 从多个分类项目中获取数据
$query = new WP_Query([
    'post_type' => 'post',
    'tax_query' => [
        [
            'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
            'field' => 'slug',   //告诉查询工具,根据什么字段查询数据
            'terms' => ['book', 'pancel'],  //告诉查询工具,上面指定的字段的值是什么
            'operator' => 'AND', //操作类型。默认值是IN。AND表示是交集,NOT IN表示排除
        ]
    ]
]);

// 从多个分类方式的分类项目中获取数据
$query = new WP_Query([
    'post_type' => 'post',
    'tax_query' => [
        'relation' => 'AND', //多个分类方式的关系。可用值为AND,OR
        [
            'taxonomy' => 'category', //告诉查询工具,需要查询哪个分类方式
            'field' => 'slug',   //告诉查询工具,根据什么字段查询数据
            'terms' => ['book', 'pancel'],  //告诉查询工具,上面指定的字段的值是什么
            'operator' => 'AND', //操作类型。默认值是IN。AND表示是交集,NOT IN表示排除
        ],
        [
            'taxonomy' => 'post_tag', //告诉查询工具,需要查询哪个分类方式
            'field' => 'slug',   //告诉查询工具,根据什么字段查询数据
            'terms' => ['test', 'default'],  //告诉查询工具,上面指定的字段的值是什么
            'operator' => 'AND', //操作类型。默认值是IN。AND表示是交集,NOT IN表示排除
        ]
    ]
]);

// 复杂查询,带嵌套。
$query = new WP_Query([
    'post_type' => 'post',
    'tax_query' => [
        'relation' => 'AND',
        [
            'relation' => 'AND',
            [
                'taxonomy' => 'category', 
                'field' => 'slug',
                'terms' => ['book', 'pancel'], 
            ],
            [
                'taxonomy' => 'post_tag',
                'field' => 'slug',
                'terms' => ['test', 'default'],
            ]
        ],
        [
            'taxonomy' => 'post_copy',
            'field' => 'slug',
            'terms' => ['test', 'default'],
        ]
    ]
]);

WP_Query搜索相关查询

  • s 会通过正文、摘要、标题等内容搜索内容
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    's' => '世界',
]);

WP_Query内容相关查询

  • post_type 查询的内容类型
    • post
    • page
    • revision
    • attachment
    • nav_menu_item
    • any
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
//查询单个内容类型
$query = new WP_Query([
    'post_type' => 'post',
]);

//查询多个内容类型
$query = new WP_Query([
    'post_type' => ['post', 'page', 'product'],
]);

// 查询附件内容
$query = new WP_Query([
    'post_type' => 'attachment',
    'post_status' => 'inherit', // 查询附件内容,需要指定内容状态的字段,才能查询出内容
]);

// any
$query = new WP_Query([
    'post_type' => 'any', //除了设置exclude from search = true的内容类型之外的所有类型
]);
  • p 根据内容ID编号查询,查询单个
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'p' => 19,
]);
  • name 根据内容别名slug查询,查询单个
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'name' => 'test-01',
]);
  • page_id 根据页面ID编号查询
1
2
3
$query = new WP_Query([
    'page_id' => 12
]);
  • pagename 根据页面别名查询
1
2
3
$query = new WP_Query([
    'pagename' => 'test-page-01'
]);
  • post_parent 根据父级id查询
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
$query = new WP_Query([
    'post_type' => 'page', //自定义内容类型如果设置了层级关系,也可以查询
    'post_parent' => 10, //查询指定的父级id的内容,只获取直接关系的数据,不会递归获取
]);

// 获取所有顶级页面,post_parent = 0
$query = new WP_Query([
    'post_type' => 'page', //自定义内容类型如果设置了层级关系,也可以查询
    'post_parent' => 0, 
]);
  • post_parent__in
1
2
3
4
$query = new WP_Query([
    'post_type' => 'page', //自定义内容类型如果设置了层级关系,也可以查询
    'post_parent__in' => [10, 11],//查询指定的父级id的内容,只获取直接关系的数据,不会递归获取
]);
  • post_parent__not_in
1
2
3
4
$query = new WP_Query([
    'post_type' => 'page', //自定义内容类型如果设置了层级关系,也可以查询
    'post_parent__not_in' => [10, 11],//排除指定的父级id的内容
]);
  • post__in 根据多个内容ID编号查询
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'post_in' => [10,11], // 如果填写空数组,会查询指定类型的最新内容
]);
  • post__not_in 排除指定内容ID编号
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'post__not_in' => [10,11], // 如果填写空数组,会查询指定类型的最新内容
]);
  • post_name__in 根据内容别名查询
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'post_name__in' => ['test-01', 'test-02'], 
]);
  • post_status 根据内容状态查询
    • publish 已发布
    • pending 待审核
    • draft 草稿
    • auto-draft 自动草稿
    • future 定时发布
    • private 私密
    • inherit 附件的特有的状态
    • trash 回收站
    • any 除了auto-draft和trash之外的所有状态
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
$query = new WP_Query([
    'post_type' => 'post',
    'post_status' => 'draft' //获取草稿状态的内容
]);

$query = new WP_Query([
    'post_type' => 'post',
    'post_status' => 'publish' //获取已发布的内容
]);

// 同时查询多个状态
$query = new WP_Query([
    'post_type' => 'post',
    'post_status' => ['publish', 'future'] //获取已发布的和定时发布的内容
]);
  • comment_count 根据评论数量查询
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
$query = new WP_Query([
    'post_type' => 'post',
    'comment_count' => 5 //查询评论数量为5的内容
]);

// 范围查询 '=', '!=', '>', '>=', '<', '<=' 默认值是'='
$query = new WP_Query([
    'post_type' => 'post',
    'comment_count' => [
        'value' => 1,
        'compare' => '>=', //查询评论数量大于等于1的内容
    ]
]);
  • date_query 根据日期查询
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$query = new WP_Query([
    'post_type' => 'post',
    'date_query' => [
       [
           'year' => 2024,  //查询2024年发布的内容
           'month' => 9,    //查询9月份的内容
           'day' => 1,      //查询1号的内容
       ] 
    ],
]);

// 范围查询 如:查询2024年9月1日 - 2024年10月1日的内容
// after 表示xx日期之后  before 表示xx日期之前
// 即:after 2024年9月1日  before  2024年10月1日
// 生成sql语句为 > '2024-09-01 00:00:00' and < '2024-10-01 23:59:59'
$query = new WP_Query([
    'post_type' => 'post',
    'date_query' => [
       'after' => [
           [
               'year' => 2024,  //查询2024年发布的内容
               'month' => 9,    //查询9月份的内容
               'day' => 1,      //查询1号的内容
           ] 
       ],
       'before' => [
           [
               'year' => 2024,  //查询2024年发布的内容
               'month' => 10,    //查询10月份的内容
               'day' => 1,      //查询1号的内容
           ] 
       ]
    ],
]);

WP_Query密码相关查询

  • has_password
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'has_password' => true|false|null, //true获取设置了访问密码的内容;false获取没有设置访问密码的内容;null获取所有内容
]);
  • post_password
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post', //post、page、product
    'post_password' => '123456', //获取访问密码为123456的内容
]);

WP_Query自定义字段相关查询

  • meta_query 根据元数据(自定义字段)查询
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 曾经设置过指定自定义字段的内容。即:wp_post_meta里面有记录
$query = new WP_Query([
    'post_type' => 'post',
    'meta_query' => [
        [
            'key' => 'seo_keywords', //key指的是字段的名称
            'value' => '',           //value指的是字段的值
            'compare' => '=',        //compare指的是关系. '='、'!='等
            'type' => 'CHAR',            //type指的是字段数值的类型. 'NUMERIC', 'BINARY', 'CHAR', 'DATE', 'DATETIME', 'DECIMAL', 'SIGNED', 'TIME', 'UNSIGNED' 默认值 'CHAR'
        ]
    ],
]);

WP_Query的MIME类型相关查询

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$query = new WP_Query([
    'post_type' => 'attachment',
    'post_status' => 'inhirent', 
    'post_mime_type' => 'image/jpeg', // 附件类型string/array
]);

// 查询多种类型
$query = new WP_Query([
    'post_type' => 'attachment',
    'post_status' => 'inhirent', 
    'post_mime_type' => ['image/jpeg', 'image/png'], // 附件类型string/array
]);

WP_Query排序参数相关查询

  • order 排序方式

    • ASC 升序
    • DESC 降序
  • orderby 排序的字段

    • none 不排序
    • ID
    • author 根据作者ID编号排序
    • title
    • name 根据别名排序
    • type 根据内容类型排序
    • date 根据发布日期排序
    • modified 根据修改时间排序
    • parent 根据父级ID编号排序
    • rand 随机排序
    • comment_count 根据评论数量排序
    • relevance 根据相关性排序,一般配合s参数使用
    • menu_order 根据后台的页面属性中的“排序”进行排列
    • meta_value 根据自定义字段来排序,需要配合“meta_key”字段指定排序字段
    • meta_value_num 根据自定义字段(数值类型的字段)来排序
    • post__in 根据in中参数填写的顺序排序
    • post_name__in 根据in中参数填写的顺序排序
    • post_parent__in 根据in中参数填写的顺序排序
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'ASC',
    'orderby' => 'date'
]);

// 设置不排序
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'ASC',
    'orderby' => 'none'
]);

// 根据ID排序,倒序
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'DESC',
    'orderby' => 'ID'
]);

// 随机排序
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'DESC',
    'orderby' => 'rand'
]);

// 根据自定义字段排序,实际是根据post_meta表的数据排序
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'DESC',
    'orderby' => 'meta_value',
    'meta_key' => 'seo_keywords',
]);

// 根据自定义字段的数值排序,meta_key指定的字段需要设置为数字类型
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'DESC',
    'orderby' => 'meta_value_num',
    'meta_key' => 'seo_keywords',
]);

// post__in 排序
$query = new WP_Query([
    'post_type' => 'post',
    'order' => 'DESC',
    'orderby' => 'post__in',
    'post__in' => [10, 11, 12],//根据填写的顺序排序
]);

WP_Query分页参数相关查询

  • nopaging
1
2
3
4
5
// 不分页,获取所有数据
$query = new WP_Query([
    'post_type' => 'post',
    'nopaging' => true,
]);
  • posts_per_page 每页大小
1
2
3
4
5
// 每页最多包含两条记录
$query = new WP_Query([
    'post_type' => 'post',
    'posts_per_page' => 2,
]);
  • paged 页码
1
2
3
4
5
// 查询第二页
$query = new WP_Query([
    'post_type' => 'post',
    'paged' => 2,
]);
  • posts_per_archive_page (wp内部参数)
  • offset(wp内部参数)
  • page(wp内部参数)
  • ignore_sticky_posts 忽略置顶内容
1
2
3
4
$query = new WP_Query([
    'post_type' => 'post',
    'ignore_sticky_posts' => true,
]);

主题自定义功能

新版文档:developer.wordpress.org

老版文档:codex.wordpress.org

顶部图像功能

  • 开启顶部图像
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
function start_custom_header()
{
    // 开启顶部图像功能
    add_theme_support('custom-header');
}

add_action('after_setup_theme', 'start_custom_header');

// 顶部图像在数据库保存的位置
wp_options表option_name为theme_mods_{当前主题文件夹名称}的记录中。option_value保存是一个序列化的对象数据。
  • 显示顶部图像
1
<img src="<?php echo header_image(); ?>" />
  • 默认参数
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
$defaults = [
    # 设置默认图像,也就是没有上传时所使用的图像
    'default-image'        => '',
    # 采取随机方式显示默认图像(设置多个默认图像,然后随机使用)
    'random-default'       => false,
    # 上传图片时候,会显示一个裁剪框,用户可以拖动裁剪框,选择要显示的图片部分。上传图片后,会对上传的图片进行尺寸裁剪,设置裁剪后图片的宽度
    'width'                => 0,
    # 上传图片时候,会显示一个裁剪框,用户可以拖动裁剪框,选择要显示的图片部分。上传图片后,会对上传的图片进行尺寸裁剪,设置裁剪后图片的高度
    'height'               => 0,
    # 是否采用可变高度,即:裁剪框的高度可以拖动改变
    'flex-heigth'          => false,
    # 是否采用可变宽度,即:裁剪框的宽度可以拖动改变
    # 特别注意:可变宽度,最终生成的图片宽度都是1500px
    'flex-width'           => false,
    # 是否允许用户在后台裁剪上传的图片
    'uploads'              => true,
    # 是否使用顶部文本,使用的话则允许用户设置顶部文本的颜色
    'header-text'          => true,
    # 设置顶部文本的默认颜色
    'default-text-color'   => '',
    # 设置会被添加到主题中的wp_head钩子中的函数,在wp_head()函数执行时会调用此处的函数
    'wp-head-callback'     => '',
];

function start_custom_header()
{
    // 开启顶部图像功能
    add_theme_support('custom-header', $defaults);
}

add_action('after_setup_theme', 'start_custom_header');
  • default-image参数
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
// functions.php中设置
<?php
function start_custom_header()
{
	$params = [
        'default-image' => get_template_directory_uri() . '/images/default.jpg'
    ];
    // 开启顶部图像功能
    add_theme_support('custom-header', $params);
}

add_action('after_setup_theme', 'start_custom_header');
?>

// 前台调用,如果用户没有设置顶部图像,则显示默认图像
<?php echo header_image(); ?>
  • random-default参数
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// functions.php中设置
<?php
function start_custom_header()
{
	$params = [
        'random-default' => true,
    ];
    // 开启顶部图像功能
    add_theme_support('custom-header', $params);
    
    // 设置多张顶部图像,用来随机获取
    register_default_headers([
        'pic-01' => [
            'url' => get_template_directory_uri() . '/images/pic-01.jpg',
            'thumbnail_url' => get_template_directory_uri() . '/images/pic-01-thumbnail.jpg',
            'description' => '图片01',
        ],
        'pic-02' => [
            'url' => get_template_directory_uri() . '/images/pic-02.jpg',
            'thumbnail_url' => get_template_directory_uri() . '/images/pic-02-thumbnail.jpg',
            'description' => '图片02',
        ],
    ]);
}

add_action('after_setup_theme', 'start_custom_header');
?>

// 前台调用,随机显示默认图像
<?php echo header_image(); ?>
  • header-text
  • default-text-color
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// functions.php中设置

function start_custom_header()
{
	$params = [
        'header_text' => true, //false表示关闭顶部文本,用户无法设置顶部字体颜色
        'default-text-color' => '2c0747', // 设置默认颜色,不能包含#号
    ];
    // 开启顶部图像功能
    add_theme_support('custom-header', $params);
}

add_action('after_setup_theme', 'start_custom_header');
  • wp-head-callback
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
// functions.php中设置
<?php
function start_custom_header()
{
	$params = [
        'wp-head-callback' => function () {
            echo '<style type="text/css">.my-class {color:#}' . get_header_textcolor() . '}</style>';
        }
    ];
    // 开启顶部图像功能
    add_theme_support('custom-header', $params);
}

add_action('after_setup_theme', 'start_custom_header');
?>

// 前台调用my-class样式
<div class="my-class">文本</div>
  • 顶部图像相关函数
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
// 获取用户设置的顶部图像url网址
header_image();
get_header_images();

/* 获取顶部图像的所有相关信息
 * 返回stdClass Object
 * 字段url,thumbnail_url,width,height,attachment_id
 * 使用举例:<img src="<?=get_custom_header()->url ?>" width="<?=get_custom_header()->width ?>" height="<?=get_custom_header()->height ?>" />
 */
get_custom_header();

// 获取设置的顶部文本设置的颜色值,不包含#号
header_textcolor();
get_header_textcolor();

// 将多个图片设置成随机顶部图像
register_default_headers();

自定义背景图像

  • 开启背景图像
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
function start_custom_background()
{
    // 开启背景图像功能
    // 后台设置背景图像和颜色-背景颜色的功能
    add_theme_support('custom-background');
}

add_action('after_setup_theme', 'start_custom_background');

// 前端页面需要设置body_class()后,才会有效果
// <body class="<?php body_class() ?>"> </body>
  • 默认参数
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
# 默认的背景图片
'default-image'          => '',
# 图片是否平铺。repeat-x、repeat-y、repeat、no-repeat
'default-repeat'         => 'repeat',
# 图片水平方向的位置。left、center、right
'default-position-x'     => 'left',
# 图片是否跟随内容滚动,scroll滚动 | fixed固定
'default-attachment'     => 'scroll',
# 默认的背景颜色
'default-color'          => '',
# 挂载到wp_head钩子下的函数
'wp-head-callback'       => '_custom_background_cb',
  • 自定义背景图像相关函数
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// 获取主题定义设置项的值
get_theme_mod( $name, $default_value = false );

// 获取自定义背景图像url
background_image();
get_background_image();

// 获取自定义背景颜色的值(不包含#号)
background_color()
get_background_color();

// 获取自定义背景图像的平铺方式
get_theme_mod('background_repeat');

// 获取自定义背景图像水平方向的对齐方式
get_theme_mod('background_position_x');

// 获取自定义背景图像是否跟随内容滚动
get_theme_mod('background_attachment');

//从 global $_wp_theme_features 中获取指定的设置
get_theme_support('custom-background', 'xxxx');

// 结合get_theme_mod 和 get_theme_support。如果用户没有设置,则获取默认值
get_theme_mod('background_attachment', get_theme_support('custom-background', 'background_attachment'););

自定义菜单和小工具

  • 开启小工具
1
2
3
4
5
6
7
add_action('widgets_init', function () {
    // 注册小工具,主题自定义功能中就可以看到小工具
    register_sidebar();
});

// 前端调用
dynamic_sidebar();
  • 菜单相关函数
1
2
3
4
5
6
7
8
/**
 * Registers a navigation menu location for a theme.
 * @param string $location    Menu location identifier, like a slug.
 * @param string $description Menu location descriptive text.
 */
function register_nav_menu( $location, $description ) {
	register_nav_menus( array( $location => $description ) );
}
  • 注册导航菜单
1
2
3
add_action('after_setup_theme', function () {
    register_nav_menu('primary', '主要菜单');
});

Customize API

panel、section、control 、setting

panel 可设置的参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$panel_args = [
    # panel的优先级,用于排序,值越小越往前排
    'priority'          => 160,
    # 操作panel需要具备的权限。如果无权限,界面上看不到该panel
    'capability'        => 'edit_theme_options',
    # title的描述,会在界面中展示给用户
    'title'             => '',
    # panel的描述,会在界面中展示给用户
    'description'       => '',
    # panel激活时会调用的函数,填写函数的名称,如果函数的返回值为false,则不显示此panel
    'active_callback'   => '',
    # 【用默认值即可】panel包含了哪些section,填写section的id,一般无需填写
    'sections'          => '',
    # 【用默认值即可】主题必须首先支持什么功能,才让panel生效。
    # 比如'custom-header',主题必须开启顶部图像功能才能生效
    'theme_supports'    => '',
    # 【用默认值即可】panel的类型,使用默认值即可
    'type'              => 'default',
];

section 可设置的参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
$section_args = [
    # section的优先级,用于同一个panel下的section间的排序,值越小越靠前
    'priority'             => 160,
    'panel'                => '',
    # 操作此section,需要的权限
    'capability'           => 'edit_theme_options',
    # section的标题,会在界面中展示给用户
    'title'                => '',
    # section的说明,会在界面中展示给用户
    'description'          => '',
    # section激活前会调用的函数,如果函数的返回值为false的话,则不显示此section
    'active_callback'      => '',
    # 【用默认值即可】此section下包含哪些control
    'controls'             => [],
    # 【用默认值即可】section的类型,无需理解
    'type'                 => '',
    # 【用默认值即可】主题必须首先支持什么功能,才让section生效
    'theme_supports'       => '',
];

setting 可以设置的参数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
$setting_args = [
    # setting的保存类型,可以取的值为'option' 或  'theme_mod'
    # 'option' - 将数据当作独立条目保存到wp_option表中
    # 'theme_mod' - 将数据保存到wp_option表中的名称为theme_mods_{当前主题名称}'项目中
    'type'                 => 'theme_mod',
    # 保存setting数据需要的权限
    'capability'           => 'edit_theme_options',
    # setting(设置项)的默认值
    'default'              => '',
    # 可取的值,要么是refresh,要么是postMessage
    # refresh - 刷新查看预览效果
    # postMessage - 无需刷新即可查看预览效果,需要自定义js来实现
    'transport'            => 'refresh',
    # 设置数据处理函数,填写函数的名称
    # 在保存数据前,首先将数据先传递给此处的函数进行处理,然后才是保存
    # 需要接受原始数据,并返回处理后的数据
    'sanitize_callback'    => '',
    # 设置数据处理函数,填写函数的名称
    # 在获取数据后,首先将数据传递给此处的函数进行处理,然后才是使用
    # 需要接受原始数据,并返回处理后的数据
    # 使用举例:可以用来保存颜色数值,在显示的时候自动加上'#'号
    'sanitize_js_callback' => '',
    # 【用默认值即可】主题必须首先支持什么功能,才让setting生效
    'theme_supports' => '',
];

使用举例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function customize_new_items ($wp_customize) {
    //创建panel
    $wp_customize->add_panel('general_panel', [
        'title' => 'hollis test panel',
        'description' => 'this is holiis test panel',
    ]);
    
    //创建section
    $wp_customize->add_section('section_first', [
        'panel' => 'general_panel',
        'title' => 'section first',
        'description' => 'section first description',
    ]);
    
    //创建setting
    $wp_customize->add_setting('my_site_keywords', [
        'type' => 'theme_mod'
    ]);
    
    //创建control
    $wp_customize->add_control('control_first', [
        'section' => 'section_first',
        'settings' => 'my_site_keywords',
        'label' => 'control label',
    ]);
}

add_action('customize_register', 'customize_new_items');

获取数据

1
2
3
4
5
6
# setting 中设置type为theme_mod
# 应用场景:限定设置项目在当前主题中使用。如:主题个性化的设置,logo,背景等
get_theme_mod('setting_id');

# setting 中设置type为opiton
get_option('setting_id');

关键设置

  • active_callback 用于控制方式显示。true显示,false不显示
1
2
3
4
5
$wp_customize->add_panel([
    'title' => '',
    'description' => '',
    'active_callback' => 'is_front_page', //调用wp内置的is_front_page函数,判断是否显示该panel
]);
  • theme_supports 开启指定主题功能时才显示
1
2
3
4
5
6
7
8
9
$wp_customize->add_panel([
    'title' => '',
    'description' => '',
    'theme_supports' => 'custome-header', //当开启自定义顶部图像的时候才显示
]);


//需要开启指定主题功能
add_theme_support('custom-header');

control的类型

  • 普通表单类型的control(WP_Customize_Control)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
$control_args = [
    # control属于哪个section,填写section的id
    'section'           => '',
    # 填写和此control对应的setting的ID,作用是将contorl(界面)和setting(数据)建立关系
    'settings'          => '',
    # control的类型,可取的值有
    # text - 文本域类型的表单,如<input type="text">
    # checkbox - 复选框类型的表单,如<input type="checkbox">
    # textarea - 多行文本域类型的表单,如<textarea></textarea>
    # radio - 单选框类型的表单,如<input type="radio">男
    # select - 定义下拉列表,如<select><option></option></select>
    # dropdown-pages - 可以在站点的所有页面选择一个页面
    # email - 文本域类型的表单, 如<input type="text" >
    # url - 文本域类型的表单, 如<input type="text" >
    # number - 文本域类型的表单, 如<input type="text" >
    # hidden - 隐藏域类型的表单, 如<input type="hidden" >
    # date - 日期选择,可以选择日期
    'type'              => 'text',
    # 优先级,用于同一个section下的control间的排序,值越小越靠前
    'priority'          => 10,
    'label'             => '',
    'description'       => '',
    # control激活前会调用的函数,如果函数的返回值为false的话,则不显示此control
    'active_callback'   => '',
    # 当control的类型(type)是radio(单选框)或select(下拉选择框)时有效
    # 该参数的格式:如下
    # array('value' => 'label')
    # 其中value是真正保存的值,而label是给用户看的提示
    'choices' => [],
    # 当type不是checkbox|radio|select|textarea|dropdown-pages时有效
    # 作用是给input元素增加属性,例如
    # array('size' => '40', 'class' => 'my-class')
    'input_attrs' => [],
    # 【用默认值即可】无需设置,不用去理解
    'setting' => 'default',
];
  • 普通表单control综合应用
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function customize_control_args($wp_customize) {
    //创建panel略  
    //创建section略  
    //创建setting略 
    // 为隐藏域设置值
    $wp_customize->add_setting('setting_id_05', [
        'type' => 'theme_mod',
        'default' => 123456
    ]);
    
    //创建control
    //单选框
    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'control_id_01',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_01',
            'label' => '单行文本',
            'description' => 'type取值为text',
            'type' => 'text',
            'input_attrs' => [
                'size' => '20',
                'class' => 'my-class'
            ],//通过input_attrs参数增加属性
        ]
    ));
    
    //复选框
    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'control_id_02',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_02',
            'label' => '复选框',
            'description' => 'type取值为checkbox',
            'type' => 'checkbox',
        ]
    ));
    
    //多行文本
    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'control_id_03',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_03',
            'label' => '多行文本',
            'description' => 'type取值为textarea',
            'type' => 'textarea',
        ]
    ));
    
    //单选框
    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'control_id_04',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_04',
            'label' => '单选框',
            'description' => 'type取值为radio',
            'type' => 'radio',
            'choices' => [
                '1' => '男',
                '2' => '女',
            ],
        ]
    ));
    
    //隐藏域,value只能通过setting设置,看上面setting_id_05定义
    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'control_id_05',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_05',
            'label' => '隐藏域',
            'description' => 'type取值为hidden',
            'type' => 'hidden',
            'choices' => [
                '1' => '男',
                '2' => '女',
            ],
        ]
    ));
    
}

add_action('customize_register', 'customize_control_color_args');
  • 颜色选择类型的control(WP_Customize_Color_Control)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$color_control_args = [
    # control属于哪个section,填写section的id
    'section'           => '',
    # 填写和此control对应的setting的ID,作用是将contorl(界面)和setting(数据)建立关系
    'settings'          => '',
    # 优先级,用于同一个section下的control间的排序,值越小越靠前
    'priority'          => 10,
    'label'             => '',
    'description'       => '',
    # control激活前会调用的函数,如果函数的返回值为false的话,则不显示此control
    'active_callback'   => '',
];
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function customize_control_color_args($wp_customize) {
    //创建panel略  
    //创建section略  
    //创建setting略  
    
    //创建control
    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'control_id_01',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_01',
            'label' => '颜色设置',
            'description' => '请设置颜色',
        ]
    ));
}

add_action('customize_register', 'customize_control_color_args');
  • 图片上传类型的control(WP_Customize_Image_Control)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$image_control_args = [
    # control属于哪个section,填写section的id
    'section'           => '',
    # 填写和此control对应的setting的ID,作用是将contorl(界面)和setting(数据)建立关系
    'settings'          => '',
    # 优先级,用于同一个section下的control间的排序,值越小越靠前
    'priority'          => 10,
    'label'             => '',
    'description'       => '',
    # control激活前会调用的函数,如果函数的返回值为false的话,则不显示此control
    'active_callback'   => '',
];
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
function customize_control_image_args($wp_customize) {
    //创建panel略  
    //创建section略  
    //创建setting略  
    
    //创建control
    $wp_customize->add_control(new WP_Customize_Image_Control(
        $wp_customize,
        'control_id_01',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_01',
            'label' => '图片上传',
            'description' => '上传图片',
        ]
    ));
}

add_action('customize_register', 'customize_control_image_args');
  • 附件上传类型的control(WP_Customize_Upload_Control)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
$upload_control_args = [
    # control属于哪个section,填写section的id
    'section'           => '',
    # 填写和此control对应的setting的ID,作用是将contorl(界面)和setting(数据)建立关系
    'settings'          => '',
    # 优先级,用于同一个section下的control间的排序,值越小越靠前
    'priority'          => 10,
    'label'             => '',
    'description'       => '',
    # control激活前会调用的函数,如果函数的返回值为false的话,则不显示此control
    # mime_type描述的是,设置可以从媒体库中选择的文件类型
    'mime_type'         => '',
    'active_callback'   => '',
];
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
function customize_control_upload_args($wp_customize) {
    //创建panel略  
    //创建section略  
    //创建setting略  
    
    //创建control
    $wp_customize->add_control(new WP_Customize_Upload_Control(
        $wp_customize,
        'control_id_01',
        [
            'section' => 'section_id_01',
            'settings' => 'setting_id_01',
            'label' => '附件上传',
            'description' => '上传附件',
            'mime_type' => 'application/msword', //限定只能在媒体库中选择doc文件,但是无法限制上传文件的类型
        ]
    ));
}

add_action('customize_register', 'customize_control_upload_args');

操作panel

场景主要是针对wp自带的功能panel

菜单->panel ID为’nav_menus'

小工具->panel ID为’widgets'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
// 修改panel
add_action('customize_register', function ($wp_customize) {
    $my_panel = $wp_customize->get_panel('custom_form');
    $my_panel->title = '哈哈';
    $my_panel->description = '修改了panel的description';
});

// 删除panel
add_action('customize_register', function ($wp_customize) {
    $wp_customize->remove_panel('custom_form');
});

//操作默认的panel
add_action('customize_register', function ($wp_customize) {
    $menu_panel = $wp_customize->get_panel('nav_menus');
    $menu_panel->title = '站点菜单';
    $meun_panel->priority = 10;
    $widget_panel = $wp_customize->get_panel('widgets');
    $widget_panel->title = '站点边栏';
    $widget_panel->priority = 5;
});

操作section

wp自带的section

激活主题->section的ID为’themes'

站点身份->section的ID为’title_tagline'

颜色->section的ID为’colors'

顶部图像->section的ID为’header_image'

背景图像->section的ID为’background_image'

静态首页->section的ID为’static_front_page'

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 修改section
add_action('customize_register', function ($wp_customize) {
    $my_section = $wp_customize->get_section('section_id_01');
    $my_section->title = '最新标题';
    $my_section->panel = '修改了section的panel';
});

// 删除section
add_action('customize_register', function ($wp_customize) {
    $wp_customize->remove_section('section_id_01');
});

操作control

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
// 修改control
add_action('customize_register', function ($wp_customize) {
    $my_control = $wp_customize->get_control('my_control');
    $my_section->label = '最新标题';
    $my_section->type = 'text';
});

// 删除control
add_action('customize_register', function ($wp_customize) {
    $wp_customize->remove_control('my_control');
});

操作setting

如果setting被移除,对应的control可以显示,但是control的数据不再被保存

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
// 修改setting
add_action('customize_register', function ($wp_customize) {
    $my_setting = $wp_customize->get_setting('my_setting');
    $my_setting->type = 'theme_mod';//更改setting的存储方式为theme_mod
    $my_setting->default = 'default value';
    $Y
});

// 删除setting
add_action('customize_register', function ($wp_customize) {
    $wp_customize->remove_setting('my_setting');
});

获取用户自定义的数据

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * Retrieves an option value based on an option name.
 *
 * If the option does not exist, and a default value is not provided,
 * boolean false is returned. This could be used to check whether you need
 * to initialize an option during installation of a plugin, however that
 * can be done better by using add_option() which will not overwrite
 * existing options.
 *
 * Not initializing an option and using boolean `false` as a return value
 * is a bad practice as it triggers an additional database query.
 *
 * The type of the returned value can be different from the type that was passed
 * when saving or updating the option. If the option value was serialized,
 * then it will be unserialized when it is returned. In this case the type will
 * be the same. For example, storing a non-scalar value like an array will
 * return the same array.
 *
 * In most cases non-string scalar and null values will be converted and returned
 * as string equivalents.
 *
 * Exceptions:
 *
 * 1. When the option has not been saved in the database, the `$default_value` value
 *    is returned if provided. If not, boolean `false` is returned.
 * 2. When one of the Options API filters is used: {@see 'pre_option_$option'},
 *    {@see 'default_option_$option'}, or {@see 'option_$option'}, the returned
 *    value may not match the expected type.
 * 3. When the option has just been saved in the database, and get_option()
 *    is used right after, non-string scalar and null values are not converted to
 *    string equivalents and the original type is returned.
 *
 * Examples:
 *
 * When adding options like this: `add_option( 'my_option_name', 'value' )`
 * and then retrieving them with `get_option( 'my_option_name' )`, the returned
 * values will be:
 *
 *   - `false` returns `string(0) ""`
 *   - `true`  returns `string(1) "1"`
 *   - `0`     returns `string(1) "0"`
 *   - `1`     returns `string(1) "1"`
 *   - `'0'`   returns `string(1) "0"`
 *   - `'1'`   returns `string(1) "1"`
 *   - `null`  returns `string(0) ""`
 *
 * When adding options with non-scalar values like
 * `add_option( 'my_array', array( false, 'str', null ) )`, the returned value
 * will be identical to the original as it is serialized before saving
 * it in the database:
 *
 *     array(3) {
 *         [0] => bool(false)
 *         [1] => string(3) "str"
 *         [2] => NULL
 *     }
 *
 * @since 1.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $option        Name of the option to retrieve. Expected to not be SQL-escaped.
 * @param mixed  $default_value Optional. Default value to return if the option does not exist.
 * @return mixed Value of the option. A value of any type may be returned, including
 *               scalar (string, boolean, float, integer), null, array, object.
 *               Scalar and null values will be returned as strings as long as they originate
 *               from a database stored option value. If there is no option in the database,
 *               boolean `false` is returned.
 */
function get_option( $option, $default_value = false );


/**
 * Retrieves theme modification value for the active theme.
 *
 * If the modification name does not exist and `$default_value` is a string, then the
 * default will be passed through the {@link https://www.php.net/sprintf sprintf()}
 * PHP function with the template directory URI as the first value and the
 * stylesheet directory URI as the second value.
 *
 * @since 2.1.0
 *
 * @param string $name          Theme modification name.
 * @param mixed  $default_value Optional. Theme modification default value. Default false.
 * @return mixed Theme modification value.
 */
function get_theme_mod( $name, $default_value = false );

角色和权限

https://wordpress.org/documentation/article/roles-and-capabilities/

1
2
3
操作<->权限  一对一
角色<->权限  多对多
用户<->角色  多对多

用户角色

wordpress内置了6个角色

  • 超级管理员(wp开启多站点功能,才会有该角色)
  • 管理员
  • 编辑
  • 作者
  • 投稿者
  • 订阅者

后台可以设置用户注册后的默认角色

获取权限

用户登录->获取用户角色->获取角色权限

使用 Hugo 构建
主题 StackJimmy 设计