2009年7月6日星期一

精品博客

精品博客


WordPress 主题之搜索模板和静态页面模板

Posted: 05 Jul 2009 07:37 PM PDT

原文:The WordPress Theme Search Template and Page Template

原文作者:Ian Stewart

译文: WordPress 主题之搜索模板和静态页面模板

译文作者:Young

对任一完整的 WordPress 主题来说,搜索模板和静态页面模板都是很重要的,并且代码都很容易。这两个模板也是模板中的模板,都有以下相同的代码:

<?php get_header(); ?>    		<div id="container"> 			<div id="content">   				<div id="nav-above" class="navigation">  				</div><!-- #nav-above -->  				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>  				</div><!-- #post-<?php the_ID(); ?> -->   				<div id="nav-below" class="navigation">  				</div><!-- #nav-below -->    			</div><!-- #content --> 		</div><!-- #container -->   <?php get_sidebar(); ?> <?php get_footer(); ?>

当然,每个模板也都互不相同。

搜索模板

在 serach.php 里面,我们需要重新用到循环。这次使用 IF 语句 —— 以防循环里没有任何文章。

工作原理是这样的:如果有文章,或者说有符合搜索条件的文章,那么就循环检测,就像 index.php 里面的循环语句一样。但是如果没有文章,或者没有符合搜索条件的文章,那就为搜索者提供另外一个搜索机会。

代码如下:

<?php get_header(); ?>   		<div id="container"> 			<div id="content">    <?php if ( have_posts() ) : ?>   <?php while ( have_posts() ) : the_post() ?>  <!-- this is our loop --> <?php endwhile; ?>   <?php else : ?>    <!-- here's where we'll put a search form if there're no posts -->   <?php endif; ?>   			</div><!-- #content --> 		</div><!-- #container -->    <?php get_sidebar(); ?> <?php get_footer(); ?>

非常直接了当,是吧?

我想保留日志标题、元、内容(或摘要)以及实用链接等 index 模板里面有的内容不变,但是 WordPress 搜索日志的时候同时也会搜索静态页面,而静态页面是不需要元或者实用链接之类的,所以,在我们的循环里面,将会检测看是搜索日志还是搜索静态页面。

<?php if ( $post->post_type == 'post' ) { ?>  <?php } ?>

这个 IF 语句只有搜索静态页面的时候才会显示,现在我们知道会发生什么事了,以下就是搜索模板 #content DIV 标签里面的代码:

<?php if ( have_posts() ) : ?>    				<h1 class="page-title"><?php _e( 'Search Results for: ', 'your-theme' ); ?><span><?php the_search_query(); ?></span></h1>    <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>  				<div id="nav-above" class="navigation"> 					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'your-theme' )) ?></div>  					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'your-theme' )) ?></div>  				</div><!-- #nav-above --> <?php } ?>   <?php while ( have_posts() ) : the_post() ?>    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 					<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>    <?php if ( $post->post_type == 'post' ) { ?> 					<div class="entry-meta">  						<span class="meta-prep meta-prep-author"><?php _e('By ', 'your-theme'); ?></span>  						<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>  						<span class="meta-sep"> | </span> 						<span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'your-theme'); ?></span>  						<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>  						<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>  					</div><!-- .entry-meta --> <?php } ?>    					<div class="entry-summary"> <?php the_excerpt( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'your-theme' )  ); ?>  <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>  					</div><!-- .entry-summary -->   <?php if ( $post->post_type == 'post' ) { ?> 					<div class="entry-utility">  						<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>  						<span class="meta-sep"> | </span> 						<?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>  						<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'your-theme' ), __( '1 Comment', 'your-theme' ), __( '% Comments', 'your-theme' ) ) ?></span>  						<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>  					</div><!-- #entry-utility --> <?php } ?> 				</div><!-- #post-<?php the_ID(); ?> -->    <?php endwhile; ?>   <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>  				<div id="nav-below" class="navigation"> 					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'your-theme' )) ?></div>  					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'your-theme' )) ?></div>  				</div><!-- #nav-below --> <?php } ?>   <?php else : ?>    				<div id="post-0" class="post no-results not-found"> 					<h2 class="entry-title"><?php _e( 'Nothing Found', 'your-theme' ) ?></h2>  					<div class="entry-content"> 						<p><?php _e( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'your-theme' ); ?></p>  	<?php get_search_form(); ?> 					</div><!-- .entry-content -->  				</div>   <?php endif; ?>

【当然,您还可以加上页眉和页脚以及侧边栏等函数。——译者注】

静态页面模板

您知道静态页面模板是干什么用的。WordPress 认为它是一种没有时间顺序的日志。我们认为它是一个静态页面,不过日志的所有装饰它都几乎没有——除了评论,有些静态页面是有评论的。我不喜欢,不过您可能喜欢。如果您想在静态页面添加评论,可以在静态页面上添加带有 Name 和 Value 的 "comments"自定义区域,这个我听起来不错。

下面就是静态页面代码:

<?php get_header(); ?>    		<div id="container"> 			<div id="content">   <?php the_post(); ?>    				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>> 					<h1 class="entry-title"><?php the_title(); ?></h1>  					<div class="entry-content"> <?php the_content(); ?> <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>					  <?php edit_post_link( __( 'Edit', 'your-theme' ), '<span class="edit-link">', '</span>' ) ?>  					</div><!-- .entry-content --> 				</div><!-- #post-<?php the_ID(); ?> -->			    <?php if ( get_post_custom_values('comments') ) comments_template() // Add a custom field with Name and Value of "comments" to enable comments on this page ?>			    			</div><!-- #content --> 		</div><!-- #container -->  <?php get_sidebar(); ?>	  <?php get_footer(); ?>

如何创建 WordPress 主题

这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


WordPress 主题之评论模板

Posted: 04 Jul 2009 07:02 PM PDT

原文:The WordPress Theme Comments Template

原文作者:Ian Stewart

译文:WordPress 主题之评论模板

译文作者:Young

是的,我讨厌评论模板,因为它可能相当令人困惑。在 2.7 版本上,WordPress 介绍了一个简单的创建评论模板的方法,但是那种方法不能使留言和引用分开,也不能自定义评论结构,并且仍然令人困惑。

您很幸运,我把问题解决了,虽然还是有点令人困惑,但毕竟解决了问题。在这个评论模板的教程上,我将基本上介绍每一步会发生什么,展示您那些自定义代码,以及需要添加到 function.php 的代码,最后给出整个模板代码。希望这对您有帮助。但是至少您将会有一个漂亮的评论模板。

让我们简要的看一下这个评论模板会发生什么事:

  1. 阻止机器人抓取评论内容以及受密码保护的文章;
  2. 检查是否有评论;
  3. 计算留言和引用 (trackbacks or pings) 的数量;
  4. 如果有评论,显示评论,并链接到评论;
  5. 如果有引用,显示引用;
  6. 如果评论被启用,显示评论"回复"框。

这个模板的代码很多,但是总结起来也就这几个功能,直截了当。

自定义回调留言和引用

WordPress 2.7 允许通过 wp_list_comments() 函数方便有序地调用单独日志的留言和引用。如果您喜欢那样,那就很方便。但是我们不喜欢,我想要把评论里面的留言和引用分开。

