WordPress主题中常用的5个代码


WordPress 主题中常用代码总结:

1. 在WordPress主题中显示最热文章的PHP代码
<?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 $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> (<?php echo $commentcount ?>)</li>
<?php } } ?>
<div id="newpost">
<ul>
<?php

2. wordpress主题–相关文章代码
<?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 $post) {
setup_postdata($post);
$postid = $post->ID;
$title = $post->post_title;
$commentcount = $post->comment_count;
if ($commentcount != 0) { ?>
<li><a href="<?php echo get_permalink($postid); ?>" title="<?php echo $title ?>">
<?php echo $title ?></a> (<?php echo $commentcount ?>)</li>
<?php } } ?>
<div id="newpost">
<ul>
<?php
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$first_tag = $tags[0]->term_id;
$args=array(
'tag__in' => array($first_tag),
'post__not_in' => array($post->ID),
'showposts'=>10,
'caller_get_posts'=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"
title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?>
</a> </li>
<?php endwhile; } else {echo 'not realate post';} }else {echo 'not
realate post';} ?>

3. WordPress主题使用PHP代码输出最新文章
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit=7 . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>
<ul>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
</ul>
<?php endif; endwhile; ?>

4. 最新评论:
<?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) AScom_excerpt FROM $wpdb->comments LEFT OUTER
JOIN $wpdb->posts ON($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
WHEREcomment_approved = '1' AND comment_type = " AND post_password = " ORDERBY
comment_date_gmt DESC LIMIT 10";
$comments = $wpdb->get_results($sql);
$output = $pre_HTML;
foreach ($comments as $comment) {
$output.= "\n<li>". "<a href=\"" .get_permalink($comment->ID)."#comment-" .
$comment->comment_ID ."\" title=\"on
".$comment->post_title ."\">".strip_tags($comment->comment_author)."</a>" .
": ".strip_tags($comment->com_excerpt)."</li>";
}
$output .= $post_HTML;
echo $output;
?>
<?php
$limit = get_option('posts_per_page');
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts('showposts=' . $limit=7 . '&paged=' . $paged);
$wp_query->is_archive = true; $wp_query->is_home = false;
?>
<?php while(have_posts()) : the_post(); if(!($first_post == $post->ID)) : ?>
<ul>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to
<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
</ul>
<?php endif; endwhile; ?>

5. wordpress主题–相关文章代码
基本sql 是这样:$sql = “SELECT p.ID, p.post_title, p.post_date, p.comment_count,
count(t_r.object_id) as cnt FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships
t_r, $wpdb->posts p WHERE t_t.taxonomy =’post_tag’ AND t_t.term_taxonomy_id =
t_r.term_taxonomy_id AND t_r.object_id = p.ID AND (t_t.term_id IN ($taglist)) AND
p.ID != $post->ID AND p.post_status = ‘publish’ GROUP BY t_r.object_id ORDER BY cnt
DESC, p.post_date_gmt DESC LIMIT $limit;”;
一点一点的解释:term_taxonomy 、term_relationship、posts 这三张表存的什么我不多说,
网上一般都可以查到,维基百科貌似都有。把他们连起来,做个sql,注意 group by 以及
limit,这样就可以提取结果了$related_post = $wpdb->get_results($sql);
之前要把$taglist 做好,利用wp_get_post_tags($post->ID);可以将该篇文章的的tag 取到
一个数组中,然后再链接就可以了
最后怎么处理输出就看个人爱好了,这个功能我写的大概也就十几行,我比较喜欢简短的说,
呵呵。
function related_post($limit = 10) {
global $wpdb, $post;
$tags = wp_get_post_tags($post->ID);
$taglist = "'" . $tags[0]->term_id. “‘”;
$tagcount = count($tags);
if ($tagcount > 1) {
for ($i = 1; $i < $tagcount; $i++) {
$taglist .= “, ‘”.$tags[$i]->term_id.”‘”;
}
}
$sql = “SELECT p.ID, p.post_title, p.post_date, p.comment_count, count(t_r.object_id) as cnt
FROM $wpdb->term_taxonomy t_t, $wpdb->term_relationships t_r, $wpdb->posts p WHERE
t_t.taxonomy =’post_tag’ AND t_t.term_taxonomy_id = t_r.term_taxonomy_id AND t_r.object_id =
p.ID AND (t_t.term_id IN ($taglist)) AND p.ID != $post->ID AND p.post_status = ‘publish’ GROUP
BY t_r.object_id ORDER BY cnt DESC, p.post_date_gmt DESC LIMIT $limit;”;
$related_post = $wpdb->get_results($sql);
$output = “”;
foreach ($related_post as $tmp){
$output .= 这个就看个人爱好了
}
if($output == “”)$output = “No Related Post Yet”;
echo $output;
}

You can leave a response, or trackback from your own site.

51 Responses to “WordPress主题中常用的5个代码”

  1. Financial aid says:

    Hey this is a great post. I’m going to email this to my friends. I stumbled on this while surfing for some freebies, I’ll be sure to come back. thanks for sharing.

  2. burn belly fat says:

    Reading through your blog gives me a chance to recall why I like reading things with so much ideas. It is nice to know that there are still great writers out there that can put humor into knowledgable information. Thank you for your input and eagerness to share your thoughts with us.

  3. Trewwhegoro says:

    I like your site!

  4. long hard penis says:

    buddy and I. a lot of speak up here and let you know It is refreshing to find people who write like they know what they are talking about

  5. Best Registry Cleaner says:

    thanks for great informations It’s a wonderful

  6. free trial says:

    Thank you for Posting & I got to read nice information on your site.

  7. Hermes birkin bag says:

    very well information you write it very clean. I’m very lucky to get this info from you.

  8. mbt shoes says:

    very well information you write it very clean. I’m very lucky to get this info from you.

  9. 黑帽SEO says:

    Very good journey and experience!

  10. tuctmeamy says:

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers

  11. Christian Louboutin says:

    Good

  12. Rene Segundo says:

    Hello,just observed your Post when i google something and wonder what web hosting do you use for your blog,the speed is more faster than my blog, i really want to know it.will back to check it out,thanks!

  13. sunglass armani says:

    Thank you very much for the information great post, found it on Yahoo.

  14. sports jerseys says:

    great share, great article, very usefull for me…thank

    you

  15. tuctmeamy says:

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

  16. Cartier Sunglasses says:

    great experience, dude! thanks for this great

    Articles wow… it’s very wonderful report.

  17. Ed Hardy Sunglass says:

    Great journey and experience!

  18. roblox cheats says:

    You made good points made that we didn’t thought of

  19. Wholesale sunglasses says:

    Was an interesting article, thank you..

  20. tuctmeamy says:

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

    Thumbs up, and keep it going!

    Cheers
    Christian

  21. sunglasses shop says:

    Nice

  22. sunglasses shop says:

    thank! for this news it’s a good infomation !

  23. tuctmeamy says:

    Just want to say what a great blog you got here!
    I’ve been around for quite a lot of time, but finally decided to show my appreciation of your work!

  24. Christian Louboutin says:

    Enjoyed reading

    the report – most informative thanks

  25. Christian Louboutin says:

    Thanks for good information that comes out to

    read.

  26. best travel agency says:

    Hei, I like this blog, many good information, I want to subscribe it, can anyone help me?

  27. medication overweight says:

    Well, I just want to let you know I like your site.

  28. New smokeless cigarettes says:

    Admiring the time and work you set into your weblog and detailed data you deliver! I’ll bookmark your weblog and have my kids check out up the following often. Thumbs up!

  29. Tom And Jerry Games says:

    Wonderful insight:-)

  30. MBT Women Shoes says:

    wow.. i’m very

    enjoy reading your post. great.

  31. Brynn Romanchuk says:

    This is a amazing products you want to see.

  32. Ira Skaff says:

    I dont think you have to miss it.

  33. windows xp registry cleaner software says:

    Thanks for an idea, you sparked at thought from a concept I hadn’t considerd before . Now lets see if I can do something with it.

  34. [email protected] says:

    I dot it,you can try.

  35. junson chan official wow gold guide site says:

    This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

  36. conspiracy oil spill china dalian says:

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon.

  37. evony conquering a city says:

    Searching for this for some time now – i guess luck is more advanced than search engines :)

  38. insturctions to build a wind generator says:

    I can see that you are an expert at your field! I am launching a website soon, and your information will be very useful for me.. Thanks for all your help and wishing you all the success.

  39. registry cleaner review says:

    Awesome Post! It’s really nice to read this information from someone that actually knows what they are talking about.

  40. home loan says:

    Hello everyone thanks for

    good information.

  41. Chassidy Mccalpin says:

    Hi! I really liked reading your post and think you brought up many good points! I included your blog in my RSS feeds, hope you post soon! Thanks!

  42. Malik Ishmon says:

    What’s Up! Just had to leave a comment. I truly loved your opinion

  43. home loan says:

    i have enjoyed reading thank for sharing your story Greeting.

  44. Alcohol Detox says:

    Enjoyed reading this post, thanks for discussing.

  45. Tomeka Ereaux says:

    Hi! Just wanted to chime in. I honestly loved your opinion

  46. Gidget Barr says:

    What’s Up! Just had to leave a comment. I truly liked your opinion. Keep up the awesome work.

  47. wholesale sunglasses says:

    great information you write it very clean. I am very lucky to get this tips from you.

  48. wholesale shoes says:

    Sorry, but you don’t give yourself enough credit.

  49. Wholesale shoes says:

    Hey I think you have a great blog going here, I found it on Bing and plan on returning regularly for the information that you all are providing.

  50. Team Roster says:

    Best you could edit the blog subject WordPressä something more specific for your content you write. I enjoyed the blog post nevertheless.

  51. 旅心 says:

    非常感谢曹鹏老师的讲解。www.tuidaren.com

Leave a Reply

CAO PENG DOT COM | 看邦主文摘来CAOPENG.NET | 学编程之邦去CAOPENG.ORG | 搜索引擎XML地图1 2 | WMV to DVD | remove drm from wmv | DVD Creator | 虚拟现实