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()函数也能调用最新文章列表, 虽然代码会比较
多一点,但可以更好的控制 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; ?>
补充: 上面提到的query_posts同样可以生成随机文章列表。
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;?>

2.调用 TAB 页签菜单: 

添加 head 头文件:判断后调用 js文件
<?php if ( is_home() ){ ?>
<script type="text/javascript" src="<?php
bloginfo('template_url'); ?>/tab/easytabs.js"></script>
<?php } else { ?>
<script type="text/javascript" src="<?php
bloginfo('template_url'); ?>/tab/easytabs1.js"></script>
<?php } ?>
添加 css 代码到 样式文件内
l <!--tab 页签css-->
l #tab{
l text-align:left;
l position:relative;
l }
l .menu {
l background-color:#ececec;
l color:#272727;
l border-bottom:1px solid #d7d7d7;
l height:23px;
l width:450px;
l vertical-align:middle;
l }
l .menu ul {
l margin:0px;
l padding:0px;
l list-style:none;
l text-align:left;
l }
l .menu li {display:inline;
l line-height:23px;}
l
l .menu li a {color:#000000;
l text-decoration:none;
l padding:4px 5px 6px 5px;
border-left:1px solid #ececec;
l border-right:1px solid #ececec;
l }
l .menu li a.tabactive {border-left:1px solid #d7d7d7;
l border-right:1px solid #d7d7d7;
l background-color:#CCCCCC;
l font-weight:bold;
l position:relative;}
l
l #tabcontent1,#tabcontent2,#tabcontent3,#tabcontent4 {
l border:1px solid #ececec;
l width:450px;
l text-align:left;
l padding:6px 0px;
l font-size:12px;
l margin:2px 0 5px 5px;
l }
l
l
l #container1 #newcomment,#container #newcomment{
l
l position: relative;
l margin-left:30px;
l width:350px;
l }
l
l #container1 #reping,#tabcontent1 #reping{
l position: relative;
l margin-left:30px;
l width:350px;
l }
l
l #container1 #rand li, #tabcontent2 #rand li{
l margin-left:30px;
l padding:0;
l }
l
l <!--tab 页签css-->
添加 tab代码到需要调用的地方
<div id="tab"><h2>最新关注日志及评论</h2>
<!--Start of the Tabmenu 1 -->
<div class="menu">
<ul>
<li><a href="#" onmouseover="easytabs('1', '1');" onfocus="easytabs('1', '1');"
onclick="return false;" title="" id="tablink1">最新评论</a></li>
<li><a href="#" onmouseover="easytabs('1', '2');" onfocus="easytabs('1', '2');"
onclick="return false;" title="" id="tablink2">最受关注文章</a></li>
<li><a href="#" onmouseover="easytabs('1', '3');" onfocus="easytabs('1', '3');"
onclick="return false;" title="" id="tablink3">随机文章</a></li>
</ul>
</div>
<!--Start Tabcontent 1 -->
<div id="tabcontent1">
<div id="newcomment">
<?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;?> </div>
</div>
<!--Start Tabcontent 2-->
<div id="tabcontent2">
<div id="reping">
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM
$wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 10");
foreach ($result as $topten) {
$postid = $topten->ID;
$title = $topten->post_title; $commentcount = $topten->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo
$title ?>"><?php echo $title ?></a></li>
<?php } } ?>
</div>
</div>
<!--End Tabcontent 2 -->
<!--Start Tabcontent 3-->
<div id="tabcontent3">
<div id="rand">
<?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; ?>
</div>
</div><br/><br/>
</div>
保存 js代码到目录下
/*
EASY TABS 1.2 Produced and Copyright by Koller Juergen
www.kollermedia.at | www.austria-media.at
Need Help? http:/www.kollermedia.at/archive/2007/07/10/easy-tabs-12-now-with-autochange
You can use this Script for private and commercial Projects, but just leave the two credit lines, thank
you.*/
//EASY TABS 1.2 - MENU SETTINGS
//Set the id names of your tablink (without a number at the end)设置链接的tab页签名称
var tablink_idname = new Array("tablink")
//Set the id name of your tabcontentarea (without a number at the end)设置tab内容区域id名称
var tabcontent_idname = new Array("tabcontent")
//Set the number of your tabs//设置tab的个数
var tabcount = new Array("2")
//Set the Tab wich should load at start (In this Example:Tab 2 visible on load)
var loadtabs = new Array("1")
//Set the Number of the Menu which should autochange (if you dont't want to have a change menu
set it to 0)
var autochangemenu = 0;
//the speed in seconds when the tabs should change
var changespeed = 3;
//should the autochange stop if the user hover over a tab from the autochangemenu? 0=no 1=yes
var stoponhover = 0;
//END MENU SETTINGS
/*Swich EasyTabs Functions - no need to edit something here*/
function easytabs(menunr, active) {if (menunr == autochangemenu){currenttab=active;}if ((menunr
== autochangemenu)&&(stoponhover==1)) {stop_autochange()} else if ((menunr ==
autochangemenu)&&(stoponhover==0)) {counter=0;}menunr = menunr-1;for (i=1; i <=
tabcount[menunr];
i++){document.getElementById(tablink_idname[menunr]+i).className='tab'+i;document.getEleme
ntById(tabcontent_idname[menunr]+i).style.display =
'none';}document.getElementById(tablink_idname[menunr]+active).className='tab'+active+'
tabactive';document.getElementById(tabcontent_idname[menunr]+active).style.display =
'block';}var timer; counter=0; var totaltabs=tabcount[autochangemenu-1];var
currenttab=loadtabs[autochangemenu-1];function
start_autochange(){counter=counter+1;timer=setTimeout("start_autochange()",1000);if (counter ==
changespeed+1) {currenttab++;if (currenttab>totaltabs)
{currenttab=1}easytabs(autochangemenu,currenttab);restart_autochange();}}function
restart_autochange(){clearTimeout(timer);counter=0;start_autochange();}function
stop_autochange(){clearTimeout(timer);counter=0;}
window.onload=function(){
var menucount=loadtabs.length; var a = 0; var b = 1; do {easytabs(b, loadtabs[a]); a++; b++;}while
(b<=menucount);
if (autochangemenu!=0){start_autochange();} }

