WordPress常用标签笔记

常用标签汇总;如果对你有用收藏,注意部分标签是需要在function中添加代码的;
如果你使用调用未生效可以联系我提供function文件

常用部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php the_title(); ?>	//文章标题  
<?php the_excerpt(); ?> //文章简介
<?php the_permalink() ?> // href连接
<?php the_category(); ?> //| 当前分类
<?php the_ID(); ?> //当前ID
<?php the_post_thumbnail(); ?> //缩略图
<?php the_post_thumbnail_url(); ?> //获取缩略图URL地址
has_post_thumbnail();
<?php the_post_thumbnail('index', array( 'class' => 'style','title' => get_the_title(),'alt' => get_the_title())); ?>
//自定义class缩略图
<?php the_tags(); ?> // TAG
<?php the_author(); ?> //作者
<?php the_author_link(); ?>
<?php wp_tag_cloud( $args ); ?> //标签云
<?php the_time(); ?> // 时间
/*英语月输入
使用get_the_time('U')获取时间戳
*然后用date函数重新输出
*/
echo date('M',get_the_time('U'));

关于timthumb标签
timthumb.php自定义缩略图

1
2
<img src="<?php bloginfo('template_url');?>/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&h=150&w=200&zc=1" alt="<?php the_title(); ?>" class="thumbnail"/>

导航

导航分类页面

.```php

name?>

1
2
3
4
5
6
7

### 导航单页

```php
<a href="<?php echo get_page_link(对应ID)?>">
<?php echo get_page(ID)->post_title; ?>
</a>

调用指定单篇ID文章

1
2
3
4
<?php query_posts('p=1'); ?>//p=文章ID
<?php while (have_posts()) : the_post(); ?>
//填写对应调用的信息
<?php endwhile;wp_reset_query();?>

文章截取简介

1
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200,"...");?>

自动获取简介

1
2
3
4
5
6
7
8
9
10
11
<?php if(has_excerpt()) the_excerpt(); else echo mb_strimwidth(strip_tags($post->post_content),0,200,'...'); ?>

echo str_replace(array('<p>','</p>'),array('',''),get_the_excerpt())
//去除P标签
echo str_replace(array('<p>','</p>',PHP_EOL),array('','',','),get_the_excerpt())
//去除换行
<?php if(has_excerpt()): //如果有摘要就显示,没有就从文章中截取 ?>
<?php the_excerpt(); ?>
<?php else: ?>
<?php echo mb_strimwidth(strip_tags(apply_filters('the_content',$post->post_content)),0,200,"...");?>
<?php endif; ?>

面包屑导航(functions)

1
2
<?php if (function_exists('dimox_breadcrumbs')) dimox_breadcrumbs(); ?>

当前分类名称

1
<?php single_cat_title(); ?>

获取当前分类ID

1
2
3
$category = get_the_category();
echo $category[0]->cat_ID;
//详情页获取当前所属分类

获取顶级分类

1
2
3
4
5
6
7
8
9
//获取顶级分类ID
function salong_category_top_parent_id ($current_cat_ID) {
while ($current_cat_ID) {
$cat = get_category($current_cat_ID); // 得到分类
$current_cat_ID = $cat->category_parent; // 当前分类的父分类
$catParent = $cat->cat_ID;
}
return $catParent;
}

文章列表调用

分页(需插件支持)

1
<?php wp_pagenavi(); ?>

置顶文章

1
2
3
4
5
6
7
8
9
10
//调用置顶文章
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 9);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 2 ) );
if (have_posts()) :while (have_posts()) : the_post();
?>
//要显示的内容
<?php endwhile; endif; ?>

指定ID文章列表调用

1
2
3
4
5
6
<?php query_posts('cat=ID&posts_per_page=数目'); while(have_posts()): the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_title(); ?></a>
</li>
<?php endwhile; wp_reset_query(); ?>

当前文章调用

1
2
3
4
5
6
7
8
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li><h3><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a></h3>
</li>

<?php endwhile; ?>
<?php endif; ?>

