百度未收录

WordPress建站模版页面调用函数功能代码大全

收集WordPress开发时必备的一些函数是必须的,大家都知道WP更新的节奏也算比较快的啦经常推出新函数替换之前使用的函数,新函数的性能肯定要比旧函数好如果我们开始开发的时候就使用的旧函数那等回过头来修改也是一大麻烦事,所以我把我收集整理的版本以便于自己和大家查看参考。

模板文件说明

style.css : 样式表Css文件
index.php : 主页模板
archive.php : 文章归档页模板(非必需)
category.php :分类页面模板
404.php : Not Found 错误页模板
comments.php : 评论模板
footer.php : Footer模板
header.php : Header模板
sidebar.php : 侧栏模板
page.php:页面(Page)模板
single.php : 文章页(Post)模板
searchform.php : 搜索表单模板
search.php : 搜索结果模板
tag.php:标签页面模板
image.php:图片附件页面模板(非必需)
rtl.css:RTL样式表(非必需)
上面的模板文件有些事非必要文件,大家可以根据主题的功能按需选用,但是大部分文件都是方便主题代码循环利用的,可以减少开发时间提供代码的再编辑性能,方便二次开发。

模板调用函数

<?php get_header(); ?> : 调用Header模板
<?php get_sidebar(); ?> : 调用Sidebar模板
<?php get_footer(); ?> : 调用Footer模板
<?php comments_template(); ?> : 调用评论模板
<?php get_search_form(); ?>:调用搜索表单

主题内置函数调用

Header部分常用到的PHP函数

<?php bloginfo('name'); ?> : 博客名称(Title)
<?php bloginfo('stylesheet_url'); ?> : CSS文件路径
<?php bloginfo('pingback_url'); ?> : PingBack Url
<?php bloginfo('template_url'); ?> : 模板文件路径
<?php bloginfo('version'); ?> : WordPress版本
<?php bloginfo('atom_url'); ?> : Atom Url
<?php bloginfo('rss2_url'); ?> : RSS 2.o Url
<?php bloginfo('url'); ?> : 博客 Url
<?php bloginfo('html_type'); ?> : 博客网页Html类型
<?php bloginfo('charset'); ?> : 博客网页编码
注:<meta charset="<?php bloginfo( 'charset' ); ?>">实例应用
<?php bloginfo('description'); ?> : 博客描述
<?php wp_register(); ?> : 显示注册链接
<?php wp_loginout(); ?> : 显示登录/注销链接

Single部分常用到的PHP函数

<?php the_title(); ?> : 内容页(Post/Page)标题
<?php the_permalink() ?> : 内容页(Post/Page) Url
<?php the_content(); ?>:调用文章正文内容
<?php the_category(', ') ?> : 特定内容页(Post/Page)所属Category
<?php the_author(); ?> : 作者
<?php the_ID(); ?> : 特定内容页(Post/Page) ID
<?php edit_post_link(); ?> : 如果用户已登录并具有权限,显示编辑链接
<?php next_post_link(' %link '); ?> : 下一篇文章链接
<?php previous_post_link('%link'); ?> : 上一篇文章链接
<?php the_tags('','',''); ?>:调用文章标签
<?php the_time('Y /n/j G:i'); ?>:调用文章发布日期
<?php comments_popup_link( '沙发', '评论 1 条', '评论 % 条' ); ?>:调用文章的评论链接和评论数量
<?php posts_nav_link(); ?> : 导航,显示上一篇/下一篇文章链接
<?php wp_link_pages(); ?>调用文章分页导航
<!–next page–> : 将当前内容分页
<!–more–> : 将当前内容截断,以不在主页/目录页显示全部内容

WordPress 主题模版 PHP代码

