欢迎您光临爱永设计官网!
电话图标 服务热线/微信:13436537174 QQ咨询:276583799

新闻资讯

news

WordPress文章调用代码函数

发表日期:2012-07-27 文章作者:爱永设计  浏览次数:14414 次

WordPress文章调用代码函数是玩转wp博客必备的知识,尤其是对那些想要修改博客主题样式的朋友。了解wordpress文章调用代码函数知识,你就可以很轻松的实现一些你想要的功能。本文接上篇文章,我们一起来学一下wordpress文章调用代码。
1. wordpress调用最新文章
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()函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。
2. wordpress调用随机文章
<?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调用最新留言
该代码直接调用数据库显示一份最新留言。其中 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调用网站统计大全
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(“2008-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; ?>
10、最后更新:
<?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; ?>
9.wordpress判断语句
is_single()
判断是否是具体文章的页面
is_single(’2′)
判断是否是具体文章(id=2)的页面
is_single(’Beef Stew’)
判断是否是具体文章(标题判断)的页面
is_single(’beef-stew’)
判断是否是具体文章(slug判断)的页面
comments_open()
是否留言开启
pings_open()
是否开启ping
is_page()
是否是页面
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’)
下面是通过不同的判断实现以年、月、日、时间等方式来显示归档
is_date()
is_year()
is_month()
is_day()
is_time()
判断当前是否是归档页面
is_archive()
判断是否是搜索
is_search()
判断页面是否404
is_404()
判断是否翻页,比如你当前的blog是http://domain.com 显示http://domain.com?paged=2的时候,这个判断将返 回真,通过这个函数可以配合is_home来控制某些只能在首页显示的界面,
例如:
<?php if(is_single()):?>
//这里写你想显示的内容,包括函数
<?php endif;?>
或者:
<?php if(is_home() && !is_paged() ):?>
//这里写你想显示的内容,包括函数
<?php endif;?>
10.wordpress 非插件调用评论表情
<!–smilies–>      
<?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.getElementById(‘comment’).value += ‘{$value}’\”>{$img}</a>&nbsp;”;
}
echo ‘<div>’.$link.'</div>’;
}
?>
<?php wp_smilies();?>
<!–smilies—>
将以上代码复制到 comments.php 中合适的位置

将文章分享到..
相关资讯
最新主题模板
随机新闻
最新网站案例
  • 更多 +我们能做什么

    致力于互联网品牌建设与网络营销,专业领域包括网站建设、网站模板、移动互联网营销、wordpress平台开发等,服务范围涵盖基础的域名服务、主机 服务;企业邮箱、云服务器、网络营销等应用服务,为不同类型的客户提供良好的互联网应用定制解决方案,帮助客户在新的全球化互联网环境中保持优势。

  • 更多 +网站模板优势

  • 更多 +关于爱永设计

    爱永设计工作室一直致力于品牌精美的网页设计、网页制作DIV+CSS布局、JS效果、精美网站模板、标志设计、专业仿站, 低廉的价格,真诚的服务,我们拥有全国各地的客户群体和各行业的成功案例。以一流的服务,出色的网页设计和制作能力,认真严谨的工作态度为客户提供优质满意的服务。期待与您的合作!

Copyright © 2012 - 2024 aysheji.com All Rights Reserved 爱永设计 版权所有
邮箱:aysheji@163.com 在线客服:276583799 模板演示地址:www.aymoban.com 备案号:京ICP备13060102号-3
服务内容: 网页设计 网站建设 网站制作 网站模板 婚庆网站模板 摄影网站 手机网站制作 自适应网站制作