WordPress如何通过WP_Term_Query查询自定义分类下所有的分类信息?

WP_Term_Query查询参数条件

1
2
3
4
5
6
7
8
// WP_Term_Query arguments
$args = array(
'taxonomy' => array( '自定义分类名称' ),// register_taxonomy('自定义分类名称', '自定义分类', $args);
);
// The Term Query
$term_query = new WP_Term_Query( $args );
//print_r($term_query);
wp_reset_postdata();

WP_Term_Query查询结果输出

1
2
3
4
5
6
7
$resource_tags=array();
if ( ! empty( $term_query->terms ) ) {
foreach ( $term_query ->terms as $term ) {
$resource_tags[$term->name]=$term->term_id;
//$resource_tags[$term->name]=$term->slug;
}
}

WordPress移除编辑器自带的noreferer和noopener参数

1
2
3
4
5
6
7
//remove noreferrer on the frontend, *will still show up in the editor.*
function no_referrer($content) {
$replace = array('rel="noopener noreferrer"' => "" );
$new_content = strtr($content, $replace);
return $new_content;
}
add_filter('the_content', 'no_referrer', 999);

wordpress如何获取文章特色图像IDget_post_thumbnail_id()

1
2
//$post_id – 文章ID,默认为空
get_post_thumbnail_id( $post_id );

获取文章特色图像

1
2
wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()));
echo $thumbnail_src[0];

wordpress如何获取文章特色图像wp_get_attachment_image_src()

1
$full_size_image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()));
1
<img    src="<?php echo $full_size_image[0];?>" width="<?php echo $full_size_image[1];  ?>" height="<?php echo $full_size_image[2];  ?>">

WordPress如何通过WP_Query函数查询产品信息

1
2
3
4
5
6
7
8
9
10
11
12
13
//设置每页产品数量
$posts_per_page =8;
$args = array(
'post_type' => 'product',//调用的类型为产品(product)
'posts_per_page' => $posts_per_page,
'posts_per_archive_page' => $posts_per_page,
'nopaging' => false,
'ignore_sticky_posts' => false,
'paged' => $paged,
);
$query=new WP_Query($args);
// print_r($query);
$total_pages = $query->max_num_pages; //总共多少页

WordPress遇到the_date不显示的情况该怎么办?

输出文章的信息,有时候替代the_date并不能在前台显示

使用echo get_the_date(‘Y-m-d’);替代the_date(‘Y-m-d’);完美解决

1
echo get_the_date('Y-m-d');

WordPress如何通过URL获取多个参数,并通过WP_Query查询

PHP判断URL参数是否存在已经参数的值

1
2
if(isset($_GET['Type']) && $_GET['Type']!="ALL"){
}

WP_Query 查询变量

1
2
3
4
5
6
7
8
$args=array(
'post_type' =>'post',
'cat' => $catnos,
'nopaging' => false,
'paged' => $paged,
'posts_per_page' => '10',
'posts_per_archive_page' => '10',
);

WP_Query 补充修改变量

1
2
3
4
// 获取分类
if (isset($_GET['category'])) {
$args[cat]=$_GET['category'];
}
1
2
3
4
5
6
7
8
9
// 获取国家
if (isset($_GET['country']) && $_GET['country']!="ALL") {
$country_query = array(
'key' => 'country',
'value' => $_GET['country'] ,
'compare' => '='
);
$meta_query[]=$country_query;
}
1
2
3
4
5
6
7
8
9
// 获取Type参数
if (isset($_GET['Type']) && $_GET['Type']!="ALL") {
$Type_query = array(
'key' => 'type',
'value' => $_GET['Type'] ,
'compare' => '='
);
$meta_query[]=$Type_query;
}
1
2
3
4
5
$meta_query[relation] = "AND";
$args[meta_query]=$meta_query;

$query = new WP_Query( $args );
wp_reset_query();
1
2
3
4
if( $query->have_posts() ):
while( $query->have_posts() ) : $query->the_post();
endwhile;
endif;

通过WP_Query查询特定meta的文章或产品,统计数量并保存为json文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$data = array();

$args = array(
'orderby' => 'title',
'order' => 'ASC',
'post_type' =>'post',
'nopaging' => true,
'meta_key' => '交易货币',
'meta_value' => 'United States',
);
$the_query = new WP_Query( $args );
wp_reset_postdata();
//重组数组
$data["United States"] = $the_query->post_count;

//数组转json
$json_string = json_encode($data);

//保存文件在网站根目录
file_put_contents(ABSPATH .'/json/json.json', $json_string);

分类页面如何获取当前页面的分类名 single_cat_title()

1
2
3
single_cat_title($prefix,$display);
//$prefix :用于设置在标题之前显示的内容。
//$display :用于设置是直接显示还是返回到变量。

文章页面如何获取当前页面的标题名 single_post_title()

1
single_post_title();