分类页指定category.php

1
2
3
4
5
6
7
8
9
10
<?php
if ( is_category(array(3,4,5)) ){
include(TEMPLATEPATH .'/category-pro.php');
}
else if( is_category(1) ){
include(TEMPLATEPATH . '/category-products.php');
}else{
include(TEMPLATEPATH . '/category-new.php');
}//产品列表
?>

随机调用文章

1
2
3
4
<?php query_posts('showposts=10&cat=1&orderby=rand');
while(have_posts()) : the_post();?>

<?php endwhile; wp_reset_query(); ?>

相关文章调用

1
2
3
4
5
6
7
8
9
在列表页获取当前ID,并随机当前栏目文章列表,详情页也可使用

<?php $category = get_the_category();$cat_Id = $category[0]->term_id; ?>
<?php query_posts('showposts=6&cat='.$cat_Id.'&orderby=rand');
while(have_posts()) : the_post();?>

<!--循环的内容-->

<?php endwhile; wp_reset_query(); ?>

最新文章(全站)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//官方推荐函数wp_query()
<?php $post_query = new WP_Query('showposts=10');
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>


<?php
$args = array('post__not_in'=> get_option( 'sticky_posts' ),'showposts'=> 10,);//除去置顶文章
$post_query = new WP_Query($args);
while ($post_query->have_posts()) : $post_query->the_post();
$do_not_duplicate = $post->ID; ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endwhile; wp_reset_query(); ?>


<?php
$new_posts = get_posts("numberposts=10");
foreach( $new_posts as $post ) :
?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

文章点击量

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
<?php
//文章点击数
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0";
}
return $count;
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}

?>

显示点击量(如category.php或single.php)
<?php echo getPostViews(get_the_ID()); ?>

内容页

内容分页指定

1
2
3
4
5
6
7
8
9
10
11
<?php
if ( in_category(array(3,4,5)) ){
include(TEMPLATEPATH .'/single-pro.php');
}
else if( in_category(array(2)) ){
include(TEMPLATEPATH . '/single-products.php');
}else{
include(TEMPLATEPATH . '/single-new.php');
}//产品列表
?>

内容调用

1
2
3
4
5
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content(__('(more...)')); ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

内容过滤标签

1
2
3
4
5
6
7
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $content = apply_filters('the_content', get_the_content());
$content = str_replace('h3>', 'p>', $content);?>
<?php echo $content; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

过滤标签中含有width = “500”

1
2
3
4
5
6
7
8
9

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php $search = '/((width)=[\'"]+[0-9]+[\'"]+)/'; ?>
<?php $content = apply_filters('the_content', get_the_content());
$content = preg_replace($search, '', $content);?>
<?php echo $content; ?>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>

内容页调用同分类文章

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php  
if ( is_single() ) :
global $post;
$categories = get_the_category();
foreach ($categories as $category) :
?>

<?php
$posts = get_posts('numberposts=5&category='. $category->term_id.'&exclude='.get_the_ID());
foreach($posts as $post) :
?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</li>
<?php endforeach; ?>

<?php
endforeach; endif ; ?>

上一篇 下一篇

1
2
3
4
5
6
7
8
9
10
11
12
13
14
上一篇:
<?php $prev_post = get_previous_post();if (!empty( $prev_post )): ?>
<a title="<?php echo $prev_post->post_title; ?>" href="<?php echo get_permalink( $prev_post->ID ); ?>">
<?php echo $prev_post->post_title; ?>
</a>
<?php else: ?>
None
<?php endif; ?>
下一篇:
<?php $next_post = get_next_post();if (!empty( $next_post )): ?>
<a title="<?php echo $next_post->post_title; ?>" href="<?php echo get_permalink( $next_post->ID ); ?>">
<?php echo $next_post->post_title; ?>
</a>
<?php endif; ?>

TAG

has_tag

1
2
3
4
5
6
7

