Display Tags In A Dropdown Menu 在下拉菜单中显示的标签
In your theme folder, paste the following code to the functions.php file. If you don’t have a functions.php file, create one.
在你的主题文件夹,下面的代码粘贴到functions.php文件。如果你没有一个functions.php文件,创建一个。
<?php
function dropdown_tag_cloud( $args = '' ) {
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC',
'exclude' => '', 'include' => ''
);
$args = wp_parse_args( $args, $defaults );
$tags = get_tags( array_merge($args, array('orderby' => 'count', 'order' => 'DESC')) ); // Always query top tags
if ( empty($tags) )
return;
$return = dropdown_generate_tag_cloud( $tags, $args ); // Here's where those top tags get sorted according to $args
if ( is_wp_error( $return ) )
return false;
else
echo apply_filters( 'dropdown_tag_cloud', $return, $args );
}
function dropdown_generate_tag_cloud( $tags, $args = '' ) {
global $wp_rewrite;
$defaults = array(
'smallest' => 8, 'largest' => 22, 'unit' => 'pt', 'number' => 45,
'format' => 'flat', 'orderby' => 'name', 'order' => 'ASC'
);
$args = wp_parse_args( $args, $defaults );
extract($args);
if ( !$tags )
return;
$counts = $tag_links = array();
foreach ( (array) $tags as $tag ) {
$counts[$tag->name] = $tag->count;
$tag_links[$tag->name] = get_tag_link( $tag->term_id );
if ( is_wp_error( $tag_links[$tag->name] ) )
return $tag_links[$tag->name];
$tag_ids[$tag->name] = $tag->term_id;
}
$min_count = min($counts);
$spread = max($counts) - $min_count;
if ( $spread <= 0 )
$spread = 1;
$font_spread = $largest - $smallest;
if ( $font_spread <= 0 )
$font_spread = 1;
$font_step = $font_spread / $spread;
// SQL cannot save you; this is a second (potentially different) sort on a subset of data.
if ( 'name' == $orderby )
uksort($counts, 'strnatcasecmp');
else
asort($counts);
if ( 'DESC' == $order )
$counts = array_reverse( $counts, true );
$a = array();
$rel = ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) ? ' rel="tag"' : '';
foreach ( $counts as $tag => $count ) {
$tag_id = $tag_ids[$tag];
$tag_link = clean_url($tag_links[$tag]);
$tag = str_replace(' ', ' ', wp_specialchars( $tag ));
$a[] = "t<option value='$tag_link'>$tag ($count)</option>";
}
switch ( $format ) :
case 'array' :
$return =& $a;
break;
case 'list' :
$return = "<ul class='wp-tag-cloud'>nt<li>";
$return .= join("</li>nt<li>", $a);
$return .= "</li>n</ul>n";
break;
default :
$return = join("n", $a);
break;
endswitch;
return apply_filters( 'dropdown_generate_tag_cloud', $return, $tags, $args );
}
?>
Now open the file where you want the list to be displayed and paste the following code现在打开文件在你想要的列表显示和粘贴以下代码:
<select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="#">Tags</option>
<?php dropdown_tag_cloud('number=0&order=asc'); ?>
</select>
Via: WpRecipes Credits: WpHacks
Get Posts Published Between Two Particular Dates有两个特别的日期之间发表的帖子
Just before the loop starts, paste the following code. Change the dates on line 3 according to your needs
就在循环开始,粘贴以下代码。根据您的需要更改日期在3线
.
<?php
function filter_where($where = '') {
$where .= " AND post_date >= '2009-05-01' AND post_date <= '2009-05-15'";
return $where;
}
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>
Get Posts With A Specific Custom Field & Value 获取文章用一个特定的自定义字段和值
Add the query_posts() function just before the Loop. Change the meta_key
and meta_value
accordingly. The example shown below will get posts with custom field “review_type” with value “movie”.
<?php query_posts('meta_key=review_type&meta_value=movie'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
...
...
Via: WpRecipes Credits: John Kolbert
Get Latest Sticky Posts 获取最新的置顶文章
Paste the following code before the loop. This code will retrieve the 5 most recent sticky posts. To change the number of retrieved posts, just change the 5 by the desired value on line 4.在loop循环代码钱粘贴下面的代码,此代码将检索最近的5篇文章,检索到的文章数目改变
<?php
$sticky = get_option('sticky_posts');
rsort( $sticky );
$sticky = array_slice( $sticky, 0, 5);
query_posts( array( 'post__in' => $sticky, 'caller_get_posts' => 1 ) );
?>
Via: WpRecipes Credits: Justin Tadlock
Automatically Insert Content In Your Feeds 在 Feeds中自动插入内容
In your theme folder, functions.php file, paste the following code. This code will automatically insert the content after each post in your RSS feeds. Hence you can use this code to insert ads or promotional text.
在你的主题文件夹,functions.php文件,粘贴以下代码。此代码将自动插入的内容在你的RSS供稿每个后。因此,您可以使用此代码插入广告或宣传的文字。
function insertFootNote($content) {
if(!is_feed() && !is_home()) {
$content.= "<h4>Enjoyed this article?</h4>";
$content.= "<p>Subscribe to our <a href='#'>RSS feed</a></p>";
}
return $content;
}
add_filter ('the_content', 'insertFootNote');
Via: WpRecipes Credits: Cédric Bousmane
Display The Most Commented Posts 显示评论最多的文章
Just paste the following code in your template file where you want to display the most commented posts (eg. sidebar.php). To change the number of displayed posts, simply change the 5 on line 3.
只是粘贴以下代码在你的模板文件,你想显示评论最多的文章里(如侧边栏。PHP)。改变显示的文章数目,仅仅改变第三行的数字5。
<h2>Popular Posts</h2>
<ul>
<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5");
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%20echo%20get_permalink($postid);%20?>" title="<?php echo $title ?>">
<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>
<?php } } ?>
</ul>
Via: WpRecipes Credits: ProBlogDesign
Display Most Commented Posts In 2008 显示在2008年评论最多的文章
<h2>Most commented posts from 2008</h2>
<ul>
<?php
$result = $wpdb->get_results("SELECT comment_count,ID,post_title, post_date FROM $wpdb->posts WHERE post_date BETWEEN '2008-01-01' AND '2008-12-31' 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%20echo%20get_permalink($postid);%20?>"><?php echo $title ?></a></li>
<?php }
}
?>
</ul>
Via: WpRecipes
Display Related Posts Based On Post Tags 显示基于文章标签相关的posts
This code will display related posts based on the current post tag(s). It must be pasted within the loop.
下面的代码将根据当前文章的tag标签显示相关文章。它必须贴在循环
<?php
//for use in the loop, list 5 post titles related to first tag on current post
$tags = wp_get_post_tags($post->ID);
if ($tags) {
echo 'Related Posts';
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>5,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><a href="<?php%20the_permalink()%20?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php
endwhile;
}
}
?>
Via: WpRecipes Credits: MichaelH
Display The Number Of Search Results 显示的搜索结果数
Open search.php, copy and paste the following code.打开search.php文件,粘贴下面的代码
<h2 class="pagetitle">Search Results for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e(''); _e('<span class="search-terms">'); echo $key; _e('</span>'); _e(' — '); echo $count . ' '; _e('articles'); wp_reset_query(); ?></h2>
Via: WpRecipes Credits: ProBlogDesign
Display The Comment Page Number In The <Title> Tag 在title标签中显示评论页面数量
Open the header.php file. Paste the following code in between the 打开header.php文件,把下面代码粘贴到title标题标签里面
<?php if ( $cpage < 2 ) {}
else { echo (' - comment page '); echo ($cpage);}
?>
Via: WpRecipes Credits: Malcolm Coles
Display The Future Posts 显示最流行的文章
The following code will display 10 future posts.下面的代码显示最流行的十篇文章
<p>Future events</p>
<?php query_posts('showposts=10&post_status=future'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<p><?php the_title(); ?> <?php the_time('j. F Y'); ?></p>
<?php endwhile; else: ?><p>No future posts.</p><?php endif; ?>
Via: WpRecipes
Randomize Posts Order 随即文章排序
To randomize posts order, just add the following code before the Loop. 如果要随即文章排序,将下面代码放到循环代码中
query_posts('orderby=rand');
//the Loop here...
Via: WpRecipes
Display Word Count Of The Post显示帖子字数
Open single.php and paste the following code where you want to display the word count.打开single.php将下面代码粘贴到你要显示文章字数的地方
<?php function count_words($str){
$words = 0;
$str = eregi_replace(" +", " ", $str);
$array = explode(" ", $str);
for($i=0;$i < count($array);$i++)
{
if (eregi("[0-9A-Za-zÀ-ÖØ-öø-ÿ]", $array[$i]))
$words++;
}
return $words;
}?>
Word count: <?php echo count_words($post->post_content); ?>
Via: WpRecipes
Fetch RSS Feeds 获取RSS供稿
To display RSS feeds, you can use the WordPress built-in RSS parser. To do so, include the rss.php file, and then use its wp_rss()
function.
<?php include_once(ABSPATH.WPINC.'/rss.php');
wp_rss('http://feeds2.feedburner.com/WebDesignerWall', 5); ?>
Via: WpRecipes
Highlight Searched Text In Search Results 突出显示在搜索结果中搜索文本
Open search.php file and find the the_title()
function. Replace it with the following:
echo $title;
Now, just before the modified line, add this code:
<?php
$title = get_the_title();
$keys= explode(" ",$s);
$title = preg_replace('/('.implode('|', $keys) .')/iu',
'<strong class="search-excerpt">