3.所有页面的归档

function BX_archive(){
global $month, $wpdb;
$now = current_time('mysql');
$arcresults = $wpdb->get_results("SELECT DISTINCT YEAR(post_date) AS year,
MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts . " WHERE post_date <'" .
$now . "' AND post_status='publish' AND post_type='post' AND post_password='' GROUP BY
YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC");
if ($arcresults) {
foreach ($arcresults as $arcresult) {
$url = get_month_link($arcresult->year, $arcresult->month);
$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
echo get_archives_link($url, $text, '','<h3>','</h3>');
$thismonth = zeroise($arcresult->month,2);
$thisyear = $arcresult->year;
$arcresults2 = $wpdb->get_results("SELECT ID, post_date, post_title, comment_status
FROM " . $wpdb->posts . " WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_status='publish'
AND post_type='post' AND post_password='' ORDER BY post_date DESC");
if ($arcresults2) {
echo "<ul class=\"postspermonth\">\n";
foreach ($arcresults2 as $arcresult2) {
if ($arcresult2->post_date != '0000-00-00 00:00:00') {
$url = get_permalink($arcresult2->ID);
$arc_title = $arcresult2->post_title;
if ($arc_title) $text = strip_tags($arc_title);
else $text = $arcresult2->ID;
echo "<li>".get_archives_link($url, $text, '');
$comments = mysql_query("SELECT * FROM " . $wpdb->comments . "
WHERE comment_approved='1' and comment_post_ID=" . $arcresult2->ID);
$comments_count = mysql_num_rows($comments);
if ($arcresult2->comment_status == "open" OR $comments_count > 0)
echo ' ('.$comments_count.')';
echo "</li>\n";
}
}
echo "</ul>\n";
} } }}

  

Wordpress 常用代码解释的更多相关文章

  1. wordpress常用插件汇总

    WordPress之所以能成为目前最具人气的独立博客程序,除了无数爱好者为它开发的主题外,源源不断的插件支持也是重要的原因之一.wordpress的强大,也是在于无数爱好者源源不断的主题和插件. wo ...

  2. java常用英文解释

    java常用名词解释: OO: object-oriented ,面向对象 OOP:object-oriented programming,面向对象编程 Author:JCC Object:对象JDK ...

  3. javascript常用代码大全

    http://caibaojian.com/288.html    原文链接 jquery选中radio //如果之前有选中的,则把选中radio取消掉 $("#tj_cat .pro_ca ...

  4. css代码添加背景图片常用代码

    css代码添加背景图片常用代码 1 背景颜色 { font-size: 16px; content: ""; display: block; width: 700px; heigh ...

  5. 常用代码页与BOM

    常用代码页:CP437 IBM437 OEM United StatesCP1252表示West European LatinCP932 日本CP949 韩国CP936表示GBK中文编码CP54936 ...

  6. Deep Learning入门视频(下)之关于《感受神经网络》两节中的代码解释

    代码1如下: #深度学习入门课程之感受神经网络(上)代码解释: import numpy as np import matplotlib.pyplot as plt #matplotlib是一个库,p ...

  7. [ARM] Cortex-M Startup.s启动文件相关代码解释

    1. 定义一个段名为CSTACK, 这里: NOROOT表示如何定义的段没有被关联,那么同意会被优化掉,如果不想被优化掉就使用ROOT. 后面的括号里数字表示如下: (1):这个段是2的1次方即2字节 ...

  8. GCD 常用代码

    GCD 常用代码 体验代码 异步执行任务 - (void)gcdDemo1 { // 1. 全局队列 dispatch_queue_t q = dispatch_get_global_queue(0, ...

  9. WordPress 常用数据库SQL查询语句大全

    在使用WordPress的过程中,我们少不了要对数据库进行修改操作,比如,更换域名.修改附件目录.批量修改文章内容等等.这个时候,使用SQL查询语句可以大大简化我们的工作量. 关于如何操作SQL查询语 ...

随机推荐

  1. MySQL 常用字段类型,介绍及其建表使用方法经验分享

    由于工作的公司没有专业的DBA又经常需要建立各种各种的表来满足自己的业务逻辑,所以经常查看MySQL 手册或者谷歌查看相关资料,所以本人就根据我的工作经验和相关资料来介绍一下MySQL各种字段类型及其 ...

  2. XHTML 结构化:使用 XHTML 重构网站

    http://www.w3school.com.cn/xhtml/xhtml_structural_01.asp 我们曾经为本节撰写的标题是:"XHTML : 简单的规则,容易的方针.&qu ...

  3. UML中九种图的理解

    1.用例图. 用例图是用来描述用户需求的,从用户的角度来描述系统的功能,并指出各个执行者.强调谁在使用,系统的执行者是谁. 2.类图. 用来定义系统中的类,包括描述类的结构和类之间的关系.类图的主要作 ...

  4. python快速搭建WebServer

    #!/usr/bin/python import SimpleHTTPServer import SocketServer import os PORT = 7777 WEBDIR = "/ ...

  5. HTML+CSS学习

    1.彻底弄懂CSS盒子模式(DIV布局快速入门) 2.在CSS中,BOX的Padding属性的数值赋予顺序为padding:10px; 四个内边距都是10px padding:5px 10px; 上下 ...

  6. kururu的VHDL学习笔记

    最近开始做课程设计,VHDL设计一个中央空调的控制程序.所以开始学习VHDL,在这篇文章里面记录一些自己的笔记,期望对于同样的初学者有些借鉴意义~ 编写VHDL所需的工具: 那自然很是quartus啦 ...

  7. Generate Parentheses 解答

    Question Given n pairs of parentheses, write a function to generate all combinations of well-formed ...

  8. 【转】10个你必须掌握的超酷VI命令技巧

    摘要:大部分Linux开发者对vi命令相当熟悉,可是遗憾的是,大部分开发者都只能掌握一些最常用的Linux vi命令,下面介绍的10个vi命令虽然很多不为人知,但是在实际应用中又能让你大大提高效率. ...

  9. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

  10. Java有用经验总结--Swing篇

    Java有用经验总结--Swing篇 前言 本文前言部分为我的一些感想,假设你仅仅对本文介绍的Java有用技巧感兴趣,能够跳过前言直接看正文的内容. 本文的写作动机来源于近期接给人家帮忙写的一个小程序 ...