has_tag()
//判断当前文章是否有一个标签,必须用在主循环中。
has_tag( 'mild' )
//判断当前文章是否有标签 “mild”。这里也可以用标签ID.
has_tag( array( 'sharp', 'mild', 'extreme' ) )
//判断当前文章是否有数组里的那些标签。这里也可以用标签ID,如has_tag( array( '44', '45', '107' ) // ),解说同上。

tag相关函数

1
2
3
4
5
6
7
8
9
10
11
12
13

<?php get_tag() ?>
<?php get_tag_link() ?>
<?php get_tags() ?>
<?php get_the_tag_list() ?>
<?php get_the_tags() ?>
<?php is_tag() ?>
<?php the_tags() ?>
<?php single_tag_title() ?>
<?php tag_description() ?>
<?php wp_generate_tag_cloud()?>
<?php wp_tag_cloud()
<?php get_the_term_list() ?>

page页面

获取指定ID下的子页面的列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
function page_child_list($pageIds){
$page_object = get_queried_object();
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
$portfolio_children = get_page_children( $pageIds, $all_wp_pages );
$page_html = "";
foreach($portfolio_children as $tao_page){
$tao_url = get_permalink($tao_page->ID);
$tao_title = $tao_page -> post_title;
$page_html ="<li><a class = '' href='$tao_url' title ='".$tao_title."'>$tao_title</a></li>";
echo $page_html;
}
}

page_child_list(id);
?>

分别为父级下获取和子级下获取

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
<?php 
//调用所有page页面的方法;
$args = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $current_obj_id,
'orderby' => 'menu_order',
'order' => 'ASC',
'nopaging' => true,
);

//----默认的值---
$defaults = array(
'numberposts' => 5,
'category' => 0, 'orderby' => 'date',
'order' => 'DESC', 'include' => array(),
'exclude' => array(), 'meta_key' => '',
'meta_value' =>'', 'post_type' => 'post',
'suppress_filters' => true
);
$child_pages = get_posts($args);
?>
<?php
function page_childrens(){
if ( is_page() ){
//当前页面的ID
$current_obj_id = get_queried_object_id();
$args = array(
'post_status' => 'publish',
'post_type' => 'page',
'post_parent' => $current_obj_id,
'orderby' => 'menu_order',
'order' => 'ASC',
'nopaging' => true,
);

$child_pages = get_posts($args);
$html = "";
foreach ($child_pages as $post) {
setup_postdata($post);
$url = get_permalink($post->ID);
$title = $post->post_title;
$html = "<li><a href='".$url."'>".$title."</a></li>";
echo $html;
}
wp_reset_postdata();
}
}
?>

<?php
function page_child(){
$page_object = get_queried_object();
$page_ID =$page_object->ID;//当前页面的ID
$page_parents = $page_object->post_parent ;//获取父pageID
if($page_parents){
$my_wp_query = new WP_Query();
$all_wp_pages = $my_wp_query->query(array('post_type' => 'page', 'posts_per_page' => '-1'));
$portfolio_children = get_page_children( $page_parents, $all_wp_pages );
$page_html = "";
foreach($portfolio_children as $tao_page){
$tao_url = get_permalink($tao_page->ID);
$tao_title = $tao_page -> post_title;
$tao_pID = $tao_page -> post_parent;
$tao_ID = $tao_page -> ID;
if($page_ID == $tao_ID){ $classon = "on";}
$page_html ="<li><a class = '' href='$tao_url' title ='".$tao_title."'>$tao_title</a></li>";
echo $page_html;
}
}


}
//获取当前page页面下同父page下的所有子page
?>

page页面的特色图片调用

1
<?php $timthumb_src = wp_get_attachment_image_src( get_post_thumbnail_id(20),full); ?>

page页面函数:通过page名获取父级ID

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
//获取父级分类ID	
function tao_get_page_ID($pageName) {
global $wpdb;

$pages = get_pages();

for($i = 0; $i < count($pages); $i++) {

if($pageName == $pages[$i]->post_title) $page_slug = $pages[$i]->post_name;//根据用户提供的页面名称获得页面别名

}

if($page_slug) {

//根据页面别名获得页面ID

$page_ID = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_name = '" . $page_slug . "' AND post_status = 'publish' AND post_type = 'page'");

return $page_ID;

} else {

return false;

}

}

