// 小工具注册
register_sidebar( array(
'name' => 'plugin-1',
'id' => 'plugin-1',
'description' => '插件下载小工具',
'class' => 'plugin',
'before_widget' => '
',
'after_widget' => '
',
'before_title' => ''
) );
register_sidebar( array(
'name' => 'index-1',
'id' => 'index-1',
'description' => '首页小工具',
'class' => 'plugin',
'before_widget' => '',
'after_widget' => '
',
'before_title' => ''
) );
register_sidebar( array(
'name' => 'news-1',
'id' => 'news-1',
'description' => '新闻分类小工具',
'class' => 'plugin',
'before_widget' => '',
'after_widget' => '
',
'before_title' => ''
) );
// 短代码
function post_shortcode($atts,$content=null)
{
extract(shortcode_atts(array('category_id'=>'0','count'=>1), $atts));
$str1 = '';
$post_arr = query_posts('cat='.$category_id.'&showposts='.$count);
foreach($post_arr as $val):
$str2 .= '' . get_the_title( $val->ID ) . '';
endforeach;
wp_reset_postdata();
return $str1 . $str2 . $str3;
}
add_shortcode("Postcode",'post_shortcode');
function post_rand_shortcode($atts) {
extract(shortcode_atts(array('category_id'=>'0','slug'=>'1','count'=>2), $atts));
$str1 = '';
$rand_posts = get_posts('orderby=rand');
$i = 0;
foreach( $rand_posts as $post ) {
$i++;
$str2 .= '- ' . get_the_title($post->ID) . '
';
if($i === (int)$count) {
// return $count;
break;
}
}
wp_reset_query();
$str3 = '
';
return $str1 . $str2 . $str3;
}
add_shortcode("rand_post",'post_rand_shortcode');
function poll_shortcode($atts,$content=null)
{
$str = '
';
return $str;
}
add_shortcode("poll",'poll_shortcode');
function custom_post_shortcode($atts) {
extract(shortcode_atts(array('category'=>'0'), $atts));
$title = '' . get_the_title() . '
';
$time = '' . get_the_time('l,Y/m/d H:i:s') . '
';
$arr_post = sp_query_post(array(
'post_status'=>'publish',
'posts_per_page'=>-1,
'order'=>'ASC',
'meta_query' => array(
array(
'key'=>'wp-codex',
'value'=>$category
)
)
), -1);
$str1 = '';
foreach($arr_post as $val){
$str2 .= '' . $val['post_title'] . '';
}
return $title . $time . $str1 . $str2 . $str3;
}
add_shortcode("custom_post_1",'custom_post_shortcode');
function custom_postall_shortcode($atts) {
extract(shortcode_atts(array('category'=>'0'), $atts));
$title = '' . get_the_title() . '
';
$time = '' . get_the_time('l,Y/m/d H:i:s') . '
';
$arr_post_3 = sp_query_post(array(
'post_status'=>'publish',
'posts_per_page'=>-1,
'order'=>'ASC',
'meta_query' => array(
array(
'key'=>'wp-codex',
'value'=>'3'
)
)
), -1);
$arr_post_3_1 = sp_query_post(array(
'post_status'=>'publish',
'posts_per_page'=>-1,
'order'=>'ASC',
'meta_query' => array(
array(
'key'=>'wp-codex',
'value'=>'3-1'
)
)
), -1);
$arr_post_3_2 = sp_query_post(array(
'post_status'=>'publish',
'posts_per_page'=>-1,
'order'=>'ASC',
'meta_query' => array(
array(
'key'=>'wp-codex',
'value'=>'3-2'
)
)
), -1);
$arr_post_3_3 = sp_query_post(array(
'post_status'=>'publish',
'posts_per_page'=>-1,
'order'=>'ASC',
'meta_query' => array(
array(
'key'=>'wp-codex',
'value'=>'3-3'
)
)
), -1);
$arr_post_3_4 = sp_query_post(array(
'post_status'=>'publish',
'posts_per_page'=>-1,
'order'=>'ASC',
'meta_query' => array(
array(
'key'=>'wp-codex',
'value'=>'3-4'
)
)
), -1);
$str1 = '';
foreach($arr_post_3 as $value3):
switch ($value3['post_title']) {
case '常用函数(上)':
$str2 .= '' . $value3['post_title'] . ''. each_post($arr_post_3_1) .'
';
break;
case '常用函数(下)':
$str2 .= '' . $value3['post_title'] . ''. each_post($arr_post_3_2) .'
';
break;
case '常用函数(中)':
$str2 .= '' . $value3['post_title'] . ''. each_post($arr_post_3_3) .'
';
break;
case '模板标签':
$str2 .= '' . $value3['post_title'] . ''. each_post($arr_post_3_4) .'
';
break;
default:
$str2 .= '' . $value3['post_title'] . '';
}
endforeach;
return $title . $time . $str1 . $str2 . $str3;
}
add_shortcode("custom_post_2",'custom_postall_shortcode');
// 查询文章
function sp_query_post( $args, $count ) {
$array = array();
$i = 0;
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
$the_query->the_post();
$i++;
array_push($array,array(
'post_title'=>get_the_title(),
'post_permalink'=>get_permalink(),
'post_time'=>get_the_time('Y-m-d'),
'post_content'=>get_the_content()
));
if( $i === $count ): break ;
endif;
endwhile;
endif;
wp_reset_postdata();
return $array;
}
function set_post_count($string)
{
$arr = array();
$post_arr = query_posts($string);
foreach($post_arr as $val):
array_push($arr, array(
'title'=>get_the_title( $val->ID ),
'permalink'=>get_permalink( $val->ID )
));
endforeach;
wp_reset_postdata();
return $arr;
}
// 循环数组
function each_post($array){
foreach($array as $value){
$str .= '' . $value['post_title'] . '';
}
return $str;
}
/**
* @author smm
* @param $str String
* @param $start int
* @param $width int
* @param $trimmarker String
* @return String
*
**/
if( !function_exists('deel_strimwidth') ){
function deel_strimwidth($str ,$start , $width ,$trimmarker ){
$output = preg_replace('/^(?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$start.'}((?:[\x00-\x7F]|[\xC0-\xFF][\x80-\xBF]+){0,'.$width.'}).*/s','\1',$str);
return $output.$trimmarker;
}
}
/**
* @author smm
* @param $post_id 文章ID
* @param $array 数组
*
**/
if( !function_exists('get_custom_fields_array') ){
function get_custom_fields_array($post_id, $array) {
foreach($array as $arrayValue){
$getCustomArray = get_post_meta($post_id, $arrayValue );
foreach($getCustomArray as $value){
echo '' . $value . '';
}
}
}
}
//控制文章显示数量,依次首页,分类,标签
function iteblog_posts_per_page($query) {
if (is_home()) {
$query->set('posts_per_page', 10);
}
if (is_category()) {
$query->set('posts_per_page', 10);
}
if( is_category(2208) ){
$query->set('posts_per_page',12);
}
if (is_tag()) {
$query->set('posts_per_page', 10);
}
}
add_action('pre_get_posts', 'iteblog_posts_per_page');
function catch_frist_image() {
global $post;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/
/iU', wp_unslash($post->post_content), $matches);
if(empty($output)){
$first_img = "/wp-content/themes/generatepress/images/logoImg.jpg";
}else {
//$first_img = $matches [1][0];
// 获取匹配到的第一张图片路径
$matched_img = trim($matches[1][0]);
// 判断路径是否包含指定的fckeditor表情路径
if (strpos($matched_img, '/modules/fckeditor/fckeditor/editor/images/smiley/msn/') !== false) {
// 包含表情路径,替换为默认图
$first_img = "/wp-content/themes/generatepress/images/logoImg.jpg";
} else {
// 不包含表情路径,使用匹配到的图片路径
$first_img = $matched_img;
}
}
return $first_img;
}
function sp_pagenavi( $range = 4 ) {
global $paged,$wp_query;
if ( !$max_page ) {
$max_page = $wp_query->max_num_pages;
}
if( $max_page >1 ) {
echo "";
if( !$paged ){
$paged = 1;
}
if( $paged != 1 ) {
echo "
首页";
}
previous_posts_link('上一页');
if ( $max_page >$range ) {
if( $paged <$range ) {
for( $i = 1; $i <= ($range +1); $i++ ) {
echo "
$i";
}
}elseif($paged >= ($max_page -ceil(($range/2)))){
for($i = $max_page -$range;$i <= $max_page;$i++){
echo "
$i";
}
}elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
echo "
$i";
}
}
}else{
for($i = 1;$i <= $max_page;$i++){
echo "
$i";
}
}
echo '
...';
next_posts_link('下一页');
if($paged != $max_page){
echo "
尾页";
}
echo "
\n";
}
}
WordPress啦-WordPress主题模板插件分享 WordPress建站安装教程第111页
跳转到内容