<?php the_content(); ?> 日志内容
<?php if(have_posts()) : ?> 确认是否有日志
<?php while(have_posts()) : the_post(); ?> 如果有,则显示全部日志
<?php endwhile; ?> 结束PHPhanshu”while”
<?php endif; ?> 结束PHPhanshu”if”
<?php get_header(); ?> header.php文件的内容
<?php get_sidebar(); ?> sidebar.php文件的内容
<?php get_footer(); ?> footer.php文件的内容
<?php the_time(’m-d-y’) ?> 显示格式为”02-19-08″的日期
<?php comments_popup_link(); ?> 显示一篇日志的留言链接
<?php the_title(); ?> 显示一篇日志或页面的标题
<?php the_permalink() ?> 显示一篇日志或页面的永久链接/URL地址
<?php the_category(’, ‘) ?> 显示一篇日志或页面的所属分类
<?php the_author(); ?> 显示一篇日志或页面的作者
<?php the_ID(); ?> 显示一篇日志或页面的ID
<?php edit_post_link(); ?> 显示一篇日志或页面的编辑链接
<?php get_links_list(); ?> 显示Blogroll中的链接
<?php comments_template(); ?> comments.php文件的内容
<?php wp_list_pages(); ?> 显示一份博客的页面列表
<?php wp_list_cats(); ?> 显示一份博客的分类列表
<?php next_post_link(’ %link ‘) ?> 下一篇日志的URL地址
<?php previous_post_link(’%link’) ?> 上一篇日志的URL地址
<?php get_calendar(); ?> 调用日历
<?php wp_get_archives() ?> 显示一份博客的日期存档列表
<?php posts_nav_link(); ?> 显示较新日志链接(上一页)和较旧日志链接(下一页)
<?php bloginfo(’description’); ?> 显示博客的描述信息

其它的一些WordPress模板代码

/%postname%/ 显示博客的自定义永久链接
<?php the_search_query(); ?> 搜索表单的值
<?php _e(’Message’); ?> 打印输出信息
<?php wp_register(); ?> 显示注册链接
<?php wp_loginout(); ?> 显示登入/登出链接
<!–next page–> 在日志或页面中插入分页
<!–more–> 截断日志
<?php wp_meta(); ?> 显示管理员的相关控制信息
<?php timer_stop(1); ?> 显示载入页面的时间
<?php echo get_num_queries(); ?> 显示载入页面查询

Fonter常用函数

<?php timer_stop(1); ?> : 网页加载时间(秒)
<?php echo get_num_queries(); ?> : 网页加载查询量
<?php echo get_option( 'zh_cn_l10n_icp_num' );?>:调用网站备案号码

会员用户信息获取函数

<?php global $current_user;
wp_get_current_user();//2016年3月更新
echo(‘Username: ‘ . $current_user->user_login . “ ”);//登陆用户名
echo(‘User email: ‘ . $current_user->user_email . “ ”);//用户邮箱
echo(‘User level: ‘ . $current_user->user_level . “ ”);//用户ID
echo(‘User first name: ‘ . $current_user->user_firstname . “ ”);//用户名字
echo(‘User last name: ‘ . $current_user->user_lastname . “ ”);//用户姓氏
echo(‘User display name: ‘ . $current_user->display_name . “ ”);//用户昵称
echo(‘User ID: ‘ . $current_user->ID . “ ”);//用户ID
?>

判断命令

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页是否以多页显示

is_single() //判断是否是具体文章的页面
is_single(’2′) //判断是否是具体文章(id=2)的页面
is_single(’Beef Stew’) //判断是否是具体文章(标题判断)的页面
is_single(’beef-stew’) //判断是否是具体文章(slug判断)的页面
comments_open() //是否留言开启
pings_open() //是否开启ping
is_page(’42′) //id判断,即是否是id为42的页面
is_page(’About Me’) //判断标题
is_page(’about-me’) //slug判断
is_category() //是否是分类
is_category(’6′) //id判断,即是否是id为6的分类
is_category(’Cheeses’) //分类title判断
is_category(’cheeses’) //分类 slug判断
in_category(’5′) //判断当前的文章是否属于分类5
is_author() //将所有的作者的页面显示出来
is_author(’1337′) //显示author number为1337的页面
is_author(’Elite Hacker’) //通过昵称来显示当前作者的页面
is_author(’elite-hacker’) //下面是通过不同的判断实现以年、月、日、时间等方式来显示归档

判断语句应用案列

1、全php应用一

<?php if ( is_single() ) ://判断语句这里还可以写成if ( !is_single() ) :else输出和现在想反
the_title( '<h1 class="entry-title">', '</h1>' );
else ://不符合判断内容时输出
the_title( );
endif;//判断结束
?>
2、全php应用范例二