jQuery 过滤表格

过滤table复制过来自带的样式

1
$('.product-desc table tr').attr('style','');

社媒分享

社媒分享按钮

https://www.addtoany.com

使用方法

1
2
3
4
5
6
7
8
9
10
11
<div class="a2a_kit a2a_kit_size_32 a2a_default_style">
<a class="a2a_button_facebook"></a>
<a class="a2a_button_twitter"></a>
<a class="a2a_button_google_plus"></a>
<a class="a2a_button_linkedin"></a>
<a class="a2a_button_email"></a>
<a class="a2a_button_skype"></a>
</div>

<script async src="https://static.addtoany.com/menu/page.js"></script>

WP时间拓展

1
2
3
4
5
/*使用get_the_time('U')获取时间戳
*然后用date函数重新输出
*/
echo date('M',get_the_time('U'));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
a - "am" 或是 "pm"
A - "AM" 或是 "PM"
d - 几日,二位数字,若不足二位则前面补零; 如: "01""31"
D - 星期几,三个英文字母; 如: "Fri"
F - 月份,英文全名; 如: "January"
h - 12 小时制的小时; 如: "01""12"
H - 24 小时制的小时; 如: "00""23"
g - 12 小时制的小时,不足二位不补零; 如: "1"12"
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"
i - 分钟; 如: "00" 至 "59"
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"
l - 星期几,英文全名; 如: "Friday"
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"
M - 月份,三个英文字母; 如: "Jan"
s - 秒; 如: "00" 至 "59"
S - 字尾加英文序数,二个英文字母; 如: "th","nd"
t - 指定月份的天数; 如: "28" 至 "31"
U - 总秒数
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)
Y - 年,四位数字; 如: "1999"
y - 年,二位数字; 如: "99"
z - 一年中的第几天; 如: "0" 至 "365"

常用is_xxx()判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
is_sticky() //是否置顶
is_home() //: 是否为主页
is_single() //: 是否为内容页(Post)
is_page() //: 是否为内容页(Page)
is_category() //: 是否为Category/Archive页
is_tag() //: 是否为Tag存档页
is_date() //: 是否为指定日期存档页
is_year() //: 是否为指定年份存档页
is_month() //: 是否为指定月份存档页
is_day() //: 是否为指定日存档页
is_time() //: 是否为指定时间存档页
is_archive() //: 是否为存档页
is_search() //: 是否为搜索结果页
is_404() //: 是否为 “HTTP 404: Not Found” 错误页
is_paged() //: 主页/Category/Archive页是否以多页显示

Search搜索处理

主题下新建search.php文件
内容为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$search_query =& new WP_Query("s=$s & showposts=-1");?>

<?php get_header(); ?>

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

<?php endwhile; else: ?>
<p >
<?php _e('The content you are searching for does not exist'); ?>
</p>
<?php endif; ?>
<div class="nav-previous">
<?php previous_posts_link(__('« Older Entries', 'kubrick')); ?>
</div>
<div class="nav-next">
<?php next_posts_link(__('Newer Entries »', 'kubrick')); ?>
</div>

<?php get_footer(); ?>

搜索form表单

1
2
3
4
<form  action="/" method="get"  >
<input name="s" type="text" placeholder="网站检索" id="s" value="<? the_search_query(); ?>" />
<input name="sa" value="检索" type="image" src="<?php bloginfo('template_url'); ?>/images/search_icon.gif" align="top" class="btn" />
</form>

数据库常用操作

备份数据库

1
2
mysqldump -uroot -pxxxxx xxx>xxx.sql

数据库恢复操作

1
mysql -uroot -pxxxxxx aluminum-alloy</home/web_bak/mysql_bak/aluminum-alloy.sql

WordPress 常用函数

wp_list_categories()