为了实现这种结果,您需要对留言和引用进行自定义回调,把下面代码插入到 functions.php 文件:

// Custom callback to list comments in the your-theme style function custom_comments($comment, $args, $depth) {    $GLOBALS['comment'] = $comment; 	$GLOBALS['comment_depth'] = $depth;    ?>   	<li id="comment-<?php comment_ID() ?>" <?php comment_class() ?>>   		<div class="comment-author vcard"><?php commenter_link() ?></div>    		<div class="comment-meta"><?php printf(__('Posted %1$s at %2$s <span class="meta-sep">|</span> <a href="%3$s" title="Permalink to this comment">Permalink</a>', 'your-theme'),    					get_comment_date(),   					get_comment_time(),   					'#comment-' . get_comment_ID() );    					edit_comment_link(__('Edit', 'your-theme'), ' <span class="meta-sep">|</span> <span class="edit-link">', '</span>'); ?></div>    <?php if ($comment->comment_approved == '0') _e("\t\t\t\t\t<span class='unapproved'>Your comment is awaiting moderation.</span>\n", 'your-theme') ?>            <div class="comment-content">       		<?php comment_text() ?>   		</div>  		<?php // echo the comment reply link 			if($args['type'] == 'all' || get_comment_type() == 'comment') :  				comment_reply_link(array_merge($args, array( 					'reply_text' => __('Reply','your-theme'), 					'login_text' => __('Log in to reply.','your-theme'),  					'depth' => $depth, 					'before' => '<div class="comment-reply-link">', 					'after' => '</div>'  				))); 			endif; 		?> <?php } // end custom_comments
// Custom callback to list pings function custom_pings($comment, $args, $depth) {        $GLOBALS['comment'] = $comment;          ?>     		<li id="comment-<?php comment_ID() ?>" <?php comment_class() ?>>     			<div class="comment-author"><?php printf(__('By %1$s on %2$s at %3$s', 'your-theme'),      					get_comment_author_link(),     					get_comment_date(),     					get_comment_time() );     					edit_comment_link(__('Edit', 'your-theme'), ' <span class="meta-sep">|</span> <span class="edit-link">', '</span>'); ?></div>      <?php if ($comment->comment_approved == '0') _e('\t\t\t\t\t<span class="unapproved">Your trackback is awaiting moderation.</span>\n', 'your-theme') ?>              <div class="comment-content">     			<?php comment_text() ?> 			</div>  <?php } // end custom_pings

这些代码看起来有点乱,是吧?但它们是最好的。现在到创建评论结构了,我想下面那个结构很漂亮,并且能够让您只通过 CSS 就能更改很多 —— 如果您真的想修改这个结构。

我们还需要调用一个特殊的自定义函数,这个函数将会显示符合 microformat hcard 架构的头像结构。

// Produces an avatar image with the hCard-compliant photo class function commenter_link() { 	$commenter = get_comment_author_link();  	if ( ereg( '<a[^>]* class=[^>]+>', $commenter ) ) {  		$commenter = ereg_replace( '(<a[^>]* class=[\'"]?)', '\\1url ' , $commenter );  	} else { 		$commenter = ereg_replace( '(<a )/', '\\1class="url "' , $commenter );  	} 	$avatar_email = get_comment_author_email(); 	$avatar = str_replace( "class='avatar", "class='photo avatar", get_avatar( $avatar_email, 80 ) );  	echo $avatar . ' <span class="fn n">' . $commenter . '</span>';  } // end commenter_link

(以上代码插入于 functions.php 文件中。——译者注)

如果想改变头像的尺寸,您只需要改一下 get_avatar($avatar_email, 80) 里面的 80 就可以了,80 表示头像的大小,单位是像素。

评论模板

没有把您吓跑吧?诚然,并不是很可怕。下面是整个评论模板代码,上面有些 PHP 注释,以便您的理解。

