Wordpress 常用代码解释

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 .= "
<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 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 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 350px; 
l } 
l 
l #container1 #reping,#tabcontent1 #reping{ 
l position: relative; 
l margin-left:30px; 
l 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 .= 
"
<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">
"; 
 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>
"; 
 } 
 } 
 echo "</ul>
"; 
 } } }} 

  

原文地址:https://www.cnblogs.com/qiandu/p/4034854.html