1
2
3
4
<?php wp_list_categories('orderby=name&depth=&title_li=&hierarchical=1&hide_empty=0&child_of=893'); ?>
传递参数方式
<?php wp_list_categories('orderby=name&depth=1&title_li=&hierarchical=1&hide_empty=0&child_of='.$catId); ?>

指定ID分类下的子分类

引用:https://www.5zzu.com/post/28.html

get_queried_object();一个神奇的函数

1
$page_object = get_queried_object();

get_queried_object_id() 函数

1
2
<?php get_queried_object_id() ?>
//检索当前查询对象的 ID

get_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
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
<?php	$terms = get_terms( 'category', array('orderby' => 'count', 'hide_empty' => 0,) ); ?>
<?php array('orderby' => 'count', 'hide_empty' => 0,'parent' => '0',) //parent父栏目ID为指定下子分类的数组对象,0为不限默认值 ?>
<?php
//数组可设置值
$get_terms_default_attributes = array (
'taxonomy' => 'category', //empty string(''), false, 0 don't work, and return empty array
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true, //can be 1, '1' too
'include' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude_tree' => 'all', //empty string(''), false, 0 don't work, and return empty array
'number' => false, //can be 0, '0', '' too
'offset' => '',
'fields' => 'all',
'name' => '',
'slug' => '',
'hierarchical' => true, //can be 1, '1' too
'search' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false, //can be 0, '0', '' too
'get' => '',
'child_of' => false, //can be 0, '0', '' too
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true, //can be 1, '1' too
'meta_query' => '',
'meta_key' => array(),
'meta_value'=> '',
);
?>
返回的数组对象是:
object(WP_Term)#372 (10) {
["term_id"]=>
int(1)
["name"]=>
string(11) "Bronze lion"
["slug"]=>
string(11) "bronze-lion"
["term_group"]=>
int(0)
["term_taxonomy_id"]=>
int(1)
["taxonomy"]=>
string(8) "category"
["description"]=>
string(0) ""
["parent"]=>
int(0)
["count"]=>
int(0)
["filter"]=>
string(3) "raw"
}
-->

WP_Query 参数:状态、排序和分页

默认array数组值

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
$get_terms_default_attributes = array (
'taxonomy' => 'category', //empty string(''), false, 0 don't work, and return empty array
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => true, //can be 1, '1' too
'include' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude' => 'all', //empty string(''), false, 0 don't work, and return empty array
'exclude_tree' => 'all', //empty string(''), false, 0 don't work, and return empty array
'number' => false, //can be 0, '0', '' too
'offset' => '',
'fields' => 'all',
'name' => '',
'slug' => '',
'hierarchical' => true, //can be 1, '1' too
'search' => '',
'name__like' => '',
'description__like' => '',
'pad_counts' => false, //can be 0, '0', '' too
'get' => '',
'child_of' => false, //can be 0, '0', '' too
'childless' => false,
'cache_domain' => 'core',
'update_term_meta_cache' => true, //can be 1, '1' too
'meta_query' => '',
'meta_key' => array(),
'meta_value'=> '',
);

get_posts()

看page中的内容

WordPress使用问题

去除文章编辑插入图片自动短标签[caption]

1
2
3
4
5
add_filter( 'disable_captions', create_function('$a', 'return true;') );
//插入图片时删除caption短标签[/caption]

add_filter( 'img_caption_shortcode', create_function('$a, $b, $c', 'return $c;'), 10, 3 );
//去除插入img文件自动创建DIV

function功能函数

判断目录是否为空目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 判断是否为空目录
* @param $path string 指定的路径
* @return bool 空目录和非目录均返回true
*/
function is_empty_dir($path) {
$document_root=$_SERVER["DOCUMENT_ROOT"];
if(!is_dir($document_root.$path)) {

return ture;
}

$dir = opendir($document_root.$path);
while(false !== ( $file = readdir($dir)) ) {
if(!in_array($file, ['.', '..'])) {
closedir($dir);
return false;
}
}

closedir($dir);
return true;
}