<?php /* The Comments Template — with, er, comments! */ ?> 			<div id="comments"> <?php /* Run some checks for bots and password protected posts */ ?> <?php  	$req = get_option('require_name_email'); // Checks if fields are required. 	if ( 'comments.php' == basename($_SERVER['SCRIPT_FILENAME']) )  		die ( 'Please do not load this page directly. Thanks!' ); 	if ( ! empty($post->post_password) ) :  		if ( $_COOKIE['wp-postpass_' . COOKIEHASH] != $post->post_password ) :  ?> 				<div class="nopassword"><?php _e('This post is password protected. Enter the password to view any comments.', 'your-theme') ?></div>  			</div><!-- .comments --> <?php 		return; 	endif;  endif; ?>   <?php /* See IF there are comments and do the comments stuff! */ ?> <?php if ( have_comments() ) : ?>    <?php /* Count the number of comments and trackbacks (or pings) */ $ping_count = $comment_count = 0; foreach ( $comments as $comment )  	get_comment_type() == "comment" ? ++$comment_count : ++$ping_count;  ?>   <?php /* IF there are comments, show the comments */ ?> <?php if ( ! empty($comments_by_type['comment']) ) : ?>    				<div id="comments-list" class="comments"> 					<h3><?php printf($comment_count > 1 ? __('<span>%d</span> Comments', 'your-theme') : __('<span>One</span> Comment', 'your-theme'), $comment_count) ?></h3>    <?php /* If there are enough comments, build the comment navigation  */ ?> <?php $total_pages = get_comment_pages_count(); if ( $total_pages > 1 ) : ?>  					<div id="comments-nav-above" class="comments-navigation"> 								<div class="paginated-comments-links"><?php paginate_comments_links(); ?></div>  					</div><!-- #comments-nav-above --> <?php endif; ?>   <?php /* An ordered list of our custom comments callback, custom_comments(), in functions.php   */ ?> 					<ol>  <?php wp_list_comments('type=comment&callback=custom_comments'); ?> 					</ol>   <?php /* If there are enough comments, build the comment navigation */ ?>  <?php $total_pages = get_comment_pages_count(); if ( $total_pages > 1 ) : ?> 	  			<div id="comments-nav-below" class="comments-navigation">  						<div class="paginated-comments-links"><?php paginate_comments_links(); ?></div> 	        </div><!-- #comments-nav-below -->  <?php endif; ?>   				</div><!-- #comments-list .comments -->  <?php endif; /* if ( $comment_count ) */ ?>    <?php /* If there are trackbacks(pings), show the trackbacks  */ ?> <?php if ( ! empty($comments_by_type['pings']) ) : ?>    				<div id="trackbacks-list" class="comments"> 					<h3><?php printf($ping_count > 1 ? __('<span>%d</span> Trackbacks', 'your-theme') : __('<span>One</span> Trackback', 'your-theme'), $ping_count) ?></h3>    <?php /* An ordered list of our custom trackbacks callback, custom_pings(), in functions.php   */ ?> 					<ol> <?php wp_list_comments('type=pings&callback=custom_pings'); ?>  					</ol>   				</div><!-- #trackbacks-list .comments -->    <?php endif /* if ( $ping_count ) */ ?>  <?php endif /* if ( $comments ) */ ?>   <?php /* If comments are open, build the respond form */ ?> <?php if ( 'open' == $post->comment_status ) : ?>  				<div id="respond">     				<h3><?php comment_form_title( __('Post a Comment', 'your-theme'), __('Post a Reply to %s', 'your-theme') ); ?></h3>        				<div id="cancel-comment-reply"><?php cancel_comment_reply_link() ?></div>   <?php if ( get_option('comment_registration') && !$user_ID ) : ?>  					<p id="login-req"><?php printf(__('You must be <a href="%s" title="Log in">logged in</a> to post a comment.', 'your-theme'),  					get_option('siteurl') . '/wp-login.php?redirect_to=' . get_permalink() ) ?></p>    <?php else : ?> 					<div class="formcontainer">      						<form id="commentform" action="<?php echo get_option('siteurl'); ?>/wp-comments-post.php" method="post">   <?php if ( $user_ID ) : ?>  							<p id="login"><?php printf(__('<span class="loggedin">Logged in as <a href="%1$s" title="Logged in as %2$s">%2$s</a>.</span> <span class="logout"><a href="%3$s" title="Log out of this account">Log out?</a></span>', 'your-theme'),  								get_option('siteurl') . '/wp-admin/profile.php', 								wp_specialchars($user_identity, true),  								wp_logout_url(get_permalink()) ) ?></p>   <?php else : ?>    							<p id="comment-notes"><?php _e('Your email is <em>never</em> published nor shared.', 'your-theme') ?> <?php if ($req) _e('Required fields are marked <span class="required">*</span>', 'your-theme') ?></p>                  <div id="form-section-author" class="form-section"> 								<div class="form-label"><label for="author"><?php _e('Name', 'your-theme') ?></label> <?php if ($req) _e('<span class="required">*</span>', 'your-theme') ?></div>  								<div class="form-input"><input id="author" name="author" type="text" value="<?php echo $comment_author ?>" size="30" maxlength="20" tabindex="3" /></div>                </div><!-- #form-section-author .form-section -->                <div id="form-section-email" class="form-section">  								<div class="form-label"><label for="email"><?php _e('Email', 'your-theme') ?></label> <?php if ($req) _e('<span class="required">*</span>', 'your-theme') ?></div>  								<div class="form-input"><input id="email" name="email" type="text" value="<?php echo $comment_author_email ?>" size="30" maxlength="50" tabindex="4" /></div>                </div><!-- #form-section-email .form-section -->                <div id="form-section-url" class="form-section">  								<div class="form-label"><label for="url"><?php _e('Website', 'your-theme') ?></label></div>  								<div class="form-input"><input id="url" name="url" type="text" value="<?php echo $comment_author_url ?>" size="30" maxlength="50" tabindex="5" /></div>                </div><!-- #form-section-url .form-section -->  <?php endif /* if ( $user_ID ) */ ?>                  <div id="form-section-comment" class="form-section"> 								<div class="form-label"><label for="comment"><?php _e('Comment', 'your-theme') ?></label></div>  								<div class="form-textarea"><textarea id="comment" name="comment" cols="45" rows="8" tabindex="6"></textarea></div>                </div><!-- #form-section-comment .form-section -->                <div id="form-allowed-tags" class="form-section"> 	              <p><span><?php _e('You may use these <abbr title="HyperText Markup Language">HTML</abbr> tags and attributes:', 'your-theme') ?></span> <code><?php echo allowed_tags(); ?></code></p>                </div>   <?php do_action('comment_form', $post->ID); ?>    							<div class="form-submit"><input id="submit" name="submit" type="submit" value="<?php _e('Post Comment', 'your-theme') ?>" tabindex="7" /><input type="hidden" name="comment_post_ID" value="<?php echo $id; ?>" /></div>    <?php comment_id_fields(); ?>   <?php /* Just … end everything. We're done here. Close it up. */ ?>   						</form><!-- #commentform -->										  					</div><!-- .formcontainer --> <?php endif /* if ( get_option('comment_registration') && !$user_ID ) */ ?>  				</div><!-- #respond --> <?php endif /* if ( 'open' == $post->comment_status ) */ ?> 			</div><!-- #comments -->

就这么多了,您已经创建了一个自定义的漂亮的评论模板。

如何创建 WordPress 主题

这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


WordPress 主题之单独日志、附档和 404 模板

Posted: 03 Jul 2009 11:02 PM PDT

原文:The WordPress Theme Single Post, Post Attachment, & 404 Templates

原文作者:Ian Stewart

译文:WordPress 主题之单独日志、附档和 404 模板

译文作者:Young

您已经为所有的文件创建了一个 Index,现在您需要为每一篇单独的文章(或者缺失的内容)创建一个单独的模板。

模板中的模板

single.php (以及以后将要创建的几乎所有的其他模板)的结构很多都是和 index.php 一样的,事实上,您可以把它看成模板中的模板。相同结构如下:

<?php get_header(); ?>    		<div id="container"> 			<div id="content">   				<div id="nav-above" class="navigation">  				</div><!-- #nav-above -->  				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>  				</div><!-- #post-<?php the_ID(); ?> -->   				<div id="nav-below" class="navigation">  				</div><!-- #nav-below -->    			</div><!-- #content --> 		</div><!-- #container -->   <?php get_sidebar(); ?> <?php get_footer(); ?>

但是也会有些显著的不同,先从 the_post() 和 comments_template() 开始吧。

在 <div id="content"> 之后以及 <div id="nav-above" class="navigation"> 之前,我们将调用 the_post() 函数(即 <?php the_post(); ?>)。这个模板不需要循环,因为通过之前创建的 the_permalink() 函数, WordPress 知道我们要找什么。

既然这是一篇单独日志,我们需要调用 comments_template() 函数。因为我们要把留言和引用分开,所以代码该是这样:

<?php comments_template('', true); ?>

上面的代码需要放在 </div><!– #content –> 之前,</div><!– #nav-below –> 之后。

单独日志导航

这里,我们将会用 next_post_link() 替代 previous_posts_link(), 用 previous_post_link() 替代 next_posts_link(),他们的作用就和他们的名称一样。

				<div id="nav-above" class="navigation"> 					<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">&laquo;</span> %title' ) ?></div>  					<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">&raquo;</span>' ) ?></div>  				</div><!-- #nav-above -->
				<div id="nav-below" class="navigation">  					<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">&laquo;</span> %title' ) ?></div>  					<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">&raquo;</span>' ) ?></div>  				</div><!-- #nav-below -->

单独日志标题

如果您还记得 header.php 那一课,就会发现我们用一个动态的 IF 语句方便屏幕浏览器识别单独日志标题。在这里以及后面的主题模板,我们将用 H1 标签获取文章标题。

					<h1 class="entry-title"><?php the_title(); ?></h1> 

[代码放于 <div id="post-<?php the_id()? >" <?php post_class(); ?>> ] 之后。——译者注]

您会发现这个代码也有点简单,不需要任何链接。

单独日志实用性链接

文章的实用性很复杂。您从中将会发觉一次性获得答案以及站在他人肩膀上的好处。

在看代码之前,我们应该想一下它为什么那么复杂。由于 WordPress 评论的特殊性,我们需要考虑几种不同的方式:同时开放留言和引用方式、只开放引用、只开放留言或者留言和引用都关闭。这就意味着要用上一大堆 IF 语句,当然,还有 ELSEIF 语句。

