wordpress高级教程

ė 5,184 6 0

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
globa$wpdb;
$sq= "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_ur,
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($sq);
$output = $pre_HTM;
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_HTM;
echo $output;?>

调用TAB 页签菜单:
添加head 头文件:判断后调用js文件
<?php if ( is_home() ){ ?>
<script type="text/javascript" src="<?php
bloginfo('template_ur'); ?>/tab/easytabs.js"></script>
<?php } else { ?>
<script type="text/javascript" src="<?php
bloginfo('template_ur'); ?>/tab/easytabs1.js"></script>
<?php } ?>
添加css 代码到 样式文件内
<!--tab 页签css-->
#tab{
text-align:left;
position:relative;
}
.menu {
background-color:#ececec;
color:#272727;
border-bottom:1px solid #d7d7d7;
height:23px;
width:450px;
vertica-align:middle;
}
.menu u{
margin:0px;
padding:0px;
list-style:none;
text-align:left;
}
.menu li {display:inline;
line-height:23px;}
.menu li a {color:#000000;
text-decoration:none;
padding:4px 5px 6px 5px;
border-left:1px solid #ececec;
border-right:1px solid #ececec;
}
.menu li a.tabactive {border-left:1px solid #d7d7d7;
border-right:1px solid #d7d7d7;
background-color:#CCCCCC;
font-weight:bold;
position:relative;}
#tabcontent1,#tabcontent2,#tabcontent3,#tabcontent4 {
border:1px solid #ececec;
width:450px;
text-align:left;
padding:6px 0px;
font-size:12px;
margin:2px 0 5px 5px;
}
#container1 #newcomment,#container #newcomment{
position: relative;
margin-left:30px;
width:350px;
}
#container1 #reping,#tabcontent1 #reping{
position: relative;
margin-left:30px;
width:350px;
}
#container1 #rand li, #tabcontent2 #rand li{
margin-left:30px;
padding:0;
}
<!--tab 页签css-->
添加tab代码到需要调用的地方
<div id="tab"><h2>最新关注日志及评论</h2>
<!--Start of the Tabmenu 1 -->

<div class="menu">
<u>
<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>
</u>
</div>
<!--Start Tabcontent 1 -->
<div id="tabcontent1">
<div id="newcomment">
<?php
globa$wpdb;
$sq= "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_ur,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($sq); $output = $pre_HTM; 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_HTM;
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 commerciaProjects, 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();} }
所有页面的归档:
function BX_archive(){
globa$month, $wpdb;
$now = current_time('mysq');
$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) {
$ur= get_month_link($arcresult->year, $arcresult->month);

$text = sprintf('%s %d', $month[zeroise($arcresult->month,2)], $arcresult->year);
echo get_archives_link($ur, $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 "<uclass=\"postspermonth\">\n";
foreach ($arcresults2 as $arcresult2) {
if ($arcresult2->post_date != '0000-00-00 00:00:00') {
$ur= 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($ur, $text, '');
$comments = mysq_query("SELECT * FROM " . $wpdb->comments . "
WHERE comment_approved='1' and comment_post_ID=" . $arcresult2->ID);
$comments_count = mysq_num_rows($comments);
if ($arcresult2->comment_status == "open" OR $comments_count > 0)
echo '&nbsp;('.$comments_count.')';
echo "</li>\n";
}
}
echo "</u>\n";
} } }}