<?php if ( is_single() ) {//判断语句这里还可以写成if ( !is_single() )else输出和现在想反
the_title( '<h1 class="entry-title">', '</h1>' );
}else {//不符合判断内容时输出
the_title( );
}; ?>
3、php和html混合

<?php if ( is_single() ) { ?>//判断语句这里还可以写成if ( !is_single() )else输出和现在想反
如果是文章也输出的内容

<?php }else { ?>
非文章页输出这里的内容

<?php }; ?>
4、判断是否为登陆用户

<?php if ( is_user_logged_in() ) { ?>
登陆用户显示的内容

<?php global $user_ID; if( $user_ID && current_user_can('level_10') ) : ?>
入过是管理员显示的内容

<?php endif; ?>
<a href="<?php echo wp_logout_url( home_url(add_query_arg(array(),$wp->request)) ); ?>">退出</a>//退出账号按钮
<?php } else{?>
//非登陆用户显示的内容

<?php $url_this='http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; ?>
//调用当前页面链接,用于登陆之后可以返回登陆页

<a href="<?php echo wp_login_url($url_this); ?>">登陆</a>
//登陆按钮

<a href="<?php echo get_option('siteurl'); ?>/wp-login.php?action=register">注册</a>
//注册按钮

<?php }?>//判断结束

循环语句使用

<?php if(have_posts()) : ?> : 检查是否存在Post/Page

<?php while(have_posts()) : the_post(); ?> : 如果存在Post/Page则予以显示

这里可以放置文章的列表模板,文章输出时使用

<?php endwhile; ?> : While 结束
<?php else: ?> :没有文章时输出的内容
<?php endif; ?> : If 结束

网站统计调用

//文章总数
<?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 { $num_comments = wp_count_comments(); echo $num_comments->total_comments; }?>
<?php echo $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->comments”);?>
//浏览总数
<?php if(function_exists('get_totalviews')) { get_totalviews(); } ?>
//总访问量
<?php echo ''.nd_get_all_view().''; ?>
//成立时间
<?php echo floor((time()-strtotime("2022-09-23 13:57:00"))/86400); ?>
//标签总数
<?php echo $count_tags = wp_count_terms(‘post_tag’); ?>
//页面总数
<?php $count_pages = wp_count_posts(‘page’); echo $page_posts = $count_pages->publish; ?>
//分类总数
<?php echo $count_categories = wp_count_terms(‘category’); ?>
//链接总数
<?php $link = $wpdb->get_var(“SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = ‘Y’”); echo $link; ?>
//用户总数
<?php global $wpdb;$users = $wpdb->get_var("select count(id) from $wpdb->users");echo "$users" ?>
//最后更新
<?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; ?>

调用今天24小时(一周)内的文章发布数量
先将下面的代码加到 functions.php 中

function get_posts_count_from_last_24h($post_type ='post') { //24为24小时,一周为168小时
global $wpdb;
$numposts = $wpdb->get_var(
$wpdb->prepare(
"SELECT COUNT(ID) ".
"FROM {$wpdb->posts} ".
"WHERE ".
"post_status='publish' ".
"AND post_type= %s ".
"AND post_date> %s",
$post_type, date('Y-m-d H:i:s', strtotime('-24 hours')) //24为24小时,一周为168小时
)
);
return $numposts;
}

使用以下的代码调用文章数量,既放到你想要显示的地方。

<?php echo get_posts_count_from_last_24h(); ?> //24小时,一周请改为168

1.调用最新文章

WordPress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:

<?php get_archives('postbypost', 10); ?> (显示10篇最新更新文章)

或者

<?php wp_get_archives(‘type=postbypost&limit=20&format=custom’); ?>

后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可 以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)

补充: 通过WP的query_posts()hanshu也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。

2.调用随机文章

<?php $rand_posts = get_posts('numberposts=10&orderby=rand'); foreach( $rand_posts as $post ) : ?>
<!--下面是你想自定义的Loop-->
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

3.wordpress调用最新留言

下面是我之前在一个WordPress主题中代到的最新留言代码,具体也记不得是哪个主题了。该代码直接调用数据库显示一份最新留言。其中 LIMIT 10限制留言显示数量。绿色部份则是每条留言的输出样式。