我们还想添加一个页面永久链接以及 RSS 链接,这对跟踪开发交流有用。

				<div class="entry-utility"> 					<?php printf( __( 'This entry was posted in %1$s%2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>. Follow any comments here with the <a href="%5$s" title="Comments RSS to %4$s" rel="alternate" type="application/rss+xml">RSS feed for this post</a>.', 'your-theme' ),  						get_the_category_list(', '), 						get_the_tag_list( __( ' and tagged ', 'your-theme' ), ', ', '' ),  						get_permalink(), 						the_title_attribute('echo=0'), 						comments_rss() ) ?>   <?php if ( ('open' == $post->comment_status) && ('open' == $post->ping_status) ) : // Comments and trackbacks open ?>  						<?php printf( __( '<a class="comment-link" href="#respond" title="Post a comment">Post a comment</a> or leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', 'your-theme' ), get_trackback_url() ) ?>  <?php elseif ( !('open' == $post->comment_status) && ('open' == $post->ping_status) ) : // Only trackbacks open ?>  						<?php printf( __( 'Comments are closed, but you can leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', 'your-theme' ), get_trackback_url() ) ?>  <?php elseif ( ('open' == $post->comment_status) && !('open' == $post->ping_status) ) : // Only comments open ?>  						<?php _e( 'Trackbacks are closed, but you can <a class="comment-link" href="#respond" title="Post a comment">post a comment</a>.', 'your-theme' ) ?>  <?php elseif ( !('open' == $post->comment_status) && !('open' == $post->ping_status) ) : // Comments and trackbacks closed ?>  						<?php _e( 'Both comments and trackbacks are currently closed.', 'your-theme' ) ?> <?php endif; ?>  <?php edit_post_link( __( 'Edit', 'your-theme' ), "\n\t\t\t\t\t<span class=\"edit-link\">", "</span>" ) ?>  					</div><!-- .entry-utility -->

上面的代码并没有那么复杂,不是吗?

单独日志内容

和 index.php 不一样,single.php 的内容很简单,只需要调用一个简单的 wp_link_page() 函数。

<?php the_content(); ?>  <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>

(以上代码要放于 class="entry-content" 的 DIV 标签中间。——译者注)

单独日志附档

并没有多少人使用文章附档模板,但是它很有趣。当您在文章里添加图片的时候,您实际上是把图片附在文章上面。当然,您不仅可以附加图片。我们将要创建一个 attachment.php 文件,但是如果您喜欢,也可以添加 video.php、audio.php 以及 application.php,那样就可以在文章上面附视频、音频和其他应用了。创建附档模板的方法有很多种。

其中最简单的方法是复制 single.php 文件里面的内容,并重命名为 attachment.php,然后稍作修改:

首先,删除顶部的导航代码,这里完全不需要它,然后用链接到原始文章的的页面标题代替。

				<h1 class="page-title"><a href="<?php echo get_permalink($post->post_parent) ?>" title="<?php printf( __( 'Return to %s', 'your-theme' ), wp_specialchars( get_the_title($post->post_parent), 1 ) ) ?>" rev="attachment"><span class="meta-nav">&laquo; </span><?php echo get_the_title($post->post_parent) ?></a></h1>

既然页面标题已经由 h1 标签调用,我们的文章标题应该使用 h2 标签。

					<h2 class="entry-title"><?php the_title(); ?></h2>

由于很多的附档都是图片,为了正确显示附档,我们需要调用一个 IF 语句:

					<div class="entry-content"> 						<div class="entry-attachment">					  <?php if ( wp_attachment_is_image( $post->id ) ) : $att_image = wp_get_attachment_image_src( $post->id, "medium"); ?>  						<p class="attachment"><a href="<?php echo wp_get_attachment_url($post->id); ?>" title="<?php the_title(); ?>" rel="attachment"><img src="<?php echo $att_image[0];?>" width="<?php echo $att_image[1];?>" height="<?php echo $att_image[2];?>"  class="attachment-medium" alt="<?php $post->post_excerpt; ?>" /></a>  						</p> <?php else : ?> 						<a href="<?php echo wp_get_attachment_url($post->ID) ?>" title="<?php echo wp_specialchars( get_the_title($post->ID), 1 ) ?>" rel="attachment"><?php echo basename($post->guid) ?></a>		  <?php endif; ?> 						</div> 						<div class="entry-caption"><?php if ( !empty($post->post_excerpt) ) the_excerpt() ?></div>      <?php the_content( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'your-theme' )  ); ?>  <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>    					</div><!-- .entry-content -->

再删除底部的导航代码之后,attachment.php 模板就做好了。

404 模板

当访问页面不存在的时候(例如博客文章的 URL 输入错误,或者文章根本就没有发表),就会出现 404 错误。

我们可以创建一个空白的 404.php 文件,然后插入以下代码:

				<div id="post-0" class="post error404 not-found">  					<h1 class="entry-title"><?php _e( 'Not Found', 'your-theme' ); ?></h1>  					<div class="entry-content"> 						<p><?php _e( 'Apologies, but we were unable to find what you were looking for. Perhaps searching will help.', 'your-theme' ); ?></p>  	<?php get_search_form(); ?> 					</div><!-- .entry-content -->  				</div><!-- #post-0 -->  这样,当访问页面不存在的时候,就会出现一个对不起的提示以及一个搜索框,非常简单直接。

如何创建 WordPress 主题

这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。

译者注:

1、single.php 文件里面作者好像忘记了元标签,虽然这个也可以从 index.php 里面复制过来。

2、single.php 文件里面的全部代码如下:

<?php get_header(); ?> <div id="container"> 			<div id="content"> 			<?php the_post(); ?>  			<div id="nav-above" class="navigation"> 					<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">&laquo;</span> %title' ) ?></div>    					<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">&raquo;</span>' ) ?></div>    			  </div><!-- #nav-above -->   				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>  						<h1 class="entry-title"><?php the_title(); ?></h1> 						<div class="entry-meta"> 						<span class="meta-prep meta-prep-author"><?php _e('By ', 'your_theme'); ?></span>  						<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your_theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>  						<span class="meta-sep"> | </span> 						<span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'your_theme'); ?></span>  						<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>  						<?php edit_post_link( __( 'Edit', 'your_theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?> 					</div><!-- .entry-meta -->  											<div class="entry-content"> <?php the_content(); ?> <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your_theme' ) . '&after=</div>') ?>  					</div><!-- .entry-content -->   								<div class="entry-utility"> 					<?php printf( __( 'This entry was posted in %1$s%2$s. Bookmark the <a href="%3$s" title="Permalink to %4$s" rel="bookmark">permalink</a>. Follow any comments here with the <a href="%5$s" title="Comments RSS to %4$s" rel="alternate" type="application/rss+xml">RSS feed for this post</a>.', 'your-theme' ),    						get_the_category_list(', '), 						get_the_tag_list( __( ' and tagged ', 'your-theme' ), ', ', '' ),    						get_permalink(), 						the_title_attribute('echo=0'), 						comments_rss() ) ?>    <?php if ( ('open' == $post->comment_status) && ('open' == $post->ping_status) ) : // Comments and trackbacks open ?>    						<?php printf( __( '<a class="comment-link" href="#respond" title="Post a comment">Post a comment</a> or leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', 'your-theme' ), get_trackback_url() ) ?>    <?php elseif ( !('open' == $post->comment_status) && ('open' == $post->ping_status) ) : // Only trackbacks open ?>    						<?php printf( __( 'Comments are closed, but you can leave a trackback: <a class="trackback-link" href="%s" title="Trackback URL for your post" rel="trackback">Trackback URL</a>.', 'your-theme' ), get_trackback_url() ) ?>    <?php elseif ( ('open' == $post->comment_status) && !('open' == $post->ping_status) ) : // Only comments open ?>    						<?php _e( 'Trackbacks are closed, but you can <a class="comment-link" href="#respond" title="Post a comment">post a comment</a>.', 'your-theme' ) ?>    <?php elseif ( !('open' == $post->comment_status) && !('open' == $post->ping_status) ) : // Comments and trackbacks closed ?>    						<?php _e( 'Both comments and trackbacks are currently closed.', 'your-theme' ) ?> <?php endif; ?>    <?php edit_post_link( __( 'Edit', 'your-theme' ), "\n\t\t\t\t\t<span class=\"edit-link\">", "</span>" ) ?>    					</div> 								<!-- .entry-utility -->   				</div><!-- #post-<?php the_ID(); ?> --> 		<div id="nav-below" class="navigation">    					<div class="nav-previous"><?php previous_post_link( '%link', '<span class="meta-nav">&laquo;</span> %title' ) ?></div>    					<div class="nav-next"><?php next_post_link( '%link', '%title <span class="meta-nav">&raquo;</span>' ) ?></div>    				</div> <!-- #nav-below --> <?php comments_template('', true); ?>  </div> 			<!-- #content --> </div><!-- #container -->     <?php get_sidebar(); ?>	  <?php get_footer(); ?>

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


QQ 肥水不流外人田

Posted: 02 Jul 2009 04:00 PM PDT

在 《您的 QQ 号码可能存在异常情况》 一文中,有位热心读者 nbysy 在留言框里说了很多,大概意思有几点:

1、QQ 没有跟 Palringo 合作;

2、Palringo 私自通过"逆向协议""不合法"地利用 QQ;

3、Palringo 有可能偷偷记录我的帐号跟密码;

4、QQ 在这么多 IM 中,帐号安全应该做的是最好的。

留言很长,我作为附件放在文章的后面供参考。一开始看到这个留言的时候,我很想回复,但是又不知道怎么回。后来我写信给 Palringo,17 天之后才收到回复,回复的内容大概如下:

1、Palringo 承认是通过一种非正式的技术链接到 QQ;

2、之所以出现 "您的 QQ 号码可能存在异常情况"提示,是因为 Palringo 的服务器在英国;

3、QQ 已经修改了自身协议,Palringo 之前所用的技术失效;

4、Palringo 正在寻找解决方法,但恐怕有待时日。

Palringo 是用英文回复的,原始内容同样作为附件放在此文最后。

结合 Palringo 的回复,可以看出 nbysy 的第一点和第二点是正确的,至于 Palringo 是否会"偷偷记录我的帐号跟密码",我无法验证,不过我相信它不会。

而至于 QQ 的帐号安全是否是做得最好的,我也无法论证,不过我想说的是:

为什么除了 QQ,MSN、GTalk、Facebook 等其他的 IM 工具都没有问题呢?难道是他们的帐户安全都不如 QQ?对此我深表怀疑。我认为 QQ 不给 Palringo 整合的根本原因不是为了帐号的安全,而是为了避免"肥水流外人田"。为什么这样说呢?因为 QQ 聊天软件上有大量的广告,有图为证:

QQ 广告

一个对话框,就有三个明显的广告,而"点此免费拥有 QQ 秀"则至少有可能给腾讯网页带去流量。而如果用户通过 Palringo 或者其他第三方软件玩 QQ, 那么腾讯一丁点利益也捞不到,虽然 Palringo 也捞不到——它目前没有广告。

附件:

1、nbysy 的留言:

QQ怎么可能会跟palringo合作?

只是palringo逆向了QQ的协议,然后假扮QQ的合法客户端与QQ交互。逆向QQ协议的客户
端一大堆,parlringo 不是第一个。 说到底,这些作法是不合法的。

又因为逆向的不完整或者逆向的协议版本太低,所以腾讯才会发现并警告异常。

这样的警告其实合情合理,因为许多盗用QQ或用QQ发广告的不法之徒就是用这些逆向的协议代码来自动登录QQ,然后做些自动化的操作。 腾讯有理由怀疑你的QQ已经被盗了。

另外,你试试用官方的手机版QQ登录试试看,会不会有问题。我怀疑出问题的可能性很小,因为我平时也经常用。分辩移动,电信,联通的接入点,这点能力QQ应该还是的,否则这么多手机QQ用户都要造反了。

如果QQ不是因为协议版本的问题而提出警告,而是其所说的"在国外异常登录",那你应该还要怀疑的是,parlringo 是不是偷偷记录你的帐号跟密码,然后在国外登录了?或者,模拟QQ协议的软件模块是在parlringo的服务器上,所有消息都是经过parlringo 服务器中转的?

最后说句公道话,QQ在这么多IM中,帐号安全应该做的是最好的。你见过哪个IM被盗以后,还能通过申诉找回的?

2、Palringo 的回复:

Palringo was using an unofficial ‘bridge’ to connect to QQ who have now introduced a change in their protocol that prevents this. We are looking into a solution but are expecting the QQ service to remain inactive for the immediate future.

The foreign country log in will be due to the Palringo servers being situated in the UK – so nothing to work about.

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


WordPress 主题 Index 模板

Posted: 01 Jul 2009 10:11 PM PDT

原文:The WordPress Theme Index Template

原文作者:Ian Stewart

译文:WordPress 主题 Index 模板

译文作者:Young

Index.php 文件是 WordPress 主题最重要的模板,不仅当其他相关的模板(例如 category.php 或者 tag.php) 缺失时,需要用到它,而且建好这个模板还有助于建好其他的模板(除了评论模板 —— 不管您怎么看,这个模板的确不好弄)。

关于循环

Index.php 主要代码在模板中间,并都在一个循环里面。没有循环,就没有 index.php。循环就像这个样子:

<?php while ( have_posts() ) : the_post() ?> <?php endwhile; ?>

真的很简单。当数据库里面有文章的时候,您的主题就会循环检查每篇文章并起些作用,至于起什么作用则是棘手问题,不过也可能简单。

我们就从上面那个循环开始。把下面的代码插入到 index.php 文件的 #content DIV 标签中间:

<?php while ( have_posts() ) : the_post() ?>  <?php the_content(); ?> <?php endwhile; ?>

这样做会产生什么结果呢?会显示整篇文章的内容。但是我们可以改改:

<ul> <?php while ( have_posts() ) : the_post() ?>  <li>     <?php the_excerpt(); ?> </li> <?php endwhile; ?>		  </ul>

您发现改了哪里了吗?修改之后的代码就只会显示随机的文章摘要。现在您知道 the_content() 和 the_excerpt() 代码是干什么用的了吧?

