站长百科 | 数字化技能提升教程 数字化时代生存宝典
首页
数字化百科
电子书
▼
建站程序
开发
服务器
办公软件
开发教程
▼
服务器教程
软件使用教程
运营教程
热门电子书
▼
CSS教程
WordPress教程
导航
程序频道
推广频道
网赚频道
人物频道
网站程序
网页制作
云计算
服务器
CMS
论坛
网店
虚拟主机
cPanel
网址导航
WIKI使用导航
WIKI首页
热点词条
最新资讯
网站程序
站长人物
页面分类
使用帮助
编辑测试
创建条目
网站地图
站长百科导航
站长百科
主机侦探
IDCtalk云说
跨境电商导航
WordPress啦
站长专题
网站推广
网站程序
网站赚钱
虚拟主机
cPanel
网址导航专题
云计算
微博营销
虚拟主机管理系统
开放平台
WIKI程序与应用
美国十大主机
编辑“
WordPress:Conditional Tags
”
人物百科
|
营销百科
|
网赚百科
|
站长工具
|
网站程序
|
域名主机
|
互联网公司
|
分类索引
Xxf3325
(
讨论
|
贡献
)
2008年6月25日 (三) 10:26的版本
(新页面: == 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 ''...)
(差异) ←上一版本 |
最后版本
(
差异
) |
下一版本→
(
差异
)
跳转至:
导航
、
搜索
警告:您正在编辑的是本页面的旧版本。
如果您发布该更改,该版本后的所有更改都会丢失。
警告:
您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您
登录
或
创建
一个账户,您的编辑将归属于您的用户名,且将享受其他好处。
反垃圾检查。
不要
加入这个!
== 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 ''conditions'' that page matches. For example, you might want to display a snippet of text above the series of posts, but only on the main page of your blog. With the <tt>[[WordPress:#The Main Page|is_home()]]</tt> Conditional Tag, that task is made easy. Note the close relation these tags have to WordPress' [[WordPress:Template Hierarchy]]. ==The Conditions For...== All of the Conditional Tags test to see whether a certain condition is met, and then returns either TRUE or FALSE. '''The conditions under which various tags output TRUE is listed below'''. Those tags which can accept parameters are so noted. === The Main Page === ; <tt>is_home()</tt> : When the main blog page is being displayed. (Wordpress 2.1 handles this function differently than prior versions. See [http://codex.wordpress.org/Pages#Using_a_Page_as_the_Front_Page_.28version_2.1.2B_only.29 static Front Page].) '''Note:''' If you select a static Page as your frontpage (see below), this tag will be applied to your "posts page". === The Front Page === ; <tt>is_front_page()</tt> : When it is the front of the site displayed, whether it is posts or a [[WordPress:Pages|Page]]. Returns true when the main blog page is being displayed and the 'Settings->Reading->Front page displays' is set to "Your latest posts", '''or''' when 'Settings->Reading->Front page displays' is set to "A static page" and the "Front Page" value is the current [[WordPress:Pages|Page]] being displayed. Note: this tag was added at [[WordPress:Version 2.5]]. === The Administration Panels === ; <tt>is_admin()</tt>: When the Dashboard or the administration panels are being displayed. === A Single Post Page === ; <tt>is_single()</tt> : When any single Post page is being displayed. ; <tt>is_single('17')</tt> : When Post 17 is being displayed as a single Post. ; <tt>is_single('Irish Stew')</tt> : When the Post with Title "Irish Stew" is being displayed as a single Post. ; <tt>is_single('beef-stew')</tt> : When the Post with Post Slug "beef-stew" is being displayed as a single Post. ; <tt>is_single(array(17,'beef-stew','Irish Stew'))</tt> : Returns true when the single post being displayed is either post ID 17, or the ''post_name'' is "beef-stew", or the ''post_title'' is "Irish Stew". Note: the array ability was added at [[WordPress:Version 2.5]]. === Any Page Containing Posts === ; <tt>comments_open()</tt>: When comments are allowed for the current Post being processed in the [[WordPress:The Loop|WordPress Loop]]. ; <tt>pings_open()</tt>: When pings are allowed for the current Post being processed in the [[WordPress:The Loop|WordPress Loop]]. === A PAGE Page === This section refers to WordPress [[WordPress:Pages]], not any generic webpage from your blog. ; <tt>is_page()</tt> : When any [[WordPress:Pages|Page]] is being displayed. ; <tt>is_page('42')</tt> : When [[WordPress:Pages|Page]] 42 (ID) is being displayed. ; <tt>is_page('About Me And Joe')</tt> : When the [[WordPress:Pages|Page]] with a ''post_title'' of "About Me And Joe" is being displayed. ; <tt>is_page('about-me')</tt> : When the [[WordPress:Pages|Page]] with a ''post_name'' (slug) of "about-me" is being displayed. ; <tt>is_page(array(42,'about-me','About Me And Joe'))</tt> : Returns true when the [[WordPress:Pages|Pages]] displayed is either post ID 42, or ''post_name'' "about-me", or ''post_title'' "About Me And Joe". Note: the array ability was added at [[WordPress:Version 2.5]]. ==== Testing for sub-Pages ==== There is no <tt>is_subpage()</tt> function yet, but you can test this with a little code: <pre><?php // Get $post if you're inside a function global $post; if (is_page() && $post->post_parent ) { // This is a subpage } else { // This is not a subpage } ?></pre> If you need to test whether this is a particular page OR a child of that page (e.g. to present a different banner on different sections of a page-based site), get the parent page IDs from the back-end, then use code like this: <pre> <?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 } ?> </pre> ====Is a Page Template==== Beginning with [[WordPress:Version 2.5]] this allows you to determine whether or not you are in a page template or if a specific page template is being used. ; <tt>is_page_template()</tt> : Is a [[WordPress:Pages#Page_Templates|Page Template]] being used? ; <tt>is_page_template('about.php')</tt> : Is [[WordPress:Pages#Page_Templates|Page Template]] 'about' being used? Note that unlike with other conditionals, if you want to specify a particular Page Template, you need to use the filename, such as about.php or my_page_template.php. === A Category Page === ; <tt>is_category()</tt> : When any Category archive page is being displayed. ; <tt>is_category('9')</tt> : When the archive page for Category 9 is being displayed. ; <tt>is_category('Stinky Cheeses')</tt> : When the archive page for the Category with Name "Stinky Cheeses" is being displayed. ; <tt>is_category('blue-cheese')</tt> : When the archive page for the Category with Category Slug "blue-cheese" is being displayed. ; <tt>is_category(array(9,'blue-cheese','Stinky Cheeses'))</tt> : Returns true when the category of posts being displayed is either term_ID 9, or ''slug'' "blue-cheese", or ''name'' "Stinky Cheeses". Note: the array ability was added at [[WordPress:Version 2.5]]. ; <tt>in_category('5')</tt> : Returns true if the current post is '''in''' the specified category id. [http://codex.wordpress.org/Template_Tags/in_category read more] '''Note:''' Be sure to check your spelling when testing, "is" and "in" are a big difference. See also <tt>[[WordPress:#Any Archive Page|is_archive()]]</tt> and [[WordPress:Category Templates]]. === A Tag Page === ; <tt>is_tag()</tt> : When any Tag archive page is being displayed. ; <tt>is_tag('mild')</tt> : When the archive page for tag with the slug of 'mild' is being displayed. ; <tt>is_tag(array('sharp','mild','extreme'))</tt> : Returns true when the tag archive being displayed has a slug of either "sharp", "mild", or "extreme". Note: the array ability was added at [[WordPress:Version 2.5]]. See also <tt>[[WordPress:#Any Archive Page|is_archive()]]</tt> and [[WordPress:Tag Templates]]. === An Author Page === ; <tt>is_author()</tt> : When any Author page is being displayed. ; <tt>is_author('4')</tt> : When the archive page for Author number (ID) 4 is being displayed. ; <tt>is_author('Vivian')</tt> : When the archive page for the Author with Nickname "Vivian" is being displayed. ; <tt>is_author('john-jones')</tt> : When the archive page for the Author with Nicename "john-jones" is being displayed. ; <tt>is_author(array(4,'john-jones','Vivian'))</tt> : When the archive page for the author is either user ID 4, or ''user_nicename'' "john-jones", or ''nickname'' "Vivian". Note: the array ability was added at [[WordPress:Version 2.5]]. See also <tt>[[WordPress:#Any Archive Page|is_archive()]]</tt> and [[WordPress:Author Templates]]. === A Date Page === ; <tt>is_date()</tt> : When ''any'' date-based archive page is being displayed (i.e. a monthly, yearly, daily or time-based archive). ; <tt>is_year()</tt> : When a yearly archive is being displayed. ; <tt>is_month()</tt> : When a monthly archive is being displayed. ; <tt>is_day()</tt> : When a daily archive is being displayed. ; <tt>is_time()</tt> : When an hourly, "minutely", or "secondly" archive is being displayed. See also <tt>[[WordPress:#Any Archive Page|is_archive()]]</tt>. === Any Archive Page === ; <tt>is_archive()</tt> : When ''any'' type of Archive page is being displayed. Category, Author and Date based pages are all types of Archives. === A Search Result Page === ; <tt>is_search()</tt> : When a search result page archive is being displayed. === A 404 Not Found Page === ; <tt>is_404()</tt> : When a page displays after an "HTTP 404: Not Found" error occurs. === A Paged Page === ; <tt>is_paged()</tt> : When the page being displayed is "paged". This refers to an archive or the main page being split up over several pages. This does ''not'' refer to a Post or [[WordPress:Pages|Page]] whose content has been divided into pages using the <tt><!<nowiki></nowiki>--nextpage--></tt> [[WordPress:Writing Posts#Here is a run-down of the quicktag functions:|QuickTag]]. === An Attachment === ; <tt>is_attachment()</tt> : When an attachment document to a post or [[WordPress:Pages|Page]] is being displayed. An attachment is an image or other file uploaded through the post editor's upload utility. Attachments can be displayed on their own 'page' or template. For more information, see [[WordPress:Using Image and File Attachments]]. === A Syndication === ; <tt>is_feed()</tt> : When the site requested is a [[WordPress:Introduction_to_Blogging#Syndication|Syndication]]. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers. === A Trackback === ; <tt>is_trackback()</tt> : When the site requested is WordPress' hook into its Trackback engine. This tag is not typically used by users; it is used internally by WordPress and is available for Plugin Developers. === A Preview === ; <tt>is_preview()</tt> : When a single post being displayed is viewed in Draft mode. === Has An Excerpt === ; <tt>!empty($post->post_excerpt)</tt> : There is no built-in way to check if a post has an excerpt, but you can use the above code in the same way. Remove the '!' to emulate 'has_no_excerpt'. ==Working Examples== Here are working samples to demonstrate how to use these conditional tags. ===Single Post=== This example shows how to use <tt>is_single()</tt> to display something specific only when viewing a single post page: <pre> if (is_single()) { echo 'This is just one of many fabulous entries in the ' . single_cat_title() . ' category!'; } </pre> ===Date-Based Differences=== If someone browses our site by date, let's distinguish posts in different years by using different colors: <pre> <?php // this starts The 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 // are we showing a date-based archive? if (is_date()) { if (date('Y') != get_the_date('Y')) { // this post was written in a previous year // so let's style the content using the "oldentry" class echo '<div class="oldentry">'; } else { echo '<div class="entry">'; } } else { echo '<div class="entry">'; } the_content('Read the rest of this entry »'); ?> </div> </pre> ===Variable Sidebar Content=== This example will display different content in your sidebar based on what page the reader is currently viewing. <pre> <!-- begin sidebar --> <div id="sidebar"> <?php // let's generate info appropriate to the page being displayed if (is_home()) { // we're on the home page, so let's show a list of all top-level categories echo "<ul>"; wp_list_cats('optionall=0&sort_column=name&list=1&children=0'); echo "</ul>"; } elseif (is_category()) { // we're looking at a single category view, so let's show _all_ the categories echo "<ul>"; wp_list_cats('optionall=1&sort_column=name&list=1&children=1&hierarchical=1'); echo "</ul>"; } elseif (is_single()) { // we're looking at a single page, so let's not show anything in the sidebar } elseif (is_page()) { // we're looking at a static page. Which one? if (is_page('About')) { // our about page. echo "<p>This is my about page!</p>"; } elseif (is_page('Colophon')) { echo "<p>This is my colophon page, running on WordPress " . bloginfo('version') . "</p>"; } else { // catch-all for other pages echo "<p>Vote for Pedro!</p>"; } } else { // catch-all for everything else (archives, searches, 404s, etc) echo "<p>Pedro offers you his protection.</p>"; } // That's all, folks! ?> <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> <!-- end sidebar --> </pre> ===Helpful 404 page=== When a visitor gets a 404 error page, it can be intimidating, and unhelpful. Using Wordpress, you can take the edge off a 404 and make it helpful to users, and yourself, too, by emailing whenever the user clicks a link to a non-existent page. If you use this, don't forget o <pre> <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'])) { #politely blames the user for all the problems they caused echo "tried going to "; #starts assembling an output paragraph $casemessage = "All is not lost!"; } elseif (isset($_SERVER['HTTP_REFERER'])) { #this will help the user find what they want, and email me of a bad link echo "clicked a link to"; #now the message says You clicked a link to... #setup a message to be sent to me $failuremess = "A user tried to go to $website" .$_SERVER['REQUEST_URI']." and received a 404 (page not found) error. "; $failuremess .= "It wasn't their fault, so try fixing it. They came from ".$_SERVER['HTTP_REFERER']; mail($adminemail, "Bad Link To ".$_SERVER['REQUEST_URI'], $failuremess, "From: $websitename <noreply@$website>"); #email you about problem $casemessage = "An administrator has been emailed about this problem, too.";#set a friendly message } echo " ".$website.$_SERVER['REQUEST_URI']; ?> and it doesn't exist. <?php echo $casemessage; ?> You can click back and try again or search for what you're looking for: <?php include(TEMPLATEPATH . "/searchform.php"); ?> </p> </pre> ===Dynamic Menu Highlighting=== The [[WordPress:Dynamic Menu Highlighting]] article discusses how to use the conditional tags to enable highlighting of the current page in a menu. ==External Ressources== * French translation : [[WordPress:fr:Marqueurs_conditionnels]] {{No Param Tag Footer}}
摘要:
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅
Wordpress-mediawiki:版权
的细节)。
未经许可,请勿提交受版权保护的作品!
取消
编辑帮助
(在新窗口中打开)
本页使用的模板:
模板:没有 Param 标签 页底文字
(
编辑
)