Products
GG网络技术分享 2025-03-18 16:13 0
循环函数
循环开始
<?php query_posts(\'showposts=4&cat=4\');?>
//showposts=4 输出四篇文章, cat=4代表 ID=4,还可以接orderby等参数
<?php if ( have_posts() ) :?>
<?php $i=1; while ( have_posts() ) : the_post();?>
循环内容
<?phpthe_permalink()?> 文章链接
<?phpthe_title()?> 文章标题
<?phpecho catch_that_image()?> 获取文章图片,不能单独用,需配合function函数
<?phpechomb_strimwidth(strip_tags(apply_filters(\'the_content\', $post->post_content)), 0, 100,\"...\");?>
获取文章前100个字符
<?phpthe_content()?> 文章内容
<?php
$views=(int)get_post_meta($post->ID,\'views\',true)
$add_views=(int)update_post_meta($post->ID,\'views\',$views+1);
$after_views=(int)get_post_meta($post->ID,\'views\',true);
if($after_views){
echo $after_views.\'次 |\';
}else{
echo\"没有统计浏览量!\";
}?>
获取文章浏览量
<?php the_time(‘Y-m-d’) ?> 文章发布时间
<?php the_author() ?>文章作者
<?php $author=get_post_meta($post->ID,\'author\',true);
if(!!$author){ echo $author;}
else{the_author()}
?>
循环结束
<?php$i++; endwhile;?>
<?phpendif;?>
<?phpwp_reset_query();?>
获取统计
日志总数:<?php $count_posts = wp_count_posts(); echo $published_posts =$count_posts->publish;?>篇
草稿总数:<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>
页面总数:<?php $count_pages = wp_count_posts(\'page\'); echo $page_posts = $count_pages->publish; ?>
用户总数:<?php $users = $wpdb->get_var(\"SELECT COUNT(ID) FROM $wpdb->users\"); echo $users; ?>
成立时间:<?php echo floor((time()-strtotime(\"2016-8-23\"))/86400); ?> //其中的年月日改成你自己的网站成立时间
分类总数:<?php echo $count_categories = wp_count_terms(\'category\'); ?>个
标签总数:<?php echo $count_tags = wp_count_terms(\'post_tag\'); ?>个
友情链接:<?php $link = $wpdb->get_var(\"SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = \'Y\'\"); echo $link; ?>个
网站运行:<?php echo floor((time()-strtotime(\"2016-7-27\"))/86400); ?>天
最后更新:<?php $last = $wpdb->get_results(\"SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = \'post\' OR post_type = \'page\') AND (post_status = \'publish\' OR post_status = \'private\')\");$last = date(\'Y年n月j日\', strtotime($last[0]->MAX_m));echo $last; ?>
访客量:<?php
$counterFile = \"counter.txt\";
$fp = fopen($counterFile,\"a+\");
$num = fgets($fp,5);
$num += 1;
print \"您是第 \".\"$num\".\" 位访客\";
fclose($fp);
$fpp=fopen($counterFile,\"w\");
fwrite($fpp, $num);
fclose($fpp);
?>
调用页面内容
<?php $my_id = 115; echo get_post($my_id)->post_content; ?>
<?php $my_id = 115; $mypage = get_post($my_id); ?>
<h2><?php echo $mypage->post_title;?></h2> <?php echo substr($mypage->post_content,0,strpos($mypage->post_content, \'<!--more-->\'));?>
Function主函数常用示例
//添加菜单功能
if (function_exists(\'add_theme_support\')) {
add_theme_support(\'nav-menus\');
register_nav_menus( array(
\'primary\'=> __(\'Primary Navigation\',\'primary\'),
\'slide-menus\'=>__(\'Slide Menus\',\'slide\'),
\'footer-menus\'=>__(\'Footer Menus\',\'footer\')
) );
}
//在Function.php里面加入一段获取首张图片的函数:
function catch_that_image() {
global $post, $posts;
$first_img =\'\';
ob_start();
ob_end_clean();
$output = preg_match_all(\'/<img.+src=[\\\'\"]([^\\\'\"]+)[\\\'\"].*>/i\', $post->post_content, $matches);
$first_img = $matches [1] [0];
// if(empty($first_img)){
// $first_img = get_bloginfo(\'template_directory\').\"/img/default.jpg\";
// }
return $first_img;
}
//在Function.php里面加入一段更改登录页面LOGO:
function custom_loginlogo() {
echo\'<style type=\"text/css\">.login h1 a {background-image: url(\'.get_bloginfo(\'template_directory\').\'/img/login_logo.png) !important; }</style>\';
}add_action(\'login_head\',\'custom_loginlogo\');
//在Function.php里面加入一段禁用更新:
add_action(\'admin_menu\',\'wp_hide_nag\');
function wp_hide_nag() {
remove_action(\'admin_notices\',\'update_nag\', 3 );
}
// 添加菜单
function addmenu_function(){
add_menu_page( \'成绩上传\', \'成绩上传\', \'edit_themes\', \'upsql_point\',\'uppoint_function\',\'\',6);
}
//添加面包屑导航
function cmp_breadcrumbs() {
$delimiter =\'>\';// 分隔符
$before =\'<span>\';// 在当前链接前插入
$after =\'</span>\';// 在当前链接后插入
if ( !is_home() && !is_front_page() || is_paged() ) {
echo\'<div>\'.__(\'当前位置:\',\'cmp\');
global $post;
$homeLink = home_url();
echo\' <a href=\"\'. $homeLink .\'\">\'. __(\'未济首页\',\'cmp\') .\'</a> \'. $delimiter .\' \';
if ( is_category() ) {// 分类 存档
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$thisCat = $cat_obj->term_id;
$thisCat = get_category($thisCat);
$parentCat = get_category($thisCat->parent);
if ($thisCat->parent != 0){
$cat_code = get_category_parents($parentCat, TRUE,\' \'. $delimiter .\' \');
echo $cat_code = str_replace (\'<a\',\'<a itemprop=\"breadcrumb\"\', $cat_code );
}
echo $before .\'\'. single_cat_title(\'\', false) .\'\'. $after;
} elseif ( is_day() ) {// 天 存档
echo\'<a href=\"\'. get_year_link(get_the_time(\'Y\')) .\'\">\'. get_the_time(\'Y\') .\'</a> \'. $delimiter .\' \';
echo\'<a href=\"\'. get_month_link(get_the_time(\'Y\'),get_the_time(\'m\')) .\'\">\'. get_the_time(\'F\') .\'</a> \'. $delimiter .\' \';
echo $before . get_the_time(\'d\') . $after;
} elseif ( is_month() ) {// 月 存档
echo\'<a href=\"\'. get_year_link(get_the_time(\'Y\')) .\'\">\'. get_the_time(\'Y\') .\'</a> \'. $delimiter .\' \';
echo $before . get_the_time(\'F\') . $after;
} elseif ( is_year() ) {// 年 存档
echo $before . get_the_time(\'Y\') . $after;
} elseif ( is_single() && !is_attachment() ) {// 文章
if ( get_post_type() !=\'post\') {// 自定义文章类型
$post_type = get_post_type_object(get_post_type());
$slug = $post_type->rewrite;
echo\'<a href=\"\'. $homeLink .\'/\'. $slug[\'slug\'] .\'/\">\'. $post_type->labels->singular_name .\'</a> \'. $delimiter .\' \';
echo $before . get_the_title() . $after;
} else {// 文章 post
$cat = get_the_category(); $cat = $cat[0];
$cat_code = get_category_parents($cat, TRUE,\' \'. $delimiter .\' \');
echo $cat_code = str_replace (\'<a\',\'<a \', $cat_code );
echo $before . get_the_title() . $after;
}
} elseif ( !is_single() && !is_page() && get_post_type() !=\'post\') {
$post_type = get_post_type_object(get_post_type());
echo $before . $post_type->labels->singular_name . $after;
} elseif ( is_attachment() ) {// 附件
$parent = get_post($post->post_parent);
$cat = get_the_category($parent->ID); $cat = $cat[0];
echo\'<a href=\"\'. get_permalink($parent) .\'\">\'. $parent->post_title .\'</a> \'. $delimiter .\' \';
echo $before . get_the_title() . $after;
} elseif ( is_page() && !$post->post_parent ) {// 页面
echo $before . get_the_title() . $after;
} elseif ( is_page() && $post->post_parent ) {// 父级页面
$parent_id = $post->post_parent;
$breadcrumbs = array();
while ($parent_id) {
$page = get_page($parent_id);
$breadcrumbs[] =\'<a href=\"\'. get_permalink($page->ID) .\'\">\'. get_the_title($page->ID) .\'</a>\';
$parent_id = $page->post_parent;
}
$breadcrumbs = array_reverse($breadcrumbs);
foreach ($breadcrumbs as $crumb) echo $crumb .\' \'. $delimiter .\' \';
echo $before . get_the_title() . $after;
} elseif ( is_search() ) {// 搜索结果
echo $before ;
printf( __(\'Search Results for: %s\',\'cmp\'), get_search_query() );
echo $after;
} elseif ( is_tag() ) {//标签 存档
echo $before ;
printf( __(\'Tag Archives: %s\',\'cmp\'), single_tag_title(\'\', false ) );
echo $after;
} elseif ( is_author() ) {// 作者存档
global $author;
$userdata = get_userdata($author);
echo $before ;
printf( __(\'Author Archives: %s\',\'cmp\'), $userdata->display_name );
echo $after;
} elseif ( is_404() ) {// 404 页面
echo $before;
_e(\'Not Found\',\'cmp\');
echo $after;
}
if ( get_query_var(\'paged\') ) {// 分页
if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() )
echo sprintf( __(\'( Page %s )\',\'cmp\'), get_query_var(\'paged\') );
}
echo\'</div>\';
}
}
WordPress函数is_category用于判断当前页面是否为分类页面,如果指定了$category参数,该函数将另外检查是否为指定的分类页面。
is_category( mixed $category = \\\'\\\' )
$category
混合型,默认为空
可以传递分类ID、名称、别名或数组
if(is_category( array( 9, \\\'blue-cheese\\\', \\\'Stinky Cheeses\\\' ) )) {//当前页面为分类页面,且ID为9,或别名为blue-cheese,或名称为Stinky Cheeses} else {//不为指定分类页面时输出}
is_category()函数位于:wp-includes/query.php
相关函数:
Demand feedback