Home » Wordpress 代码

WordPress 主题应用代码

如果你是一个刚刚用上wordpress的新手的话,你有可能会遇到这种情况:你看中了一款自己很喜欢的主题,但主题的侧边栏或者哪一部分缺少想“最新评论”等内容。当然,这时你可以在后台添加侧边栏,但这样主题原先有的所有侧边栏都不会显示了,也有可能因为这样而失去了主题原来的特色使得你不得不放弃自己喜欢的主题。事实上,在WordPress后台的所有侧边栏都是可以通过代码来添加的。下面的这些代码就是我收集的一部分wordpress主题应用代码,该收集还有待完善,如果你希望下面出现哪些代码,可以留言告诉我。

最新文章代码

下面的例子显最新的5篇文章:

<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
</ul>

文章更新代码

<?php
$today = current_time('mysql', 1);
$howMany = 5; //Number of posts you want to display
if ( $recentposts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE post_status = 'publish' AND post_modified_gmt < '$today' ORDER BY post_modified_gmt DESC LIMIT $howMany")):
?>
<h2><?php _e("Recent Updates"); ?></h2>
<ul>
<?php
foreach ($recentposts as $post) {
if ($post->post_title == '') $post->post_title = sprintf(__('Post #%s'), $post->ID);
echo "<li><a href='".get_permalink($post->ID)."'>";
the_title();
echo '</a></li>';
}
?>
</ul>
<?php endif; ?>

最新评论代码

<?php
global $wpdb;
$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
SUBSTRING(comment_content,1,30) AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
$output .= "\n<ul>";
foreach ($comments as $comment) {
$output .= "\n<li>".strip_tags($comment->comment_author)
.":" . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\" title=\"on " .
$comment->post_title . "\">" . strip_tags($comment->com_excerpt)
."</a></li>";
}
$output .= "\n</ul>";
$output .= $post_HTML;
echo $output;?>

显示最多评论代码

<?php $result = $wpdb->get_results("SELECT comment_count,ID,post_title FROM $wpdb->posts 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 echo get_permalink($postid); ?>" title="<?php echo $title ?>"><?php echo $title ?></a></li>
<?php } } ?>

文章分类代码

<h2>Categories</h2>
<ul>
<?php wp_list_cats('sort_column=name'); ?>
</ul>

文章分类下拉菜单代码

<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>

文章存档代码

<h2>Archives</h2>
<ul>
<?php wp_get_archives('type=monthly'); ?>
</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>

在侧边栏显示页面

<h2>Pages</h2>
<ul>
<?php wp_list_pages('title_li='); ?>
</ul>

显示 Gravatar 图像 (要求WordPress 2.5)

<?php if(function_exists(’get_avatar’)){ echo get_avatar($comment, ‘50?);} ?>

链接代码

<ul>
<?php get_links_list(); ?>
</ul>

显示管理登陆代码

<ul>
<?php wp_register(); ?>
<li><?php wp_loginout(); ?></li>
<li><a href="http://www.wordpress.org/">WordPress</a></li>
<?php wp_meta(); ?>
<li><a href="http://validator.w3.org/check?uri=referer">XHTML</a></li>
</ul>

在侧边栏显示子页面

<?php$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');if ($children) { ?><ul> <?php echo $children; ?>
</ul>
<?php } ?>

显示文章标签

<?php the_tags(); ?>

标签云代码

<?php wp_tag_cloud('smallest=8&largest=36&'); ?>

如果你知道哪些比较有用的代码的话,希望你可以推荐,也希望这篇文章能在不断更新中得到完善!

Popularity: 2% [?]

Share/Bookmark this!

5 Comments

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by on December 27, 2008 at 8:15 am and filed under Wordpress 代码 category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

最新文章

最新评论

Tag Cloud

301 Redirect 301重定向 2010 about All in one seo pack AutosCraze blog CSS Darren feed FreshMag Global Translator Google GreenFun HTML Insider name.com Problogger robots.txt文件 RSS Feed seo Wordpress Wordpress 主题 Wordpress主题 代码 优惠码 关于 博客 博客建设 域名 工具 建设 技巧 提交 插件 文章 杂志型主题 标签 注册 绿色 网赚 评论 选择 链接 页面