基本上,您建一个循环(以 while 开始 endwhile 结束)并在中间添加一些东西——即 WordPress 模板标签——以便从循环文章里面提取相关信息,就像上一课的 bloginfo() 一样从 WordPress 设置里面提取博客标题。

下面我们来建一个真正复杂的循环。先从一些基本的精简代码开始,但是这些代码需要包括 More Next Page 标签,并且这些标签要包含于一个独立的 class="entry-content" 的 DIV 标签里面,即:

				<div class="entry-content"> <?php the_content( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'your-theme' )  ); ?>  <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>  					</div><!-- .entry-content -->

那么文章标题怎么办呢?也很简单。我们将使用模板标签 the_title() 来获取文章标题并且通过 <a> 标签 和 the_permalink() 使它链接到原文。同时,我们还将添加一个 title 和一个 bookmark 属性,以便告诉谷歌等机器那是博客文章的永久链接。把下面代码插入到 <div class="enty-content"> 前面:

					<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

关于任意一篇文章的作者、发表时间、分类、标签、评论链接等内容,我想把它们分开两个部分:其中一个部分是元(包括作者和发表时间),会放在文章正文之前;另一个部分是实用工具(包括分类、标签和评论链接),会放在文章正文之后。

让我们来看一下整个循环,其中我把一些 PHP 评论代码也插了进去,供参考:

<?php /* The Loop — with comments! */ ?> <?php while ( have_posts() ) : the_post() ?>    <?php /* Create a div with a unique ID thanks to the_ID() and semantic classes with post_class() */ ?> 				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>				  <?php /* an h2 title */ ?> 					<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'your-theme'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>    <?php /* Microformatted, translatable post meta */ ?> 					<div class="entry-meta"> 						<span class="meta-prep meta-prep-author"><?php _e('By ', 'your-theme'); ?></span>  						<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'your-theme' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>  						<span class="meta-sep"> | </span> 						<span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'your-theme'); ?></span>  						<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>  						<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>  					</div><!-- .entry-meta -->   <?php /* The entry content */ ?> 					<div class="entry-content">	  <?php the_content( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'your-theme' )  ); ?>  <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>  					</div><!-- .entry-content -->   <?php /* Microformatted category and tag links along with a comments link */ ?> 					<div class="entry-utility">  						<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'your-theme' ); ?></span><?php echo get_the_category_list(', '); ?></span>  						<span class="meta-sep"> | </span> 						<?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'your-theme' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>  						<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'your-theme' ), __( '1 Comment', 'your-theme' ), __( '% Comments', 'your-theme' ) ) ?></span>  						<?php edit_post_link( __( 'Edit', 'your-theme' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>  					</div><!-- #entry-utility --> 				</div><!-- #post-<?php the_ID(); ?> -->    <?php /* Close up the post div and then end the loop with endwhile */ ?>   <?php endwhile; ?>

关于导航:

现在我们需要一种方法来导航我们的文章。这种方法会用到两个 WordPress 模板标签,即 next_posts_link() 和 previous_posts_link()。这两个标签的功能并非您想象的那样,我想 WordPress codex 的下面解释最恰当:

next posts link
这是链接到较旧的文章的,但是 "next posts" 一词令人困惑。
 
previous posts link
这是链接到较新的文章的,但是 "previous posts" 一词令人困惑。

和 index.php 里面的所有内容一样,第一次创建文章导航需要非常细心,因为几乎博客的每一个页面都会用到它。

我喜欢在正文的上面和下面分别使用文章导航。当然这取决于具体的情况,您也可以不用。不过用了也没关系,您可以随时通过以下代码隐藏它:

.single #nav-above {     display:none; }

这个 CSS 代码将会隐藏单独日志内容上面的文章导航功能。

如果没有导航网页,我们也想隐藏文章导航功能。比如,如果没有较旧的文章,我们就不想输出任何的导航代码。这点可以通过以下代码实现:

<?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>  <?php } ?>

这个代码表示如果循环页面里最大的页码大于1,那么就输出导航代码,执行导航功能。

下面就是我们需要的代码,其中顶部导航代码在循环开始之前,底部导航代码在循环结束之后:

<?php /* Top post navigation */ ?>  <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>  				<div id="nav-above" class="navigation"> 					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'your-theme' )) ?></div>  					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'your-theme' )) ?></div>  				</div><!-- #nav-above --> <?php } ?>
<?php /* Bottom post navigation */ ?>  <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>  				<div id="nav-below" class="navigation"> 					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'your-theme' )) ?></div>  					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'your-theme' )) ?></div>  				</div><!-- #nav-below --> <?php } ?>

还差一样 index.php 文件就完成了,那就是在 get_footer() 之前添加以下代码:

<?php get_sidebar(); ?>

如何创建 WordPress 主题

这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。

注释:

1. 根据本文和 index.php 之前已有的内容,最终的 index.php 文件里面的代码如下:

<?php get_header(); ?>  <div id="container">   		<div id="content"> 		<?php /* Top post navigation */ ?>    <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>    				<div id="nav-above" class="navigation"> 					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'eblog' )) ?></div>    					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'eblog' )) ?></div>    				</div><!-- #nav-above --> <?php } ?>   		<?php while ( have_posts() ) : the_post() ?>  <?php /* Create a div with a unique ID thanks to the_ID() and semantic classes with post_class() */ ?> 				<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>				    <?php /* an h2 title */ ?> 					<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( __('Permalink to %s', 'eblog'), the_title_attribute('echo=0') ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>      <?php /* Microformatted, translatable post meta */ ?> 					<div class="entry-meta"> 						<span class="meta-prep meta-prep-author"><?php _e('By ', 'eblog'); ?></span>    						<span class="author vcard"><a class="url fn n" href="<?php echo get_author_link( false, $authordata->ID, $authordata->user_nicename ); ?>" title="<?php printf( __( 'View all posts by %s', 'eblog' ), $authordata->display_name ); ?>"><?php the_author(); ?></a></span>    						<span class="meta-sep"> | </span> 						<span class="meta-prep meta-prep-entry-date"><?php _e('Published ', 'eblog'); ?></span>    						<span class="entry-date"><abbr class="published" title="<?php the_time('Y-m-d\TH:i:sO') ?>"><?php the_time( get_option( 'date_format' ) ); ?></abbr></span>    						<?php edit_post_link( __( 'Edit', 'eblog' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t" ) ?>    					</div><!-- .entry-meta -->   <?php /* The entry content */ ?>   						<div class="entry-content">	  <?php the_content( __( 'Continue reading <span class="meta-nav">&raquo;</span>', 'eblog' )  ); ?>    <?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'eblog' ) . '&after=</div>') ?>    					</div><!-- .entry-content -->   <?php /* Microformatted category and tag links along with a comments link */ ?> 					<div class="entry-utility">   						<span class="cat-links"><span class="entry-utility-prep entry-utility-prep-cat-links"><?php _e( 'Posted in ', 'eblog' ); ?></span><?php echo get_the_category_list(', '); ?></span>    						<span class="meta-sep"> | </span> 						<?php the_tags( '<span class="tag-links"><span class="entry-utility-prep entry-utility-prep-tag-links">' . __('Tagged ', 'eblog' ) . '</span>', ", ", "</span>\n\t\t\t\t\t\t<span class=\"meta-sep\">|</span>\n" ) ?>    						<span class="comments-link"><?php comments_popup_link( __( 'Leave a comment', 'eblog' ), __( '1 Comment', 'eblog' ), __( '% Comments', 'eblog' ) ) ?></span>    						<?php edit_post_link( __( 'Edit', 'eblog' ), "<span class=\"meta-sep\">|</span>\n\t\t\t\t\t\t<span class=\"edit-link\">", "</span>\n\t\t\t\t\t\n" ) ?>    					</div><!-- #entry-utility --> 				</div><!-- #post-<?php the_ID(); ?> -->      <?php /* Close up the post div and then end the loop with endwhile */ ?>     <?php endwhile; ?> <?php /* Bottom post navigation */ ?>    <?php global $wp_query; $total_pages = $wp_query->max_num_pages; if ( $total_pages > 1 ) { ?>    				<div id="nav-below" class="navigation"> 					<div class="nav-previous"><?php next_posts_link(__( '<span class="meta-nav">&laquo;</span> Older posts', 'eblog' )) ?></div>    					<div class="nav-next"><?php previous_posts_link(__( 'Newer posts <span class="meta-nav">&raquo;</span>', 'eblog' )) ?></div>    				</div><!-- #nav-below --> <?php } ?>   		</div><!-- #content -->    		</div><!-- #container -->   		<div id="primary" class="widget-area"> 		</div><!-- #primary .widget-area -->   	    		<div id="secondary" class="widget-area"> 			</div><!-- #secondary --> 			<?php get_sidebar(); ?>  			<?php get_footer(); ?>

