WordPress: Conditional Tags:修订间差异
(新页面: == Introduction == The Conditional Tags can be used in your Template files to change what content is displayed and how that content is displayed on a particular page depending on what ''...) |
(→日期网页) |
||
(未显示2个用户的9个中间版本) | |||
第1行: | 第1行: | ||
== | ==介绍== | ||
条件式标签可以用在你的模板文件中,更改显示的内容,并且根据网页匹配什么样的''情况'',更改内容在一个网页上的显示方式。例如,你可能想要在一些文章的上面,在你的博客的主页上显示文章的摘录。拥有<tt>[[#WordPress:The Main Page|is_home()]]</tt>条件式标签,做到这一点,变得非常简单。 | |||
注意这些标签与WordPress的[[WordPress:Template Hierarchy|模板层级]]之间的密切关系。 | |||
== | ==条件关于...== | ||
所有的条件式标签测试看看是否符合某个条件,然后返回TRUE or FALSE。'''不同的标签输出为正确的条件列在了下面'''。能够接受参数的标签非常又名。 | |||
=== | === 主页 === | ||
; <tt>is_home()</tt> : | ; <tt>is_home()</tt> :显示主要的博客网页的时候(WordPress2.1版本与先前的版本处理这个函数的方式不同。请看看 | ||
''' | '''注:'''如果你选择了一个静态的网页作为你的首页(请看看下面的),这个标签会应用到你的"文章 页面"。 | ||
=== | === 首页=== | ||
; <tt>is_front_page()</tt> : | ; <tt>is_front_page()</tt> :显示站点的首页的时候,不管是文章还是[[WordPress:Pages|网页]]。显示主要的博客页面的时候,返回正确的,而且'设置->阅读->显示首页'设置为"你最近的文章",或者当'设置->阅读->显示首页'设置为"一个静态网页" 而且"首页"参数值是当前显示的[[WordPress:Pages|网页]]。注:这个标签是在[[WordPress:Version 2.5|2.5版本]]中添加的。 | ||
=== | === 管理面板 === | ||
; <tt>is_admin()</tt>: | ; <tt>is_admin()</tt>:Dashboard或者管理面板正在显示的时候。 | ||
=== | === 一个单独的文章网页 === | ||
; <tt>is_single()</tt> : | ; <tt>is_single()</tt> :显示任何只有一篇文章的网页的时候。 | ||
; <tt>is_single('17')</tt> :第17篇文章作为一篇单一的文章显示的时候。 | |||
; <tt>comments_open()</tt>: | ; <tt>is_single('Irish Stew')</tt> :拥有标题"土豆洋葱煮肉"的文章作为一篇单一的文章显示的时候。 | ||
; <tt>pings_open()</tt>: | |||
; <tt>is_single('beef-stew')</tt> :拥有Post Slug"beef-stew"的文章作为一篇单一的文章,正在显示的时候。 | |||
; <tt>is_single(array(17,'beef-stew','Irish Stew'))</tt> :当显示的单篇文章是文章ID17,或者''post_name''是"beef-stew",或者''post_title''是"Irish Stew"的时候,返回true。注:array ability是在[[WordPress:Version 2.5|2.5版本]]中添加的。 | |||
=== 任何包含有文章的网页=== | |||
; <tt>comments_open()</tt>:允许对正在[[WordPress:The Loop|WordPress Loop|The Loop]]中进行处理的当前文章,进行评论的时候。 | |||
; <tt>pings_open()</tt>:允许对正在[[WordPress:The Loop|WordPress Loop|The Loop]]中进行处理的当前文章,进行pings的时候。 | |||
=== A PAGE Page === | === A PAGE Page === | ||
这个部分指的是WordPress [[WordPress:Pages|网页]],而不是博客中的任何网页。 | |||
; <tt>is_page()</tt> :正在显示任何[[WordPress:Pages|网页]]的时候。 | |||
; <tt>is_page()</tt> : | ; <tt>is_page('42')</tt> :正在显示[[WordPress:Pages|网页]]42(ID)的时候。 | ||
; <tt>is_page('42')</tt> : | ; <tt>is_page('关于我和Joe')</tt> :显示拥有一个''post_title''"关于我和Joe"的[[WordPress:Pages|网页]]。 | ||
; <tt>is_page(' | ; <tt>is_page('about-me')</tt> :显示拥有''post_name'' (slug) "about-me"的[[WordPress:Pages|网页]]。 | ||
; <tt>is_page( | |||
; <tt>is_page(array(42,'about-me','关于我和 Joe'))</tt> :当显示的[[WordPress:Pages|网页]]是文章ID42,或者''post_name'' "about-me",或者''post_title'' "关于我和 Joe"时,返回true。注:array ability是在[[WordPress:Version 2.5|2.5版本]]中添加的。 | |||
====测试子网页 ==== | |||
还没有<tt>is_subpage()</tt>函数,但是你可以用较少的代码测试这个: | |||
<pre><?php | <pre><?php | ||
// | // 如果你在一个函数的内部,得到 $post | ||
global $post; | global $post; | ||
if (is_page() && $post->post_parent ) { | if (is_page() && $post->post_parent ) { | ||
// | // 这是个子网页 | ||
} else { | } else { | ||
// | // 这不是个子网页 | ||
} | } | ||
?></pre> | ?></pre> | ||
如果你需要测试这是不是个特别的网页还是子网页(例如在以网页为基础的网站上的不同部分呈现不同的标语),从后端得到当前网页ID,然后使用下面的代码: | |||
<pre> | <pre> | ||
<?php | <?php | ||
第79行: | 第83行: | ||
</pre> | </pre> | ||
==== | ====是网页模板==== | ||
从[[WordPress:Version 2.5|2.5版本]]开始,这能够使你决定你是否在网页模板上或者你是不是正在使用一个特别的网页模板。 | |||
; <tt>is_page_template()</tt> : 正在使用[[WordPress:Pages#Page_Templates|网页模板]] 吗? | |||
; <tt>is_page_template('about.php')</tt> : 正在使用 [[WordPress:Pages#Page_Templates|网页模板]] '关于' 吗? 注意与其它的条件不同,如果你想要规定一个特别的网页模板,你需要使用文件名,例如about.php或者my_page_template.php。 | |||
=== 类别网页=== | |||
; <tt>is_category()</tt> :显示任何类别归档网页的时候。 | |||
; <tt>is_category('9')</tt> :显示关于类别9的归档网页的时候。 | |||
; <tt>is_category('发臭的干酪')</tt> :显示名称是"发臭的干酪"的类别的归档网页的时候。 | |||
; <tt> | ; <tt>is_category('蓝纹奶酪')</tt> : 显示类别Slug"蓝纹奶酪"的归档网页的时候。 | ||
; <tt> | ; <tt>is_category(array(9,'blue-cheese','发臭的奶酪'))</tt> : 显示的文章的类别是term_ID 9,或者 ''slug'' "蓝纹奶酪", 或者 ''名称'' "发臭的干酪"的时候,返回正确的。 注: the array ability 是在 [[WordPress:Version 2.5|2.5版本]]中添加的。 | ||
; <tt>in_category('5')</tt> :如果当前文章是'''在'''特别规定的类别id内,返回true。[http://codex.wordpress.org/Template_Tags/in_category 阅读更多的] '''注:'''测试的时候,要确定检查好了拼写,"is" 和"in"是根本的两码事。 | |||
也看看<tt>[[#WordPress:Any Archive Page|is_archive()]]</tt>和[[WordPress:Category Templates|类别模板]]。 | |||
=== 标签网页 === | |||
; <tt>is_tag()</tt> :显示任何标签归档网页的时候。 | |||
; <tt>is_tag('mild')</tt> :显示带有slug'mild'的标签的归档网页的时候。 | |||
; <tt>is_tag(array('sharp','mild','extreme'))</tt> :当显示的标签归档拥有一个slug是"sharp", "mild",或者 "extreme"的时候,返回true。注:array ability是在[[WordPress:Version 2.5|2.5版本]]时,添加的。 | |||
也看看<tt>[[#WordPress:Any Archive Page|is_archive()]]</tt> 和[[WordPress:Tag Templates|标签模板]]。 | |||
=== 作者网页=== | |||
; <tt>is_author()</tt> :显示任何作者网页的时候。 | |||
; <tt>is_author('4')</tt> :显示作者号(ID)4的归档网页的时候。 | |||
; <tt>is_author('Vivian')</tt> :显示作者昵称名为"Vivian"的归档网页的时候。 | |||
; <tt>is_author('john-jones')</tt> :显示作者昵称"john-jones"的归档网页的时候。 | |||
; <tt>is_author(array(4,'john-jones','Vivian'))</tt> :作者是用户ID4或者''user_nicename'' "john-jones", 或者''nickname'' "Vivian"时候的归档网页。注:array ability是在[[WordPress:Version 2.5|2.5版本]]中添加的。 | |||
也看看<tt>[[#WordPress:Any Archive Page|is_archive()]]</tt> 和[[WordPress:Author Templates|作者模板]]。 | |||
=== 日期网页=== | |||
; <tt>is_date()</tt> :显示任何以日期为基础的归档网页的时候(例如,按月,按年,按日或者以时间为基础的归档)。 | |||
; <tt> | ; <tt>is_year()</tt> :显示某一年的归档。 | ||
; <tt>is_month()</tt> :显示某一月的归档。 | |||
; <tt> | ; <tt>is_day()</tt> :显示某一天的归档的时候。 | ||
; <tt>is_time()</tt> :显示某个小时,"某分",或者"某秒"的归档的时候。 | |||
; <tt>is_404()</tt> : | 也看看:<tt>[[#WordPress:Any Archive Page|is_archive()]]</tt>。 | ||
===任何归档网页=== | |||
; <tt>is_archive()</tt> :显示''任何''类型的归档网页的时候。以类别,作者和日期为基础的网页是各种类型的归档。 | |||
=== 搜索结果网页 === | |||
; <tt>is_search()</tt> :显示搜索结果网页归档的时候。 | |||
=== 404 没有找到网页 === | |||
; <tt>is_404()</tt> :在出现"HTTP 404: 没有发现"错误的之后,显示的网页。 | |||
=== A Paged Page === | === A Paged Page === | ||
; <tt>is_paged()</tt> : | ; <tt>is_paged()</tt> :当显示的网页是"paged"。这指的是归档或者主页被分成了几个网页。这''并不''指一篇文章或者[[WordPress:Pages|网页]]使用<tt><!<nowiki></nowiki>--nextpage--></tt> [[WordPress:Writing Posts#Here is a run-down of the quicktag functions:|QuickTag]]把它的内容分成了几个网页。 | ||
=== | === 附属装置=== | ||
; <tt>is_attachment()</tt> :显示文章或者[[WordPress:Pages|网页]]的附属文件的时候。附属文件是一个图像或者通过文章编辑的上传功能上传的其它的文件。附属文件可以在自身的'网页'或者模板上显示。更多的信息,请看看[[WordPress:Using Image and File Attachments|使用图像和附属文件]]。 | |||
=== 联合供稿 === | |||
; <tt>is_feed()</tt> :需要的站点是[[WordPress:Introduction_to_Blogging#Syndication|Syndication]]。通常用户不使用这个标签;由WordPress内部使用,而且插件开发人员也可以得到这个标签。 | |||
=== A Trackback === | |||
; <tt>is_trackback()</tt> :需要的站点,WordPress的hook进入Trackback引擎的时候。通常用户不使用这个标签;由WordPress内部使用,而且插件开发人员也可以得到这个标签。 | |||
=== | === 预览 === | ||
; <tt>is_preview()</tt> :以草稿样式浏览显示的单篇文章的时候。 | |||
=== 有一个摘录 === | |||
=== | ; <tt>!empty($post->post_excerpt)</tt> :没有内置的方法,检查一篇文章是否有摘录,但是你可以用同样的方法使用上传的代码。移除'!',效仿'has_no_excerpt'。 | ||
==Working 例子== | |||
下面是working例子,用来阐述怎样使用这些条件式标签。 | |||
===单篇文章=== | |||
这个例子显示怎样使用<tt>is_single()</tt>,在浏览只有一篇文章的网页的时候,显示一些特别的内容。 | |||
<pre> | <pre> | ||
if (is_single()) | if (is_single()) | ||
{ | { | ||
echo ' | echo '这只是 ' . single_cat_title() 中其中的一篇预言文章。 ' category!'; | ||
} | } | ||
</pre> | </pre> | ||
=== | ===以日期为基础的差异=== | ||
如果有人根据日期浏览我们的站点,我们可以使用不同的颜色,将不同的年份的文章,区分开来。 | |||
<pre> | <pre> | ||
<?php | <?php | ||
// | // 这开始了 Loop | ||
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> | ||
<h2 id="post-<?php the_ID(); ?>"> | <h2 id="post-<?php the_ID(); ?>"> | ||
第187行: | 第202行: | ||
<?php | <?php | ||
// | //我们正在显示以日期为基础的归档吗? | ||
if (is_date()) | if (is_date()) | ||
{ | { | ||
if (date('Y') != get_the_date('Y')) | if (date('Y') != get_the_date('Y')) | ||
{ | { | ||
// | // 这篇文章是前年写的 | ||
// | // 让我们使用 "旧的文章" 级别来设计内容 | ||
echo '<div class="oldentry">'; | echo '<div class="oldentry">'; | ||
} else { | } else { | ||
第201行: | 第216行: | ||
echo '<div class="entry">'; | echo '<div class="entry">'; | ||
} | } | ||
the_content(' | the_content('阅读这篇文章的其余部分 »'); | ||
?> | ?> | ||
</div> | </div> | ||
</pre> | </pre> | ||
=== | ===不同的工具条内容=== | ||
根据读者现在在浏览哪个网页,这个例子会在你的工具条中显示不同的内容。 | |||
<pre> | <pre> | ||
第214行: | 第229行: | ||
<div id="sidebar"> | <div id="sidebar"> | ||
<?php | <?php | ||
// | // 让我们提供与正在显示的文章相关的信息l | ||
if (is_home()) { | if (is_home()) { | ||
// | //我在在主页上,显示最高级别的类别的列表 echo "<ul>"; | ||
wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); | wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); | ||
echo "</ul>"; | echo "</ul>"; | ||
} elseif (is_category()) { | } elseif (is_category()) { | ||
// | // 我们正在查看一个单一的类别,让我们show _all_ the 类别 | ||
echo "<ul>"; | echo "<ul>"; | ||
wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); | wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); | ||
echo "</ul>"; | echo "</ul>"; | ||
} elseif (is_single()) { | } elseif (is_single()) { | ||
// | // 我们在浏览单一的网页,在工具条上不显示任何内容 | ||
} elseif (is_page()) { | } elseif (is_page()) { | ||
// | // 我们在浏览一个静态的网页。哪一个? | ||
if (is_page('About')) { | if (is_page('About')) { | ||
// | //我们的关于网页。 | ||
echo "<p> | echo "<p>这是我的关于网页!</p>"; | ||
} elseif (is_page('Colophon')) { | } elseif (is_page('Colophon')) { | ||
echo "<p> | echo "<p>这是我的最后一个网页, 在WordPress上运行 " 。 bloginfo('version') . "</p>"; | ||
} else { | } else { | ||
// | // 抓取所有的其它网页。 | ||
echo "<p>Vote for Pedro!</p>"; | echo "<p>Vote for Pedro!</p>"; | ||
} | } | ||
} else { | } else { | ||
// | //抓取所有的其它内容 (归档,搜索, 404s, 等等) | ||
echo "<p> | echo "<p>Pedro向你提供保护。</p>"; | ||
} // | } // 伙计,就这麽多! | ||
?> | ?> | ||
<form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> | <form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> | ||
第251行: | 第265行: | ||
</div> | </div> | ||
<! | <!—结束工具条--> | ||
</pre> | </pre> | ||
===有用的404页面=== | |||
访客遇到一个404错误网页的时候,这个网页可能不起作用。使用WordPress,你可以减少404的错误,使得用户和你自己能够使用这个网页,当用户点击一个链接,链接到不存在的网页的时候,可以发送电子邮件。如果你也使用这个方法,不要忘记o | |||
<pre> | <pre> | ||
<p>You | <p>You | ||
第267行: | 第280行: | ||
if (!isset($_SERVER['HTTP_REFERER'])) { | if (!isset($_SERVER['HTTP_REFERER'])) { | ||
#客气地责备导致了这些问题的用户 | |||
echo "tried going to "; # | echo "tried going to "; #开始组成一个输出段落 | ||
$casemessage = " | $casemessage = "所有的内容都没有丢失!"; | ||
} elseif (isset($_SERVER['HTTP_REFERER'])) { | } elseif (isset($_SERVER['HTTP_REFERER'])) { | ||
#这回帮助用户发现他们想要什么,如果发现一个不好的链接,请给我发送电子邮件 | |||
echo " | echo "点击链接到"; #现在消息显示,你点击链接到... | ||
# | #设置一个消息发送给我 | ||
$failuremess = " | $failuremess = "一名用户试着进入$website" | ||
.$_SERVER['REQUEST_URI']." | .$_SERVER['REQUEST_URI']." 而且遇到a 404 (没有发现网页) 错误。 "; | ||
$failuremess .= " | $failuremess .= "如果不是他们的错误,试着解决这个错误。 | ||
They came from ".$_SERVER['HTTP_REFERER']; | They came from ".$_SERVER['HTTP_REFERER']; | ||
mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'], | mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'], | ||
$failuremess, "From: $websitename <noreply@$website>"); #email you about problem | $failuremess, "From: $websitename <noreply@$website>"); #email you about problem | ||
$casemessage = " | $casemessage = "发送一封邮件给管理员,内容关于这个问题。";#set a friendly message | ||
} | } | ||
echo " ".$website.$_SERVER['REQUEST_URI']; ?> | echo " ".$website.$_SERVER['REQUEST_URI']; ?> | ||
and it doesn't exist. <?php echo $casemessage; ?> | and it doesn't exist. <?php echo $casemessage; ?> 你可以点击返回并且再试试搜索你要找的内容: | ||
<?php include(TEMPLATEPATH . "/searchform.php"); ?> | <?php include(TEMPLATEPATH . "/searchform.php"); ?> | ||
</p> | </p> | ||
</pre> | </pre> | ||
=== | ===突出动态的菜单=== | ||
[[WordPress:Dynamic Menu Highlighting|突出动态菜单]]的文章阐述怎样使用条件式标签,突出菜单中的当前的网页。 | |||
==外部资源== | |||
{{ | * 法语翻译: [[WordPress:fr:Marqueurs_conditionnels]] | ||
{{没有 Param 标签 页底文字}} |
2008年8月2日 (六) 18:28的最新版本
介绍[ ]
条件式标签可以用在你的模板文件中,更改显示的内容,并且根据网页匹配什么样的情况,更改内容在一个网页上的显示方式。例如,你可能想要在一些文章的上面,在你的博客的主页上显示文章的摘录。拥有is_home()条件式标签,做到这一点,变得非常简单。
注意这些标签与WordPress的模板层级之间的密切关系。
条件关于...[ ]
所有的条件式标签测试看看是否符合某个条件,然后返回TRUE or FALSE。不同的标签输出为正确的条件列在了下面。能够接受参数的标签非常又名。
主页[ ]
- is_home()
- 显示主要的博客网页的时候(WordPress2.1版本与先前的版本处理这个函数的方式不同。请看看
注:如果你选择了一个静态的网页作为你的首页(请看看下面的),这个标签会应用到你的"文章 页面"。
首页[ ]
- is_front_page()
- 显示站点的首页的时候,不管是文章还是网页。显示主要的博客页面的时候,返回正确的,而且'设置->阅读->显示首页'设置为"你最近的文章",或者当'设置->阅读->显示首页'设置为"一个静态网页" 而且"首页"参数值是当前显示的网页。注:这个标签是在2.5版本中添加的。
管理面板[ ]
- is_admin()
- Dashboard或者管理面板正在显示的时候。
一个单独的文章网页[ ]
- is_single()
- 显示任何只有一篇文章的网页的时候。
- is_single('17')
- 第17篇文章作为一篇单一的文章显示的时候。
- is_single('Irish Stew')
- 拥有标题"土豆洋葱煮肉"的文章作为一篇单一的文章显示的时候。
- is_single('beef-stew')
- 拥有Post Slug"beef-stew"的文章作为一篇单一的文章,正在显示的时候。
- is_single(array(17,'beef-stew','Irish Stew'))
- 当显示的单篇文章是文章ID17,或者post_name是"beef-stew",或者post_title是"Irish Stew"的时候,返回true。注:array ability是在2.5版本中添加的。
任何包含有文章的网页[ ]
- comments_open()
- 允许对正在WordPress Loop|The Loop中进行处理的当前文章,进行评论的时候。
- pings_open()
- 允许对正在WordPress Loop|The Loop中进行处理的当前文章,进行pings的时候。
A PAGE Page[ ]
这个部分指的是WordPress 网页,而不是博客中的任何网页。
- is_page()
- 正在显示任何网页的时候。
- is_page('42')
- 正在显示网页42(ID)的时候。
- is_page('关于我和Joe')
- 显示拥有一个post_title"关于我和Joe"的网页。
- is_page('about-me')
- 显示拥有post_name (slug) "about-me"的网页。
- is_page(array(42,'about-me','关于我和 Joe'))
- 当显示的网页是文章ID42,或者post_name "about-me",或者post_title "关于我和 Joe"时,返回true。注:array ability是在2.5版本中添加的。
测试子网页[ ]
还没有is_subpage()函数,但是你可以用较少的代码测试这个:
<?php // 如果你在一个函数的内部,得到 $post global $post; if (is_page() && $post->post_parent ) { // 这是个子网页 } else { // 这不是个子网页 } ?>
如果你需要测试这是不是个特别的网页还是子网页(例如在以网页为基础的网站上的不同部分呈现不同的标语),从后端得到当前网页ID,然后使用下面的代码:
<?php if (is_page(about) || $post->post_parent=="2") { $bannerimg="home.jpg"; } elseif (is_page(learning) || $post->post_parent=="56") { $bannerimg="teaching.jpg"; } elseif (is_page(admissions) || $post->post_parent=="15") { $bannerimg="admissions.jpg"; } else { $bannerimg="home.jpg" ; // Fall-through } ?>
是网页模板[ ]
从2.5版本开始,这能够使你决定你是否在网页模板上或者你是不是正在使用一个特别的网页模板。
- is_page_template()
- 正在使用网页模板 吗?
- is_page_template('about.php')
- 正在使用 网页模板 '关于' 吗? 注意与其它的条件不同,如果你想要规定一个特别的网页模板,你需要使用文件名,例如about.php或者my_page_template.php。
类别网页[ ]
- is_category()
- 显示任何类别归档网页的时候。
- is_category('9')
- 显示关于类别9的归档网页的时候。
- is_category('发臭的干酪')
- 显示名称是"发臭的干酪"的类别的归档网页的时候。
- is_category('蓝纹奶酪')
- 显示类别Slug"蓝纹奶酪"的归档网页的时候。
- is_category(array(9,'blue-cheese','发臭的奶酪'))
- 显示的文章的类别是term_ID 9,或者 slug "蓝纹奶酪", 或者 名称 "发臭的干酪"的时候,返回正确的。 注: the array ability 是在 2.5版本中添加的。
- in_category('5')
- 如果当前文章是在特别规定的类别id内,返回true。阅读更多的 注:测试的时候,要确定检查好了拼写,"is" 和"in"是根本的两码事。
也看看is_archive()和类别模板。
标签网页[ ]
- is_tag()
- 显示任何标签归档网页的时候。
- is_tag('mild')
- 显示带有slug'mild'的标签的归档网页的时候。
- is_tag(array('sharp','mild','extreme'))
- 当显示的标签归档拥有一个slug是"sharp", "mild",或者 "extreme"的时候,返回true。注:array ability是在2.5版本时,添加的。
也看看is_archive() 和标签模板。
作者网页[ ]
- is_author()
- 显示任何作者网页的时候。
- is_author('4')
- 显示作者号(ID)4的归档网页的时候。
- is_author('Vivian')
- 显示作者昵称名为"Vivian"的归档网页的时候。
- is_author('john-jones')
- 显示作者昵称"john-jones"的归档网页的时候。
- is_author(array(4,'john-jones','Vivian'))
- 作者是用户ID4或者user_nicename "john-jones", 或者nickname "Vivian"时候的归档网页。注:array ability是在2.5版本中添加的。
也看看is_archive() 和作者模板。
日期网页[ ]
- is_date()
- 显示任何以日期为基础的归档网页的时候(例如,按月,按年,按日或者以时间为基础的归档)。
- is_year()
- 显示某一年的归档。
- is_month()
- 显示某一月的归档。
- is_day()
- 显示某一天的归档的时候。
- is_time()
- 显示某个小时,"某分",或者"某秒"的归档的时候。
也看看:is_archive()。
任何归档网页[ ]
- is_archive()
- 显示任何类型的归档网页的时候。以类别,作者和日期为基础的网页是各种类型的归档。
搜索结果网页[ ]
- is_search()
- 显示搜索结果网页归档的时候。
404 没有找到网页[ ]
- is_404()
- 在出现"HTTP 404: 没有发现"错误的之后,显示的网页。
A Paged Page[ ]
=== 附属装置===
- is_attachment()
- 显示文章或者网页的附属文件的时候。附属文件是一个图像或者通过文章编辑的上传功能上传的其它的文件。附属文件可以在自身的'网页'或者模板上显示。更多的信息,请看看使用图像和附属文件。
联合供稿[ ]
- is_feed()
- 需要的站点是Syndication。通常用户不使用这个标签;由WordPress内部使用,而且插件开发人员也可以得到这个标签。
A Trackback[ ]
- is_trackback()
- 需要的站点,WordPress的hook进入Trackback引擎的时候。通常用户不使用这个标签;由WordPress内部使用,而且插件开发人员也可以得到这个标签。
预览[ ]
- is_preview()
- 以草稿样式浏览显示的单篇文章的时候。
有一个摘录[ ]
- !empty($post->post_excerpt)
- 没有内置的方法,检查一篇文章是否有摘录,但是你可以用同样的方法使用上传的代码。移除'!',效仿'has_no_excerpt'。
Working 例子[ ]
下面是working例子,用来阐述怎样使用这些条件式标签。
单篇文章[ ]
这个例子显示怎样使用is_single(),在浏览只有一篇文章的网页的时候,显示一些特别的内容。
if (is_single()) { echo '这只是 ' . single_cat_title() 中其中的一篇预言文章。 ' category!'; }
以日期为基础的差异[ ]
如果有人根据日期浏览我们的站点,我们可以使用不同的颜色,将不同的年份的文章,区分开来。
<?php // 这开始了 Loop if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <h2 id="post-<?php the_ID(); ?>"> <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"> <?php the_title(); ?></a></h2> <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small> <?php //我们正在显示以日期为基础的归档吗? if (is_date()) { if (date('Y') != get_the_date('Y')) { // 这篇文章是前年写的 // 让我们使用 "旧的文章" 级别来设计内容 echo '<div class="oldentry">'; } else { echo '<div class="entry">'; } } else { echo '<div class="entry">'; } the_content('阅读这篇文章的其余部分 »'); ?> </div>
不同的工具条内容[ ]
根据读者现在在浏览哪个网页,这个例子会在你的工具条中显示不同的内容。
<!-- begin sidebar --> <div id="sidebar"> <?php // 让我们提供与正在显示的文章相关的信息l if (is_home()) { //我在在主页上,显示最高级别的类别的列表 echo "<ul>"; wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); echo "</ul>"; } elseif (is_category()) { // 我们正在查看一个单一的类别,让我们show _all_ the 类别 echo "<ul>"; wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); echo "</ul>"; } elseif (is_single()) { // 我们在浏览单一的网页,在工具条上不显示任何内容 } elseif (is_page()) { // 我们在浏览一个静态的网页。哪一个? if (is_page('About')) { //我们的关于网页。 echo "<p>这是我的关于网页!</p>"; } elseif (is_page('Colophon')) { echo "<p>这是我的最后一个网页, 在WordPress上运行 " 。 bloginfo('version') . "</p>"; } else { // 抓取所有的其它网页。 echo "<p>Vote for Pedro!</p>"; } } else { //抓取所有的其它内容 (归档,搜索, 404s, 等等) echo "<p>Pedro向你提供保护。</p>"; } // 伙计,就这麽多! ?> <form id="searchform" method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <div> <input type="text" name="s" id="s" size="15" /> <input type="submit" value="<?php _e('Search'); ?>" /> </div> </form> </div> <!—结束工具条-->
有用的404页面[ ]
访客遇到一个404错误网页的时候,这个网页可能不起作用。使用WordPress,你可以减少404的错误,使得用户和你自己能够使用这个网页,当用户点击一个链接,链接到不存在的网页的时候,可以发送电子邮件。如果你也使用这个方法,不要忘记o
<p>You <?php #some variables for the script to use #if you have some reason to change these, do. but wordpress can handle it $adminemail = get_bloginfo('admin_email'); #the administrator email address, according to wordpress $website = get_bloginfo('url'); #gets your blog's url from wordpress $websitename = get_bloginfo('name'); #sets the blog's name, according to wordpress if (!isset($_SERVER['HTTP_REFERER'])) { #客气地责备导致了这些问题的用户 echo "tried going to "; #开始组成一个输出段落 $casemessage = "所有的内容都没有丢失!"; } elseif (isset($_SERVER['HTTP_REFERER'])) { #这回帮助用户发现他们想要什么,如果发现一个不好的链接,请给我发送电子邮件 echo "点击链接到"; #现在消息显示,你点击链接到... #设置一个消息发送给我 $failuremess = "一名用户试着进入$website" .$_SERVER['REQUEST_URI']." 而且遇到a 404 (没有发现网页) 错误。 "; $failuremess .= "如果不是他们的错误,试着解决这个错误。 They came from ".$_SERVER['HTTP_REFERER']; mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'], $failuremess, "From: $websitename <noreply@$website>"); #email you about problem $casemessage = "发送一封邮件给管理员,内容关于这个问题。";#set a friendly message } echo " ".$website.$_SERVER['REQUEST_URI']; ?> and it doesn't exist. <?php echo $casemessage; ?> 你可以点击返回并且再试试搜索你要找的内容: <?php include(TEMPLATEPATH . "/searchform.php"); ?> </p>
突出动态的菜单[ ]
突出动态菜单的文章阐述怎样使用条件式标签,突出菜单中的当前的网页。