Wordpress 主题中常用代码总结:
1. 在Wordpress主题中显示最热文章的PHP代码
2. wordpress主题–相关文章代码
<?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 $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> (<?php echo $commentcount ?>)</li>
<?php } } ?>
<div id="newpost">
<u>
<?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() ?>" re="bookmark"
title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?>
</a> </li>
<?php endwhile; } else {echo 'not realate post';} }else {echo 'not
realate post';} ?>
3. WordPress主题使用PHP代码输出最新文章
4. 最新评论:
<?php
globa$wpdb;
$sq= "SELECT DISTINCT ID, post_title, post_password,
comment_ID,comment_post_ID, comment_author, comment_date_gmt,
comment_approved,comment_type,comment_author_ur,
SUBSTRING(comment_content,1,30) AScom_excerpt FROM $wpdb->comments LEFT OUTER
JOIN $wpdb->posts ON($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHEREcomment_approved = '1' AND comment_type = " AND post_password = " ORDERBY
comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sq);
$output = $pre_HTM;
foreach ($comments as $comment) {
$output.= "\n<li>". "<a href=\"" .get_permalink($comment->ID)."#comment-" .
$comment->comment_ID ."\" title=\"on
".$comment->post_title ."\">".strip_tags($comment->comment_author)."</a>" .
": ".strip_tags($comment->com_excerpt)."</li>";
}
$output .= $post_HTM;
echo $output;
?>
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit=7 . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>
<u>
<li><a href="<?php the_permalink() ?>" re="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
</u>
<?php endif; endwhile; ?>
5. wordpress主题–相关文章代码
基本sq是这样:$sq= “SELECT p.ID, p.post_title, p.post_date, p.comment_count,
count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships
t_r, $wpdb->posts p WHERE t_t.taxonomy =’post_tag’ AND t_t.term_taxonomy_id =
t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND
p.ID != $post->ID AND p.post_status = ‘publish’ GROUP BY t_r.object_id ORDER BY cnt
DESC, p.post_date_gmt DESC LIMIT $limit;”;
一点一点的解释:term_taxonomy 、term_relationship、posts 这三张表存的什么我不多说,
网上一般都可以查到,维基百科貌似都有。把他们连起来,做个sq,注意 group by 以及
limit,这样就可以提取结果了$related_post = $wpdb->get_results($sq);
之前要把$taglist 做好,利用wp_get_post_tags($post->ID);可以将该篇文章的的tag 取到
一个数组中,然后再链接就可以了
最后怎么处理输出就看个人爱好了,这个功能我写的大概也就十几行,我比较喜欢简短的说,
呵呵。
function related_post($limit = 10) {
globa$wpdb, $post;
$tags = wp_get_post_tags($post->ID);
$taglist = "'" . $tags[0]->term_id. “‘”;
$tagcount = count($tags);
if ($tagcount > 1) {
for ($i = 1; $i < $tagcount; $i++) {
$taglist .= “, ‘”.$tags[$i]->term_id.”‘”;
}
}
$sq= “SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt
FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE
t_t.taxonomy =’post_tag’ AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id =
p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = ‘publish’ GROUP
BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC LIMIT $limit;”;
$related_post = $wpdb->get_results($sq);
$output = “”;
foreach ($related_post as $tmp){
$output .= 这个就看个人爱好了
}
if($output == “”)$output = “No Related Post Yet”;
echo $output;
}

WordPress 简单企业模板,企业网站完成
先在WordPress 生成一分类,叫[基本资料], 然后分别写[公司简介]和[联系方
式]等,再增加分类[新闻中心] .
首页模板里调用query_posts(’p=1) , 完成公司简介,1 是公司简介这文章ID
index.php 代码:
<?php
get_header();
?>
<div id=”page” class=”clearfloat”>
<div class=”clearfloat”>
<div id=”header”>
<div class=”mainheader”><a href=”<?php bloginfo(’ur’); ?>”><?php
bloginfo(’name’); ?> </a></div>
<div class=”description”></div>
</div>
<uid=”nav” class=”clearfloat”>
<li><a href=”<?php bloginfo(’ur’); ?>”>HOME</a></li>
</u>
<div id=”content”>
<br />
<img src=”<?php bloginfo(’template_ur’); ?>/banner-page.jpg” border=0
alt=”Banner” />
<?php globa$firstx; ?>
<?php $firstx=0;?>
<?php if ( is_home()) { ?>
<?php $firstx=1;?>
<?php query_posts(’p=1); ?>
<?php } ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<div class=”post” id=”post-<?php the_ID(); ?>”>
<div class=singlepage><a href=”<?php the_permalink() ?>”
re=”bookmark”><?php the_title(); ?></a></div>

<div class=”entry”>
<?php the_content(); ?>
<?php
// get_the_title()
// get_the_content()
// $ss=get_the_content();
// echo “<p> ********************<p>”;
// echo $ss;
// echo “<p>*********************<p>”;
?>
<!–yes kao–>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e(’No Data’); ?></p>
<?php endif; ?>
<p>
</div>
<?php get_sidebar(); ?>
</div>
<?php get_footer(); ?>
在sibebar.php 写死了部分导航,其实可以用其它方式的,以下是sibebar.php
的代码:
<div id=”sidebar”>
<div class=”sideblock”>
<h3>导航</h3>
<u>
<li><a href=’<?php bloginfo(’ur’); ?>’>首页</a></li>
<?php wp_list_cats (’include=3); ?>
</u>
<br />
</div>
<div class=”sideblock”>

------------------------------------------------第39 页/共60 页-------------------------------------------------
<h3>新闻中心</h3>
<u>
<?php query_posts(’cat=3&showposts=5); ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<li><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></li>
<?php endwhile; else: ?>
<li><?php _e(’No Data’); ?></li>
<?php endif; ?>
</u>
<br />
</div>
<?php globa$firstx; ?>
<?php if ($firstx==1){ ?>
<div class=”sideblock”>
<h3>联系我们</h3>
<u>
<?php query_posts(’p=12); ?>
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: ?>
<li><?php _e(’No Data’); ?></li>
<?php endif; ?>
</u>
<br />
</div>
<?php } ?>
</div>

WordPress 搭建企业网站
在看该文档之前需要对WordPress 主题模板的层次架构有一定的了解。简单介绍如图:
更详细的WordPress 主题模板的结构层次请见:wordpress 主题结构层次图
一.WordPress 搭建中小企业网站思路
WordPress 是基于文章的博客程序,而企业或产品网站也是基于一篇篇的文章,所以用
WordPress 搭建企业网站是可行的。用WordPress 建中小企业站模板的思路如下:
1) 大分类+子分类:
首先,根据需求明确企业网站的整体结构,需要几大类的内容,如分为1 产品介绍,2 技术
中心,3 关于我们,4 试用购买,这些内容可以放在网站的主菜单上。
然后,细化企业网站结构,既明确子分类,如:

由上图可以清晰的了解网站的结构,上图中 表示该文章分类的id 号,因为
WordPress 本身有一个未分类和链接占用分类的1、2 两个id 号,我们只能从3 开始,图
中id 为3、4、5、6 的几个分类为大分类;而7、8、9、10 为3 的子分类,其他父子分类
关系也如此。子分类可作为网站内页的side 里的内容。值得一提的是, 表示文章,
“关于公司”、“联系我们”、“合作伙伴”三个文章是直接属于大分类5,这个网站的“关于我们”
内页side 里既有分类又有文章,如何解决?下边会有介绍。现在整体的大思路应该很明确
了就是 大分类+子分类。
2) 让每个大分类内页的side 里显示子分类:
如图:
这是我们要的效果 , 因为我们每个大分类下面都要显示各自的子分类 , 所以在
sidebar.php 里的分类列表只写 <?php wp_list_cats(’child_of=5); ?> 已经不能满足我们
的需求。因此需要写一个if 判断语句:
这个if 语句帮我们解决了在每个大分类下面调用其各自子分类。
<?php $post = $wp_query->post;
if ( in_category(’ 3 ‘)|in_category(’7)|in_category(’ 8 ‘) |in_category(’ 9 ‘)|in_category(’ 10
‘)) <! — 如果文章属于大分类3 及其所有子分类7、8、9、10 时–>
{ wp_list_cats(’child_of= 3 ‘);} <! — 则显示分类3 的子分类–>
elseif ( in_category(’ 4 ‘) |in_category(’ 11 ‘)|in_category(’ 12 ‘) |in_category(’ 13
‘)|in_category(’ 14 ‘))
{ wp_list_cats(’child_of= 4 ‘);}
elseif ( in_category( 5 ‘)| in_category(’1 5 ‘)|in_category(’1 6 ‘))
{ wp_list_cats(’child_of= 5 ‘);}
elseif ( in_category(’ 6 ‘))
{ wp_list_cats(’child_of= 6 ‘);}
?>
但上面我们所提到的“关于我们”的side 里是“文章+子分类”的列表,所以以上代码也不能完
全满足,需要在 { wp_list_cats(’child_of= 5 ‘);} 里加入已发文章的静态链接。如下:
{
?>
<li><a href=”<?php bloginfo(’ur’); ?>/about/company/” title=”关于公司”>关于公司</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/about/contact/” title=”联系我们”>联系我们</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/about/partner/” title=”合作伙伴”>合作伙伴</a></li>
<?
wp_list_cats(’child_of= 5 ‘);
}
现在side 里“文章+子分类”的列表,我们很好的解决了。由此启发,当我们在点击各大分类
下side 列表不一定都用分类,因为如果是分类的话,右边调用的是 category .php, 显示
分类页。如果每个大分类下都是这样显示分类列表的话,看起来还是很像blog,不像企业
网站。所以我们的网站构架要改一下了,如图:
正如图中所示有两种方法实现:一是写一篇该子分类概述的文章,文章里有其他该子分类的
链接,把概述文章的链接用刚才所述的方法写在sidebar.php 里;另外一种是写一个子分类
概述page 页面把链接写在sidebar 里。这里我们采取第一种方法。因为用page 的话会导
致页面过多不好管理,而且page 的ur地址为: http://域名/页面缩略名/ 的结构,这样就
不好识别是哪个分类的内容了。改完的代码如下:
<u>

