当前位置:首页 »“秋了秋”个人博客 » WP教程 » 整理Wordpress主题制作内置调用标签函数

整理Wordpress主题制作内置调用标签函数

作者:秋了秋 发表时间:2014年12月26日

一个完整的主题“通常”包括以下这些文件:

style.css 样式表文件 index.php 主页文件 single.php 日志单页文件 page.php 页面文件 archvie.php 分类和日期存档页文件 searchform.php 搜索表单文件 search.php 搜索页面文件 comments.php 留言区域文件(包括留言列表和留言框) 404.php 404错误页面 header.php 网页头部文件 sidebar.php 网页侧边栏文件 footer.php 网页底部文件

获取主题目录模版文件

调用wordpress内置插件辅助功能页面:<?php wp_head(); ?> 调用wordpress主题路径下的header.php文件:<?php get_header(); ?> 调用wordpress主题路径下的sidebar.php文件:<?php get_sidebar();?> 调用wordpress主题路径下的footer.php文件:<?php get_footer();?> 调用wordpress主题路径下的style.css文件:<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />

Bloginfo的调用

获取日志或页面标题:<?php wp_title(); ?> 获取博客的Pingback地址:<?php bloginfo('pingback_url'); ?> 获取博客的WordPress版本:<?php bloginfo('version'); ?> 获取博客的RSS2地址:<?php bloginfo('rss2_url'); ?> 获取网站的HTML版本:<?php bloginfo('html_type'); ?> 获取博客的Atom地址:<?php bloginfo('atom_url'); ?> 获取博客页面编码:<?php bloginfo( 'charset' ); ?> 获取博客名称标题:<?php bloginfo('name'); ?> 获取博客描述:<?php bloginfo('description'); ?> 获取博客安装路径(首页地址):<?php echo get_option('home'); ?>或者<?php bloginfo('url'); ?> 获取主题路径:<?php bloginfo('template_directory'); ?>或<?php bloginfo('template_url'); ?>

标题的动态调用设置:

<title><?php if (is_home()||is_search()) { bloginfo('name'); } else { wp_title(''); print " - "; bloginfo('name'); } ?> </title>

列表内容的调用

调用页面的函数:<?php wp_list_pages('sort_column=menu_order&title_li=&depth=2&include='); ?> 调用分类目录的函数:<?php wp_list_categories('title_li=0&orderby=name&show_count=0&depth=2'); ?> 调用最新的10篇文章的函数:<?php wp_get_archives('type=postbypost&limit=10'); ?> 调用100个标签:<?php wp_tag_cloud('number=100&smallest=8&largest=36&separator= '); ?> 调用按日期归档文章:<?php wp_get_archives( 'type=monthly' ); ?> 调用所有分类目录:<?php wp_list_cats('sort_column=name&optioncount=1&hierarchical=0'); ?> 要用友情链接列表:<?php wp_list_bookmarks('title_li=&categorize=0&orderby=rand&limit=24'); ?>

首页模版文章展示主循环:

<?php the_content(); ?> <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> 展示内容区 <?php endwhile; ?> <?php else : ?> <?php endif; ?>

内容区信息细节调用:

文章标题:<?php the_title(); ?> 文章链接:<?php the_permalink() ?> 文章内容:<?php the_content("阅读全文..."); ?> 文章摘要:<?php the_excerpt("阅读全文..."); ?> 输出作者超链接:<?php the_author_posts_link(); ?> 文章发表时间:<?php the_time('F d, Y') ?>或<?php the_time('m-d') ?>或<?php the_time('Y年n月d日') ?> 输出评论数:<?php comments_popup_link('0条评论','1条评论', '%条评论', '','评论关闭'); ?> 调用评论框:<?php comments_template(); ?> 上下页调用:<?php previous_posts_link('<span class="next">«上一页 </span>') ?><?php next_posts_link('<span class="previous">下一页» </span>') ?> 显示一篇日志的留言链接:<?php comments_popup_link(); ?> 显示一篇日志或页面的所属分类:<?php the_category(', ') ?> 显示一篇日志或页面的作者:<?php the_author(); ?> 显示一篇日志或页面的ID:<?php the_ID(); ?> 显示一篇日志或页面的编辑链接:<?php edit_post_link(); ?> 显示Blogroll中的链接(友情链接):<?php get_links_list(); ?> comments.php文件的内容:<?php comments_template(); ?> 显示一份博客的页面列表:<?php wp_list_pages(); ?> 显示一份博客的分类列表:<?php wp_list_cats(); ?> 下一篇日志的URL地址:<?php next_post_link(' %link ') ?> 上一篇日志的URL地址:<?php previous_post_link('%link') ?> 调用日历:<?php get_calendar(); ?> 显示一份博客的日期存档列表:<?php wp_get_archives() ?> <?php posts_nav_link(); ?> 显示较新日志链接(上一页)和较旧日志链接(下一页)