2、最后 <?php the_content(); ?><?php the_excerpt(); ?> 都不见了。

3、包含 /* 这个符号的整个语句 (例如 <?php /* Top post navigation */ ?>)是可以删掉的。

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


WordPress 主题 Header 模板

Posted: 30 Jun 2009 07:06 PM PDT

原文:The WordPress Theme Header Template

原文作者:Ian Stewart

译文:WordPress 主题 Header 模板

译文作者:Young

现在我们进一步学习如何创建 header.php 以及如何验证 HTML 主题。这个教程将会有很多 PHP 代码,但是不用失望。同时,我们将学习两个必需的搜索引擎优化技巧,并开始通过 functions.php 文件来装饰您的主题。

Head 部分

此刻您的空白 WordPress 主题还没有通过技术验证,因为它缺乏一个文档来告诉浏览器如何解释 HTML 语言。我们将使用 XHTML 过渡文档 —— 当然还有其他的文档,但是 XHTML 是最好的。

打开 header.php 文件,并把以下代码粘贴到最顶部:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

同时,我们将为 HTML 标签添加一些属性,以便页面更加适合浏览器。用以下代码替代 "<html>" 标签:

<html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>

现在我们正在创建 WordPress 主题的 <head> 部分。<head> 部分包括网页的元信息。例如出现于浏览器最顶部(和搜索引擎搜索结果)的文件名、样式表和 RSS Feed 的链接。

先打开 functions.php 文件,我们准备添加一些有用的功能,这些功能用于为文件标题提供页面号码。

先在 functions.php 添加:

<?php

然后添加以下代码:

// Make theme available for translation // Translations can be filed in the /languages/ directory load_theme_textdomain( 'your-theme', TEMPLATEPATH . '/languages' );    $locale = get_locale(); $locale_file = TEMPLATEPATH . "/languages/$locale.php";  if ( is_readable($locale_file) ) 	require_once($locale_file);   // Get the page number function get_page_number() {      if ( get_query_var('paged') ) {         print ' | ' . __( 'Page ' , 'your-theme') . get_query_var('paged');      } } // end get_page_number

第一个功能告诉 WordPress 我们希望我们的主题可以被翻译成不同的语言,翻译文件可以在主题目录的 "languages"文件夹里面找到。如果您准备创建一个 WordPress 主题,就应该尽量使得主题所有的内容都可以翻译。您永远不会知道自己或者别人什么时候需要用另外一种语言显示硬编码内容。

对于下一个功能 get_the_page_number(), 您将会看到一些翻译文本,例如:

__( 'Page ' , 'your-theme')

可以翻译的文本是主题名(your-theme) 前面的 "Page"。直截了当,但是还有其他方式可以写翻译文本,以后用到的时候我们再作说明。

您可以猜测 get_page_number() 是做什么用到吗?如果您看功能里面的代码,您会发现我们在检查一个 IF 语句是否一个编好号的页面。当您点击 "较旧的文章"的时候,这个功能就会在地址栏显示当前页面号码。

如果您对 PHP 很陌生,我希望您记住双斜杠后面的所有内容都是被忽略的,只作为一个注释。后面您会看到很多双斜杠。

现在我们回到 header.php 文件。

用以下代码替代 <head></head>

<head profile="http://gmpg.org/xfn/11">     <title><?php         if ( is_single() ) { single_post_title(); }         elseif ( is_home() || is_front_page() ) { bloginfo('name'); print ' | '; bloginfo('description'); get_page_number(); }          elseif ( is_page() ) { single_post_title(''); }         elseif ( is_search() ) { bloginfo('name'); print ' | Search results for ' . wp_specialchars($s); get_page_number(); }          elseif ( is_404() ) { bloginfo('name'); print ' | Not Found'; }          else { bloginfo('name'); wp_title('|'); get_page_number(); }     ?></title>    	<meta http-equiv="content-type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />    	<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_url'); ?>" />   	<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>    	<?php wp_head(); ?>   	<link rel="alternate" type="application/rss+xml" href="<?php bloginfo('rss2_url'); ?>" title="<?php printf( __( '%s latest posts', 'your-theme' ), wp_specialchars( get_bloginfo('name'), 1 ) ); ?>" />  	<link rel="alternate" type="application/rss+xml" href="<?php bloginfo('comments_rss2_url') ?>" title="<?php printf( __( '%s latest comments', 'your-theme' ), wp_specialchars( get_bloginfo('name'), 1 ) ); ?>" />  	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" /> </head>

如果您觉得这些代码像教条,没关系,我会重点解释。

一个搜索引擎优化的标题只包含单独日志和静态页面的标题。当然,还要通过 get_page_number() 显示 "较旧文章"页面的页号。

    <title><?php         if ( is_single() ) { single_post_title(); }         elseif ( is_home() || is_front_page() ) { bloginfo('name'); print ' | '; bloginfo('description'); get_page_number(); }          elseif ( is_page() ) { single_post_title(''); }         elseif ( is_search() ) { bloginfo('name'); print ' | Search results for ' . wp_specialchars($s); get_page_number(); }          elseif ( is_404() ) { bloginfo('name'); print ' | Not Found'; }          else { bloginfo('name'); wp_title('|'); get_page_number(); }     ?></title>

下面是一些关于页面内容的元信息:

	<meta http-equiv="content-type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />

样式表链接:

	<link rel="stylesheet" type="text/css" href="<?php bloginfo('stylesheet_url'); ?>" />

以下这个脚本的调用让我们可以使用评论功能:

	<?php if ( is_singular() ) wp_enqueue_script( 'comment-reply' ); ?>

调用插件和其他功能的代码:

	<?php wp_head(); ?>