<?php global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,30) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10"; $comments = $wpdb->get_results($sql); $output = $pre_HTML;  foreach ($comments as $comment) { $output .= "n<li>".strip_tags($comment->comment_author) .":" . " <a href="" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "" title="on " . $comment->post_title . "">" . strip_tags($comment->com_excerpt) ."</a></li>"; }   $output .= $post_HTML; echo $output;?>

4.wordpress调用相关文章

在文章页显示相关文章

<?php $tags = wp_get_post_tags($post->ID); if ($tags) { $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>10, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?></a></li> <?php endwhile; } } wp_reset_query(); ?>

5.wordpress调用指定分类的文章

<?php $posts = get_posts( "category=4&numberposts=10" ); ?> <?php if( $posts ) : ?> <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?> <li> <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> <?php endif; ?>

6.wordpress去评论者链接的评论输出

<?php global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,14) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10"; $comments = $wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) { $output .= "\n<li>".strip_tags($comment->comment_author) .":" . " <a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"on " . $comment->post_title . "\">" . strip_tags($comment->com_excerpt) ."</a></li>"; } $output .= $post_HTML; echo $output;?>

7.wordpress调用含gravatar头像的评论输出

<?php global $wpdb; $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,10) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = '' AND comment_author != '郑 永' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10"; $comments = $wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) { $output .= "\n<li>".get_avatar(get_comment_author_email('comment_author_email'), 18). " <a href=\"" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "\" title=\"" . $comment->post_title . " 上的评论\">". strip_tags($comment->comment_author) .": ". strip_tags($comment->com_excerpt) ."</a></li>"; } $output .= $post_HTML; $output = convert_smilies($output); echo $output; ?>

上面代码把comment_author的值改成你的ID,18是头像大小,10是评论数量。

8.wordpress非插件同步twitter

<?php require_once (ABSPATH . WPINC . ‘/class-feed.php’); $feed = new SimplePie(); $feed->set_feed_url(‘http://feeds.feedburner.com/agting′); $feed->set_file_class(‘WP_SimplePie_File’); $feed->set_cache_duration(600); $feed->init(); $feed->handle_content_type(); $items = $feed->get_items(0,1); foreach($items as $item) { echo ‘<a target=”_blank” rel=”external nofollow” title=”Follow Me on Twitter” href=”http://twitter.com/agting″>@狐狸库</a>: ‘.$item->get_description(); } ?>

代码中的agting改成你的twitter用户名,狐狸库改成你的名字。

另一种调用方法需要你的空间是国外主机:

<?php // Your twitter username. $username = "wange1228"; // Prefix - some text you want displayed before your latest tweet. // (HTML is OK, but be sure to escape quotes with backslashes: for example href=\"link.html\") // Suffix - some text you want display after your latest tweet. (Same rules as the prefix.) $suffix = ""; $feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=1"; function parse_feed($feed) { $stepOne = explode("<content type=\"html\">", $feed); $stepTwo = explode("</content>", $stepOne[1]); $tweet = $stepTwo[0]; $tweet = str_replace("<", "<", $tweet); $tweet = str_replace(">", ">", $tweet); return $tweet; } $twitterFeed = file_get_contents($feed); echo stripslashes($prefix) . parse_feed($twitterFeed) . stripslashes($suffix); ?>

总结了一下这个方法的特点:

  1. 非插件!
  2. 不用验证用户名和密码,也就是说你可以指定调用任何一个人的 tweet!
  3. 可以自定义 tweet 信息后显示的文字,就是 $suffix = “”; 这里!
  4. 只能调用最新的一条 tweet,刚好满足我的需求。
  5. 大概只有国外空间才能使用!(经我验证,确实如此)

关于 WordPress 的常用函数就先整理到这里,有需要的朋友可以评论收藏一下,方便下次查看,此文档长期更新收纳最新的wp函数。

温馨提示: 本文最后更新于2023/04/17 12:54:24。若文章内容或图片失效,请 留言联系站长反馈!
!
也想出现在这里? 联系我们
创意广告
© 版权声明
THE END
点赞0赞赏 分享
评论 共1条

请登录后发表评论

    请登录后查看评论内容