方便实用的 WordPress 小技巧

自从开 Mcooo Blog 以后为实现某些功能,会比较喜欢到处逛一些国外的博客,经常会看到一些非常实用的技巧。选择了一些本站在用和自己觉得较好的技巧收集在一起来与大家分享一下。

两列显示WordPress的分类:

<?php
$cats = explode("<br />",wp_list_categories('title_li=&echo=0&depth=1&style=none'));
$cat_n = count($cats) - 1;
$cat_col = round($cat_n / 2);
for ($i=0;$i<$cat_n;$i++){
if ($i<$cat_col){
$cat_left = $cat_left.'<li>'.$cats[$i].'</li>';
}
elseif ($i>=$cat_col){
$cat_right = $cat_right.'<li>'.$cats[$i].'</li>';
}
}
?>
<ul>
<?php echo $cat_left;?>
</ul>
<ul>
<?php echo $cat_right;?>
</ul>

CSS代码

.right {float:left; width:200px;}
.left {float:left; width:200px;}

两列显示WordPress的页面:

<?php
$page_s = explode("</li>",wp_list_pages('title_li=&echo=0&depth=1&style=none'));
$page_n = count($page_s) - 1;
$page_col = round($page_n / 2);
for ($i=0;$i<$page_n;$i++){
if ($i<$page_col){
$page_left = $page_left.''.$page_s[$i].'</li>';
}
elseif ($i>=$page_col){
$page_right = $page_right.''.$page_s[$i].'</li>';
}
}
?>
<ul>
<?php echo $page_left; ?>
</ul>
<ul>
<?php echo $page_right; ?>
</ul>

CSS代码:

.right {float:left; width:200px;}
.left {float:left; width:200px;}

版权年限:

手动地输入版权信息

Copyright &copy; < ?php echo date("Y");?>

全自动式只需打开主题中的functions.php文件,添加如下代码

function copyrightDate() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
");
if($copyright_dates) {
$copyright = "Copyright &copy; " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
echo $copyright . "&nbsp;" . get_bloginfo('name');
}
}
add_filter('wp_footer', 'copyrightDate');

自动读取文章中的第一个图片,打开functions.php文件,添加如下的php代码:

function get_first_image() {
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
$first_img = $matches [1] [0];
if(empty($first_img)){ //Defines a default image
$first_img = "/images/default.jpg";
}
return $first_img;
}

调用该函数

<?php echo get_first_image() ?>

手动定义显示文章完整或摘录的函数,在您的index.php文件,取代您的循环函数:

<?php if (have_posts()) :
while (have_posts()) : the_post();
$customField = get_post_custom_values("full");
if (isset($customField[0])) {
//Custom field is set, display a full post
the_title();
the_content();
} else {
// No custom field set, let’s display an excerpt
the_title();
the_excerpt();
endwhile;
endif;
?>

禁用评论超过30天WordPress的帖子,打开functions.php文件,添加如下的php代码:

function close_comments( $posts ) {
if ( !is_single() ) { return $posts; }
if ( time() - strtotime( $posts[0]->post_date_gmt ) > ( 30 * 24 * 60 * 60 ) ) {
$posts[0]->comment_status = 'closed';
$posts[0]->ping_status = 'closed';
}
return $posts;
}
add_filter( 'the_posts', 'close_comments' );

对你的WordPress的访客,显示一个欢迎回来的信息:

<?php
if(isset($_COOKIE['comment_author_'.COOKIEHASH])) {
$lastCommenter = $_COOKIE['comment_author_'.COOKIEHASH];
echo "Welcome Back ". $lastCommenter ."!";
} else {
echo "Welcome, Guest!";
}
?>

WordPress中显示一个下拉菜单的标签,打开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(' ', '&nbsp;', 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'>\n\t<li>";
$return .= join("</li>\n\t<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 );
}

调用该函数

<select name="tag-dropdown" onchange="document.location.href=this.options[this.selectedIndex].value;">
<option value="#">Liste dauteurs</option>
<?php dropdown_tag_cloud('number=0&order=asc'); ?>
</select>

添加一个电子邮件这个按钮:

<script type="text/javascript">
<!-- Begin
function isPPC() {
if (navigator.appVersion.indexOf("PPC") != -1) return true;
else return false;
}
if(isPPC()) {
document.write('<a HREF=\"mailto:\?subject\=Take a look at this page I found, ' + document.title + '?body=You can see this page at: ' + window.location + '\" onMouseOver="window.status=\'Send your friends e-mail about this page\'; return true" TITLE="Send your friends e-mail about this page">Email to a Friend<\/a>');
}
else { document.write('<a HREF=\"mailto:\?body\=Take a look at this page I found titled ' + document.title + '. You can see this page at: ' + window.location + '\" onMouseOver="window.status=\'Send your friends e-mail about this page\'; return true" TITLE="Send your friends e-mail about this page" rel="nofollow">Email This!<\/a>');
}
// End –>
</script>

使用PHP的preg_replace函数,显示我们要显示的东西:

<ul id="nav">
<li><a href="<?php echo get_option('home'); ?>/"><span>Home</span></a></li>
<?php echo preg_replace('@\<li([^>]*)>\<a([^>]*)>(.*?)\<\/a>@i', '<li$1><a$2><span>$3</span></a>', wp_list_pages('echo=0&orderby=name&exlude=181&title_li=&depth=1')); ?>
</ul>

<ul id="nav">
<li><a href="<?php echo get_option('home'); ?>/"><span>Home</span></a></li>
<?php echo preg_replace('@\<li([^>]*)>\<a([^>]*)>(.*?)\<\/a>@i', '<li$1><a$2><span>$3</span></a>', wp_list_categories('echo=0&orderby=name&exlude=181&title_li=&depth=1')); ?>
</ul>

一个下拉框显示档案:

<select name=\"archive-dropdown\" onChange='document.location.href=this.options[this.selectedIndex].value;'>
<option value=\"\"><?php echo attribute_escape(__('Select Month')); ?></option>
<?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>

在下拉框显示分类:

<form action="<?php bloginfo('url'); ?>/" method="get">
<?php
$select = wp_dropdown_categories('show_option_none=Select category&show_count=1&orderby=name&echo=0');
$select = preg_replace("#<select([^>]*)>#", "<select$1 onchange='return this.form.submit()'>", $select); echo $select; ?>
<noscript><input type="submit" value="View" /></noscript>
</form>

Tag(s): , ,
  • QQshuqian
  • Del.icio.us
  • Baidu
  • Google
  • Fanfou
  • FriendFeed
15 Comments Contributed by Visitors
  1. rebutton林木木 | 22:56@10-04-17 1 林木木

    恩,做个标记~

  2. rebuttonedikud | 23:03@10-04-17 2 edikud

    @林木木 刚发布你就来了,很是巧合啊!

  3. rebutton阿修 | 09:45@10-04-18 3 阿修

    这个不错,有几个挺有用处~~

  4. rebuttonwahyle | 10:30@10-04-18 4 wahyle

    我也做个标记,有空折腾~

  5. rebuttonedikud | 13:31@10-04-18 5 edikud

    @阿修
    @wahyle 有空大家都折腾一下~ :grin:

  6. rebuttonzwwooooo | 13:42@10-04-18 6 zwwooooo

    很实用,话说你的代码能否定义一下样式,这样跟文章一样贴出来感觉很“乱” :shock:

  7. rebuttonFirm | 15:14@10-04-18 7 Firm

    CSS拿去改改

  8. rebuttonedikud | 21:08@10-04-18 8 edikud

    @zwwooooo 好的,我就定义一下样式吧!

  9. rebuttonedikud | 21:09@10-04-18 9 edikud

    @Firm css?你是指哪里的css...

  10. rebutton163导航网博客 | 23:52@10-04-19 10 163导航网博客

    其实没有必要这么麻烦。

  11. rebuttonedikud | 18:36@10-04-21 11 edikud

    @163导航网博客 呵呵,可以说下你有什么好的方法喔!

  12. rebuttonJutoy | 15:38@10-04-22 12 Jutoy

    我时不时地过来偷一下师~

  13. rebuttonedikud | 23:37@10-04-23 13 edikud

    @Jutoy 欢迎啊!

  14. rebuttonMOPVHS | 20:43@10-05-03 14 MOPVHS

    震惊地飘过....

  15. rebuttonedikud | 22:16@10-05-07 15 edikud

    @MOPVHS 记得要回来啊! :grin:

Leave Comments Here...

:smile: :grin: :sad: :eek: :shock: :???: :cool: :lol: :mad: :razz: :oops: :cry: :evil: :twisted: :wink: :?: :idea: :neutral:

必填

必填&保密

随您

Ctrl+Enter

  • 已部署"白忙活" Anti-Spam 系统,猪肉 Spammer 请注意查看源代码链接处。
  • 本站不会泄露您的邮件地址,敬请放心。
  • 支持常用 HTML 代码调用,插入 PHP 代码需注意:< = &lt;> = &gt;
  • 仅当某人针对您的评论做出回应时,您才会收到由本站发出的通知信件。
  • 如您希望以相同方式通知某位网友,请点击 Reply 按钮,或自行输入 @对方名字:
  • 鼠标移至评论列表中的 @someone: 区域即可自动显示 someone 的评论内容。

您直接访问了本站! 嘿嘿