循环中文章ID获取所属分类

1
2
3
4
5
6
7
8
//object_id:3 对应wp_posts
function get_post_category_id($post_ID){
global $wpdb;
$sql="SELECT `term_taxonomy_id` FROM $wpdb->term_relationships WHERE `object_id`='".$post_ID."';";
$cat_id=$wpdb->get_results($sql);
$output=$cat_id[0]->term_taxonomy_id;
return($output);
}

==WordPress后台字体修改==

1
2
3
4
5
6
function admin_lettering(){
echo'<style type="text/css">
body{ font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu, Microsoft YaHei,Cantarell,"Helvetica Neue",sans-serif;}
</style>';
}
add_action('admin_head', 'admin_lettering');

去除excerpt中的P标签

1
remove_filter( 'the_excerpt', 'wpautop' );

去除不必要的前台样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
remove_action( 'wp_head', 'wp_resource_hints', 2 );
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
add_filter( 'emoji_svg_url', '__return_false' );
remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'rel_canonical' );
remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
add_filter('show_admin_bar', '__return_false');

function clean_header(){ //移除comment-reply.js第三方评论插件
wp_deregister_script( 'comment-reply' );
}
add_action('init','clean_header');

更改后台url

1
2
3
4
add_action('login_enqueue_scripts','login_protection');  
function login_protection(){
if($_GET['youfine'] != '2018')header('Location: https://www.google.com/search?newwindow=1&source=hp&ei=YmTJW6CxK4yRrgTB-JSYBw&q=you+fine+sculpture&oq=youfine');
}

关闭xmlrpc

1
add_filter('xmlrpc_enabled','__return_false');

然后删除

Wordpress 后台菜单嵌套表单

1
2
3
4
5
6
7
8
add_action('admin_menu', 'register_custom_menu_page');

function register_custom_menu_page(){
add_menu_page('询盘系统登录', '询盘系统登录', 'administrator', 'custompage', 'custom_menu_page', '', 6);
}
function custom_menu_page(){
echo '<iframe src="//www.essentialoilmachine.com/data/admin_safe_ims/login.php" width="100%" height="900" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no" allowtransparency="yes"></iframe>';
}

timthumb.php 截取文章缩略图

timthumb需要主机支持GD库;timthumb处理过程需要一定的服务器资源支持;timthumb不支持外链图片;
方法,上传timthumb.php到主题文件下
复制函数到function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function post_thumbnail_src(){
global $post;
if( $values = get_post_custom_values("thumbnail") ) { //输出自定义域图片地址
$values = get_post_custom_values("thumbnail");
$post_thumbnail_src = $values [0];
} elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址
$thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full');
$post_thumbnail_src = $thumbnail_src [0];
} else {
$post_thumbnail_src = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$post_thumbnail_src = $matches [1] [0]; //获取该图片 src
if(empty($post_thumbnail_src)){
$post_thumbnail_src = get_bloginfo('template_url')."/images/no-image.jpg"; //如果日志中没有图片,则显示默认图片
}
};
return $post_thumbnail_src;
}

上面的代码表示先获取自定义域的图片,如果没有设置自定义域图片则获取特色图像,如果没有设置特色图像获取内容首张图片,如果内容也没有图片,则显示默认图像no-image.jpg。

调用

1
src="<?php bloginfo('template_url');?>/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&h=150&w=200&zc=1"

更多详细的传参细节看这里

WP自带编辑器增加字体颜色

functions增加如下代码

1
2
3
4
5
6
7
8
9
10
11
12
13
function add_more_buttons($buttons) {
$buttons[] = 'hr'; //水平线
//$buttons[] = 'fontselect'; //字体
$buttons[] = 'fontsizeselect'; //字号
$buttons[] = 'styleselect'; //样式,格式
$buttons[] = 'wp_page'; //分页符
$buttons[] = 'copy'; //复制
$buttons[] = 'paste'; //粘贴
$buttons[] = 'cut'; //剪切
$buttons[] = 'backcolor'; //背景色
return $buttons;
}
add_filter("mce_buttons_3", "add_more_buttons");