<?php $post = $wp_query->post;
if (in_category(’ 3 ‘)|in_category(’7)|in_category(’ 8 ‘) |in_category(’ 9 ‘)|in_category(’ 10
‘))
{
?>
<li><a href=”<?php bloginfo(’ur’); ?>/products/products-al/production/” title=”产品
概括”>产品概括</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/products/features/functions-and-features/” tite=”功能特点”>功能特点</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/products/strength/strength/” title=”核心优势”>
核心优势</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/products/application/report-appdeployment/”
title=”部署与运用开发”>部署与运用开发</a></li>
<?
}
elseif (in_category(’4)|in_category(’1 1 ‘)|in_category(’1 2 ‘)|in_category(’ 13
‘)|in_category(’ 14 ‘))
{ wp_list_cats(’child_of=4);} / * 该分类是“技术中心”,需要它为子分类,就直接调
用其子分类 */
?>
elseif ( in_category(’5)|in_category(’ 15 ‘)|in_category(’1 6 ‘))
{
?>
<li><a href=”<?php bloginfo(’ur’); ?>/about/company/” title=”关于公司”>关于公司
</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/about/contact/” title=”联系我们”>联系我们
</a></li>

<li><a href=”<?php bloginfo(’ur’); ?>/about/partner/” title=”合作伙伴”>合作伙伴
</a></li>
<li><a href=”<?php bloginfo(’ur’); ?> /about /news/” title=”企业新闻”>企业新闻
</a></li>
<li><a href=”<?php bloginfo(’ur’); ?> /about /cases/” title=”成功案例”>成功案例
</a></li>
<?
}
elseif ( in_category(’6))
{
?>
<li><a href=”<?php bloginfo(’ur’); ?>/about/download/” title=”下载试用”>下载试用
</a></li>
<li><a href=”<?php bloginfo(’ur’); ?>/about/purchase/” title=”购买产品”>购买产品
</a></li>
<?
}
</u>
至此,sidebar 里的子分类(文章)列表已经改完。
接着,需要在子分类上标上该大分类的名称,这一点我仍是采用了if 语句将其写死。
<h3>
<?php
if ( in_category(’ 3 ‘)|in_category(’7)|in_category(’ 8 ‘) |in_category(’ 9 ‘)|in_category(’ 10
‘) ) { ?>产品介绍<?php }
elseif ( (in_category(’4)|in_category(’1 1 ‘)|in_category(’1 2 ‘)|in_category(’ 13
‘)|in_category(’ 14 ‘) ) { ?>技术中心<?php }
elseif ( in_category(’5)|in_category(’ 15 ‘)|in_category(’1 6 ‘) ) { ?>关于我们<?php }
elseif ( in_category(’6)) { ?>试用购买<?php }
?>
</h3>
至此,sidebar 里的title 名称也写完了。sidebar 里的子分类(文章)模块内容也全都完成,
如若需要可在下面再写点热点链接之类的内容。
最后,single.php、category.php、 archive.php (企业网站它的用处不大)、index.php 等
都可以调用该sidebar.php 了。
3) 菜单调用大分类
Sidebar 做好了,下面就让菜单上显示调用的大分类,调用
<?php wp_list_cats(’include=3,4,5,6); ?> 这句函数即可。include=ID:是我们调用的大类,
多个大类的ID 号用英文“,”隔开。
4) 修改category.php
现在菜单上列出的是大分类调用,这样每个大分类下面就都会显示分类列表,但如果现在有
的大分类不想显示分类列表,而显示该分类的第一篇文章,就需要来修改category.php 文
件了。思路和修改sidebar 差不多。最初 category.php 的结构应该如下:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id= ” content ” >
/*这里是调用分类的内容*/
</div>
<?php get_footer() ?>
现在因为需要点击大分类3、5、6 时,页面上展示的是文章而非分类列表,所以在 <div id= ”
content ” ></div>里添加一个if 语句:

把以前的 /*这里是调用分类的内容*/ 的
中。
而需要在结构中添加的是大分类3、5、6 中 这一块的内容,代码如下
<?php query_posts(’ name=production ‘); ?>
<div class=”post” >
<?php while (have_posts()) : the_post(); ?>
<h2><a href=”<?php the_permalink() ?>” class=”homesidetext1 title=”<?php the
_title() ?>”> <?php the_title() ?> </a> </h2>
<div class=”entry”>
<?php the_content(’Read the rest of this entry »’); ?>
</div>
<?php endwhile;?>
</div>
query_posts ()是获取文章,其内参数可用 ’ p=文章ID ’ 或 ’ name=文章缩略名 ’ 来调
用想放在大分类下首页的文章;
<h2></h2>里是文章名称;
the_content(); 是调用文章内容。
这样就可以在菜单中列出的大分类上点击,出来想要的文章或分类列表了。
以上几点就是企业级网站全站用WordPress 搭建的大致思路。
二.注意事项
1、 这样的主题模板要基于WordPress 的数据库,因为在写side 时用到了分类的id 号。
2、 正是因为第一条,这样的WordPress 主题模板没有通用的,只能因企业而异。
3、 永久链接最好用 /%category%/%postname%/ (分类+文章缩略名)的形式,这样ur地址会看起来更有结构性,看起来也更像企业或产品网站。
创建漂亮的 WordPress 日期按钮
我不知道我的那些日期按钮怎么样,但似乎你们都很喜欢。是的,今天,我们将
来学习如何为WordPress创建一个漂亮的日期按钮。总共有三步简单的步骤:
第一步:输入代码
(请不要复制粘贴,输入到模板中)
<div class=”post-date”>
<div class=”month”><?php the_time(’M') ?></div>
<div class=”day”><?php the_time(’d') ?></div>
</div>
为了符合结构格式,在源代码中应该如下图所示:
这些代码的意义解释:
<div class=”post-date”> – 我正在建立一个盒模型或者一个被命名
为”post-date”的DIV(层标签)。当然我也可以将它命名为
“little-potato.”。
<div class=”post-month”> – 在post-month盒模型中,建立一个现实月份
的盒模型。
<?php the_time(’M’) ?> – 这里我使用了php函数the_time(’M'),用来
调用缩写的月份值。例如:六月June的英文缩写为JUN。但在我的日期按钮例
子中,并没有使用JUN代替JUNE,这是因为我并没有调用函数缩写版本的代码。
</div> – 结束月份的盒模型。(即关闭DIV标签-译者注)
<div class=”day”> – 建立日期盒模型。

<?php the_time(’d’) ?> – 使用PHP函数the_time(’d'),用来调用日期
值,会显示成两位数值。例如:03
</div> – 结束日期盒模型。
</div> – 没有任何内容可以添加到我的日期按钮中了,所以这里我也结束(关
闭)了post-date盒模型(或者称DIV标签),因为我必须按顺序关闭所有打开
的标签。
(更多关于date的显示选项,请访问php.net)
第二步:建立背景图片
你需要某些设计类软件用来设计你的日期按钮的背景图片,如Photoshop,下图
是我的日期按钮背景图片:
第三步:为其创建样式
这一步需要你了解一点CSS的知识,样式决定于个人的偏好,因为你的背景图案
会与我的不同。但,下面是些基本的内容:
.post-date{
float: left;
display: inline;
margin: 0 10px 0 0;
background: ur(images/date_button_template.gif) no-repeat;
}
图片的名称应该命名为date_button_template.gif。images/是存放图片的文件
夹。如果你存储你的图片在其他文件夹,更改images/为你的图片文件夹。
创建动态的置顶文章
我们的博客,MaxPower,需要某些文章比其他文章保持在最上面更长的时间。我
们发现我们撰写的稿件像慢慢陷入沼泽,不能同其他的稿件区分开来。例如,在
这个博客上我们会写关于所有东西的内容,但重点放在我们的旅行经验。问题来
了,我们如何才能在首页重点显示这些经验,但并不会忽略所有其他的稿件?这
是我们想出的:
从wordpress help forums关于多重循环的各种讨论中让我想到一个使用两次循
环的方法。一个用来查询并仅显示某个分类中的文章。另一个用来和擦寻在所有
分类中的所有的文章。第一个查询是从the codex中复制来的,很容易理解:
$my_query = new WP_Query('category_name=frontpage&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;
上述代码的意思是,变量my_query赋值为查询分类名为frontpage的所有文章
的结果,并仅得到一篇文章。关键点在最后部分,给变量do_not_duplicate赋
值为返回的单独文章的ID值。我们在下一步需要这个值。
下面一段代码是很重要很标准的,可以在许多主题中找到。它能获取所有日志:
if (have_posts()) : while (have_posts()) : the_post();
//do stuff like format each post
endwhile;
这段代码获取所有的日志,并根据//do stuff这行(依赖于模板)的格式显示。
如果仅仅这样编写代码,就会发生你所选的文章(置顶文章)被显示两次的情况
(一次在置顶区域,另一次则是在所有其他文章区域)。显示这样并不好,所以
这里要使用到do_not_duplicate函数了。添加到一个if语句中阻止制定文章显
示两次。下面是代码:
if (have_posts()) : while (have_posts()) : the_post();
if( $post->ID == $do_not_duplicate )
continue;
//do stuff like format each post
endwhile;

代码中的第二个if语句的意思是,获取所有日志,且当发现一篇文章的ID值与
do_not_duplicate的值相等时中断执行(continue语句),否则显示根据//do
stuff这行的格式显示所有其他的文章。记住,do_not_duplicate函数值所代表
的文章已经被显示过了。
就这样当你把所有内容放到一起得到了什么?一个动态的置顶文章!且最棒的部
分是当置顶文章被新的一篇代替,之前的文章会在下面所有文章的区域显示出来
(取决于你选择了多少文章显示以及文章的发表频率)。

查询wordpress 博客资料的SQ命令
查询2006年所有的文章的SQL命令:
SELECT COUNT(*)
FROM `wp_posts`
WHERE post_date >= ‘2006-01-01′
AND post_date < '2007-01-01'
AND post_status = ‘publish’
查询2006年所有评论的SQL命令:
SELECT COUNT(*)
FROM `wp_comments`
WHERE comment_date >= ‘2006-01-01′
AND comment_date < '2007-01-01'
AND comment_approved = ‘1′
查询2006每篇文章平均字数的SQL命令:
SELECT AVG(LENGTH(post_content))
FROM `wp_posts`
WHERE post_date >= ‘2006-01-01′
AND post_date < '2007-01-01'
AND post_status = ‘publish’
查询2006年文章总字数的SQL命令:
SELECT SUM(LENGTH(post_content))
FROM `wp_posts`
WHERE post_date >= ‘2006-01-01′
AND post_date < '2007-01-01'
AND post_status = ‘publish’

给WP 添加Ctr+Enter 回复快捷键
首先是在模版的comments.php 底部加一段JS代码
<script type="text/javascript">
document.getElementById("comment").onkeydown = function (moz_ev)
{
var ev = nul;
if (window.event){
ev = window.event;
}else{
ev = moz_ev;
}
if (ev != nul&& ev.ctrlKey && ev.keyCode == 13)
{
document.getElementById("submit").click();
}
}
</script>
最后是找到这段,并修改:
<input name="提交" type="submit" class="button" id="submit"
tabindex="5" value="提交Ctr+Enter" />
方法二:(万戈)
在 comments.php 中找到以下代码:
<TEXTAREA id=comment tabIndex=4 name=comment rows=10 cols=105
jQuery1250561977609="48"></TEXTAREA>
修改为:
<TEXTAREA id=comment
onkeydown="if(event.ctrlKey&&event.keyCode==13){document.getElementById('submit')
.click();return false};" tabIndex=4 name=comment rows=10 cols=105></TEXTAREA>
总结:相比来说,第二种方法更简单,却实现了相同的效果。
需要注意的是:这里的id一定要和js里的id值一样不然无法处理。

WordPress 的10 个杀手级Hack 技巧

来源-小民 - 通过投稿渠道发表
  • 灵魂发问:谁告诉你我国防空洞位置了吗?怕是99%中国人不知道,我也不知道,为什么,因为没告诉咱们啊!怕是很多当官的都不晓得吧!所以别老想着打这个那个,真打起来,你躲都不知道躲哪?最好的位置是到没有军事区,机场等落后位置,因为人家炸这些位置不仅没有利益还会被国际谴责。 --- 2025/03/08
主机推荐
留言区 (5,184阅嗐!还没有留言,我要占位
:网友留言区仅供网友表达其个人观点,并不表明栖息邦最终立场。



管理员登录
注册

提示:点击上下面区域关闭

字号调整

View My Stats
空位 空位
本次加载耗时:1.317s