其它的一些附加代码功能

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

调用网站信息:1、日志总数:

<? php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish;?>

2、草稿数目:

<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>

3、评论总数:

<?php echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?>

4、成立时间:

<?php echo floor((time()-strtotime("2015-8-18″))/86400); ?>

5、标签总数:

<?php echo $count_tags = wp_count_terms('post_tag'); ?>

6、页面总数:

<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?>

7、分类总数:

<?php echo $count_categories = wp_count_terms('category'); ?>

8、链接总数:

<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?>

9、用户总数:

<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?>

判断语句:

< ?php if ( is_home()) {怎样怎样}< ?php endif;? >或者< php if(is_home()):? >这里写你想显示的内容,包括函数。< ? php endif;? >。is_home()表示首页,还有很多,列举: is_single()//判断是否是具体文章的页面 is_single('2')//判断是否是具体文章(id=2)的页面 is_single('秋叶是谁?')//判断是否是具体文章(标题判断)的页面 is_single('qiuye')//判断是否是具体文章(slug判断)的页面 comments_open()//是否留言开启 pings_open()//是否开启ping is_page()//是否是页面 is_page('42')//id判断,即是否是id为42的页面 is_page('投稿')//判断标题 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') //下面是通过不同的判断实现以年、月、日、时间等方式来显示归档 is_date()//判断是否是时间页面 is_year()//判断是否是年份页面 is_month()//判断是否是月份页面 is_day()//判断是否是日期页面 is_time()//判断是否是具体时间页面 is_archive()//判断当前是否是归档页面 is_search()//判断是否是搜索 is_404()//判断页面是否404

调用10篇随机文章函数:

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

调用最新文章

<?php get_archives('postbypost', 10); ?> (显示10篇最新更新文章)或者:
<?php wp_get_archives('type=postbypost&limit=20&format=custom'); ?>

调用热门文章:

<?php $post_num = 10; // 调用数量 $args = array( 'post_password' => '', 'post_status' => 'publish', // 只展示公开的文章. 'post__not_in' => array($post->ID),//过滤当前文章 'caller_get_posts' => 1, // 不要置顶文章. 'orderby' => 'comment_count', // 按评论数排序. 'posts_per_page' => $post_num); $query_posts = new WP_Query(); $query_posts->query($args); while( $query_posts->have_posts() ) { $query_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"> <?php the_title(); ?></a></li> <?php } wp_reset_query();?>

调用最新留言:

<?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 <ul><li>".strip_tags($comment-&gt;comment_author) .":"."<a>ID)."#comment-" . $comment-&gt;comment_ID . "" title="on " . $comment-&gt;post_title . ""&gt;" . strip_tags($comment-&gt;com_excerpt) ."</a></li></ul> "; } $output .= $post_HTML; echo $output;?>

调用相关文章:

<?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="&lt;?php the_permalink() ?&gt;" rel="bookmark" title="&lt;?php the_title_attribute(); ?&gt;">&lt;?php the_title();?&gt; &lt;?php comments_number(' ','(1)','(%)'); ?&gt;</a></li> <?php endwhile;}} wp_reset_query(); ?>

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; ?> 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 .= " <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;?>

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 .= " <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; ?>

非插件调用评论表情

<?php function wp_smilies() { global $wpsmiliestrans; if ( !get_option('use_smilies') or (empty($wpsmiliestrans))) return; $smilies = array_unique($wpsmiliestrans); $link="; foreach ($smilies as $key => $smile) { $file = get_bloginfo('wpurl').'/wp-includes/images/smilies/'.$smile; $value = " ".$key." "; $img = "<img src="{$file}" alt="{$smile}" />"; $imglink = htmlspecialchars($img); $link .= "<a href="#commentform" title="{$smile}" onclick="document.getElementByIdx_x('comment').value += '{$value}'">{$img}</a>&nbsp;";} echo '<div class="wp_smilies">'.$link.'</div>';} ?> <?php wp_smilies();?>
0
文章作者: “秋了秋”个人博客,本站鼓励原创。
转载请注明本文地址:http://netblog.cn/blog/229.html
目录: WP教程标签: wp主题制作 9235次阅读

请求播放音乐,请点击播放

登 录
点击获取验证码
还没账号?点击这里