图片懒加载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
https://cdn.bootcss.com/jquery_lazyload/1.9.7/jquery.lazyload.min.js

function lazyload($content) {
if(!is_feed()||!is_robots) {
$content=preg_replace('/<img(.+)src=[\'"]([^\'"]+)[\'"](.*)>/i',"<img\$1data-original=\"\$2\" src=\"data:image/gif;base64,R0lGODlhAQABAIAAAOHh4QAAACH5BAAAAAAALAAAAAABAAEAQAICRAEAOw==\"\$3>\n",$content);
}
return $content;
}
add_filter ('the_content', 'lazyload');

// lazyload JS代码(需引入jquery.lazyload.js)
$("img").lazyload({
effect : "fadeIn",
threshold : 100,
});

python采集套装函数

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
//获取顶级分类ID
function category_top_parent_id ($current_cat_ID) {
while ($current_cat_ID) {
$cat = get_category($current_cat_ID); // 得到分类
$current_cat_ID = $cat->category_parent; // 当前分类的父分类
$catParent = $cat->cat_ID;
}
return $catParent;
}

// 循环列表中通过文章ID获取分类ID

function rand_get_dir_image($dir){
$tmp=array();
if($dd = opendir($dir)){
while(($file=readdir($dd)) !== false){
if($file == "." || $file == "..") continue;
array_push($tmp,$file);
}
closedir($dd);
shuffle($tmp);
if(count($tmp)>0)
return $tmp[0];
return false;
}
return false;
}

function list_add_thumbs($content,$dir){

preg_match_all("/(<h3>([^>]*)<\/h3>)[\s\r\n]*(<p>[^>]*<\/p>)/",$content,$match);

$document_root=$_SERVER["DOCUMENT_ROOT"];
$imgdir=$document_root.$dir;
if(empty($match[1])) return $content;
$content = array();
for($i=0;$i<count($match[1]);$i++){
$title= $match[1][$i]."\r\n";
$no_tag_title =$match[2][$i];
$desc= $match[3][$i]."\r\n";
$imgsrc= $dir.rand_get_dir_image($imgdir);
$content[] = array($title,$imgsrc,$desc,$no_tag_title);

}
return $content;
//返回数组
}

// 通过产品ID获取父类ID;
function get_post_category_id($post_ID){
global $wpdb;
$sql="SELECT `term_taxonomy_id` FROM $wpdb->term_relationships WHERE `object_id`='".$post_ID."';";
$cat_id=$wpdb->get_results($sql);
$output=$cat_id[0]->term_taxonomy_id;
return $output;
}


/**********tag图片*********/
function show_rand_img($alt="",$num=1,$dir="/wp-content/load/"){
$document_root=$_SERVER["DOCUMENT_ROOT"];
$imgdir=$document_root.$dir;
if(!file_exists($imgdir)) exit("Image directory does not exist");
$imgarr=scandir($imgdir);
if($num>count($imgarr)) $num=count($imgarr);
$re="";
for($i=0;$i<$num;$i++){
$s=rand(0,count($imgarr));
if(!is_file($imgdir.$imgarr[$s])){$i--;continue;}
$re .= "<img src=\"$dir$imgarr[$s]\" alt=\"$alt\" class=\"img-responsive\"/>";
}
return $re;
}



<?php
$category = get_the_category();
$catId = $category[0]->cat_ID;
$dir="/wp-content/themes/Foam/load/$catId/";
$content = $post->post_content;
$CJ = list_add_thumbs($content,$dir);
$num = count($CJ);
//echo "<pre>";
//var_dump($CJs[0][0]);
?>

WordPress隐藏某个插件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//隐藏插件

function hide_plugin_trickspanda() {
global $wp_list_table;
$hidearr = array('插件路径/插件文件');
$myplugins = $wp_list_table->items;
foreach ($myplugins as $key => $val) {
if (in_array($key,$hidearr)) {
unset($wp_list_table->items[$key]);
}
}
}