RSS Feed 和 Pingback 链接:

 <link rel="alternate" type="application/rss+xml" href="<?php bloginfo('rss2_url'); ?>" title="<?php printf( __( '%s latest posts', 'your-theme' ), wp_specialchars( get_bloginfo('name'), 1 ) ); ?>" />  	<link rel="alternate" type="application/rss+xml" href="<?php bloginfo('comments_rss2_url') ?>" title="<?php printf( __( '%s latest comments', 'your-theme' ), wp_specialchars( get_bloginfo('name'), 1 ) ); ?>" />  	<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />

Header 部分

现在我们想在博客上添加标题,链接到主页并且显示博客描述和菜单。

我们将在 hearder.php 的 #branding DIV 标签里面添加博客标题和描述。但是我们将做得和其他大多数的主题有点不一样。

您会发现很多 WordPress 主题都会告诉搜索引擎每个页面最重要的东西是博客标题。这些主题通过 h1 标签来打包标题,h1 标签意味著"这个页面就是讲这个的"。我和其他一小部分主题开发者认为这是一个坏主意。例如,定义您现在看到这个页面最重要的文本不是 "ThemeShaper" 而是 "WordPress 主题 Header 模板"。而在主页,最重要的东西是博客的描述。博客的描述实际上总体描述了博客的所有内容。

幸运的是,这个很容易做到。

我们用 WordPress 的条件标签以及简单的 HTML 代码来设置博客标题以及描述:

			<div id="branding"> 				<div id="blog-title"><span><a href="<?php bloginfo( 'url' ) ?>/" title="<?php bloginfo( 'name' ) ?>" rel="home"><?php bloginfo( 'name' ) ?></a></span></div>  <?php if ( is_home() || is_front_page() ) { ?> 		    		<h1 id="blog-description"><?php bloginfo( 'description' ) ?></h1>  <?php } else { ?> 		    		<div id="blog-description"><?php bloginfo( 'description' ) ?></div>  <?php } ?> 			</div><!-- #branding -->

所以,博客标题通过一个简单的 DIV 标签搞定,博客描述则通过 PHP 语句和一些 Wordpress 条件标签实现,要不使用主页或者首页的非常重要的 h1 打包描述,要不就使用其他页面的 DIV 标签里面的相关标题。

对初学者来讲,现在最好回过头看看我们的代码。在上面的代码里面,我们使用了 WordPress 的模板标签 bloginfo() 用来获取 WordPress 博客的 URL、名称和描述。它可以用来获取 WordPress 博客 20 多种不同的信息。理解这个就理解了 WordPress 主题。我们通过 HTML 结构、包含 PHP 代码的 WordPress 模板标签来实现这种功能。真的很简单。只要见惯那些模板标签以及一些 IF 语句和一些 PHP 循环。

在 #access DIV 标签里面,我们会添加一个跳转链接,以便别人通过屏幕阅读器浏览您的博客的时候可以跳过菜单直接看文章内容。

<div class="skip-link"><a href="#content" title="<?php _e( 'Skip to content', 'your-theme' ) ?>"><?php _e( 'Skip to content', 'your-theme' ) ?></a></div>

添加页面菜单很简单,只有一个模板标签,一个函数:

				<?php wp_page_menu( 'sort_column=menu_order' ); ?>

很简单,对吧?所以您的 #access DIV 标签应该像这样子:

			<div id="access"> 				<div class="skip-link"><a href="#content" title="<?php _e( 'Skip to content', 'your-theme' ) ?>"><?php _e( 'Skip to content', 'your-theme' ) ?></a></div>  				<?php wp_page_menu( 'sort_column=menu_order' ); ?> 			</div><!-- #access -->

至此,您的 WordPress 主题的 Header 模板已经是经过搜索引擎优化的了。

如何创建 WordPress 主题

这篇文章是如何创建 WordPress 主题教程系列的其中一部分,该系列将会教您如何从零开始创建强大的 WordPress 主题。建议您从头开始阅读这个系列并自己动手编写一些漂亮的代码。

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


FeedBurner 出现故障?

Posted: 29 Jun 2009 08:52 AM PDT

为了跟踪 RSS Feed 更新是否正常,我是有订阅自己的博客的,但是有时候会忘记检查。所以直到早上,我才发现 FeedBurner 已经有好几天没有更新精博的 RSS Feed 了 ,最近一次更新时间为 6 月 25 日。

对这个问题我相当紧张,于是去寻求解决的方法。

首先,我到 FeedBurner 控制面板的 Troubleshootize 页面进行检测:

Troubleshootize

我先尝试 Ping,结果显示 Ping 成功,但是 FeedBurner 烧制的 RSS Feed 还是没有更新。接着尝试 "Resync Now",结果出现两个错误提示,其中一个好像是说"超时( Time Out)",多次尝试都是一样。后来尝试 "help topic"——是用来控制 Feed 在 512K 之内的,精博的 Feed 一次最多只能显示 10 篇文章,况且还未曾一天超过 3 篇,所以应该不会是大小的问题,但是我还是试了,结果出现错误提示。

自从接触 WordPress,我就未尝停止过折腾,因此也就越来越能折腾。于是我想会不会是插件的问题,有些插件(例如 YARPP) 升级后是可能出现问题的。先是禁用最近更新的两个插件,无效;接着禁用所有的插件,仍然无效。所以看来也不是插件的问题。

后来,我怀疑是不是连原始的 Feed 都无法读取,测试后发现不是。

再后来,我又怀疑是不是其他服务也无法更新精博的 Feed,于是我检查了 FeedSky 烧制的 Feed,发现答案还是否定的。

折腾了一个上午,找不到原因,问题也没有解决,但是我已经无计可施了,于是就出去玩了。

晚上 10 点左右,精博突然无法访问,于是联系虚拟主机客服,他竟然能访问并且截图为证,真是怪事。精博可以访问之后,我再次登录 FeedBurner 后台 Troubleshootize 页面,点击 "Resync Now",结果居然显示"成功"!再 Ping 一次之后, Google Reader 里面就可以显示最新的 Feed 了。于是我怀疑是 FeedBurner 的问题。

自从 Google 收购 FeedBurner 之后,FeedBurner 不再被墙,但是除了方便在 Feed 里面添加 Google AdSense 广告之外,Google 好像没有对 FeedBurner 做任何的改进。职业博客作者 Darren Rowse 为此还写了一封信给 Google,希望 FeedBurner 能有所改进。

据我所知,中文博客有很多是通过 FeedSky 来烧制和管理 RSS Feed 的,但是精博的 Feed 被 FeedSky 烧制得不好,段落与段落之间没有正常的间隙,如下图:

elbog-in-feedsky

而正常的排版应该是这样的:

eblog in feedburner

这就是为什么我不用 FeedSky 的原因了 —— 虽然精博的 RSS Feed 也被 FeedSky 烧制。

最后,许个愿,希望 FeedBurner 烧制的精博 RSS Feed 以后能 Day Day Update。

更新:此文发表后,发现 FeedBurner 又无法更新更新精博的 RSS Feed,点击 "Resync Now" 又出现错误提示:

Feedburner Failed

-------------------------------------


© 逛逛精品博客,看看博客精品。| 转载请遵循"署名-非商业性使用"的创作共用协议。


没有评论:

发表评论