add_action('pre_current_active_plugins', 'hide_plugin_trickspanda');

wordpress重定向的次数过多

wordpress网站开启https后台提示将您重定向的次数过多怎么解决?
wp-config.php开头加入以下代码

1
2
3
$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

新建分类菜单栏

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

function my_taxonomies_brands()
{
$labels = array(
'name' => _x('灯饰品牌', 'suopu'),
'singular_name' => _x('灯饰品牌', 'suopu'),
'search_items' => __('搜索品牌分类', 'suopu'),
'all_items' => __('所有品牌分类', 'suopu'),
'parent_item' => __('该品牌分类的上级分类', 'suopu'),
'parent_item_colon' => __('该品牌分类的上级分类:', 'suopu'),
'edit_item' => __('编辑品牌分类', 'suopu'),
'update_item' => __('更新品牌分类', 'suopu'),
'add_new_item' => __('添加新的品牌分类', 'suopu'),
'new_item_name' => __('新品牌分类', 'suopu'),
'menu_name' => __('品牌分类', 'suopu'),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'hide_empty' => true,
'rewrite' => array('slug' => 'brands'),
);
register_taxonomy('brands_post_type', 'post', $args);
// Prevent WordPress from sending a 404 for our new perma structure.
}
add_action('init', 'my_taxonomies_brands', 0); //关闭案例分类

Polylang多语言插件

Polylang中添加新字符串

2步
主题中插入要翻译的文字

1
php echo pll_e ('翻译内容' );

functions.php文件中插入

1
pll_register_string('child9''翻译内容','true' );

返回当前语言下对应栏目的其它语言ID;

1
2
3

pll_get_term($term_id,pll_current_language());

其它语言URL;

1
.pll_home_url( $slug );

获取分类ID

1
2
3
4
5
6
7
8
9
10
11
global $wp_query;

$cat_ID = get_query_var('cat');

$cat_top_ID = category_top_parent_id($cat_ID);

$parentid=get_category($cat_ID)->category_parent;

$valueS=get_option('cat-title-'.$cat_ID);
$valueS_parent = get_option('cat-title-'.$parentid);
$valueS_parent_top = get_option('cat-title-'.$cat_top_ID);

上面涉及到的函数

1
2
3
4
5
6
7
8
9
10
	//获取顶级分类ID
function category_top_parent_id ($current_cat_ID) {
while ($current_cat_ID) {
$cat = get_category($current_cat_ID); // 得到分类
$current_cat_ID = $cat->category_parent; // 当前分类的父分类
$catParent = $cat->cat_ID;
}
return $catParent;
}

apacheSSL配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<VirtualHost *:443>     
ServerName "www.yeacode.com"
#ServerAlias "yeacode.com"
DocumentRoot "E:\software\www.yeacode.com"
SSLEngine on
SSLProtocol all -SSLv2 -SSLv3 # 添加SSL协议支持协议,去掉不安全的协议。
SSLCipherSuite HIGH:!RC4:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!EXP:+MEDIUM # 修改加密套件。
SSLHonorCipherOrder on
SSLCertificateFile E:\software\certs\com\1827304_www.yeacode.com_apache\1827304_www.yeacode.com_public.crt
SSLCertificateKeyFile E:\software\certs\com\1827304_www.yeacode.com_apache\1827304_www.yeacode.com.key
SSLCertificateChainFile E:\software\certs\com\1827304_www.yeacode.com_apache\1827304_www.yeacode.com_chain.crt

<Directory "E:\software\www.yeacode.com">
SetOutputFilter DEFLATE
Options FollowSymLinks
AllowOverride All
Require all granted
DirectoryIndex index.php index.html index.htm default.php default.html default.htm
</Directory>
</VirtualHost>

关闭主题插件更新提醒

1
2
3
add_filter('pre_site_transient_update_plugins', create_function('$a', "return null;"));
add_filter('pre_site_transient_update_themes', create_function('$a', "return null;"));