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

2. wordpress主题–相关文章代码

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

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

  

Wordpress主题中常用代码总结的更多相关文章

  1. wordpress主题制作常用基本的模板及说明

    style.css : CSS(样式表)文件,一般包括主题声明和通用css样式代码 index.php : 主页模板,一般用来做网站的首页 header.php : Header模板,一般是所有页面的 ...

  2. [工作总结]jQuery在工作开发中常用代码片段集锦(1-10)

    1.jQuery,JS实现tab切换 原生JS实现 HTML代码如下: <div class="wrap"> <ul id="tag"> ...

  3. phpcms中常用代码总结

    1.调用数据库模型 $this->db = pc_base::load_model('test_model');//从"phpcms/model/"目录下加载模型类文件 其中 ...

  4. Android 中常用代码片段

    一:AsyncTask 的使用 (1)activity_main.xml <TextView android:id="@+id/tvInfo" android:layout_ ...

  5. LaTeX中常用代码段snippets(持续更新)

    1.displaymath 单行数学环境,不带编号. \begin{displaymath} This\ is\ displaymath\ envirment.\ I\ don 't\ have\ a ...

  6. (转)WordPress常用模板函数 修改或自制WordPress主题必备

    对于很多WordPress新手来说,不懂任何代码的情况下去瞎改WordPress主题,得出的效果往往会出现语法错误之类的东西或效果不尽人意.想要修改 WordPress主题模板文件最基本的当然要懂得H ...

  7. WordPress主题模板层次和常用模板函数

    首页: home.php index.php 文章页: single-{post_type}.php – 如果文章类型是videos(即视频),WordPress就会去查找single-videos. ...

  8. Wordpress 常用代码解释

    1. 最新文章 Wordpress最新文章的调用可以使用一行很简单的模板标签 wp_get_archvies来 实现. 代码如下: <?php get_archives('postbypost' ...

  9. WordPress主题制作导航的N种方法

    在WordPress主 题制作中,导航菜单的制作算是一个重点,已经写好导航菜单的HTML代码,放在WordPress主题中如何动态调用呢?本文将给你介绍几种编写PHP代 码动态实现导航的方法,本文也将 ...

随机推荐

  1. PXE简要配置过程

    目录 1.所需服务 2.简要配置过程     1.dhcp服务     2.tftp服务     3.提供pxelinux.0配置文件     4.提供系统所需文件 1.所需服务:     dhcp服 ...

  2. 问题汇总-20130927-关于rc.local命令无法执行

    场景:/etc/rc.local有语句 /usr/local/apache/bin/apachectl start mysql cactidb -u root -p123456 -e ' set gl ...

  3. MVC5学习相关资源整理

    1  官方 Getting Started http://www.asp.net/mvc/tutorials/mvc-5/introduction/getting-started 英文不好,英文好的同 ...

  4. Win7网络检测 WindowsAPICodePack

    原文:http://www.cnblogs.com/yincheng01/archive/2010/05/30/2213234.html 在Windows7操作系统下,支持的网络类型越来越复杂,微软提 ...

  5. NASM mode for Emacs

    NASM mode for Emacs   Quick post for those Emacs users out there.   The common assembler used on GNU ...

  6. Effective Java2读书笔记-创建和销毁对象(三)

    第5条:避免创建不必要的对象 本条主要讲的是一些反面教材,希望大家引以为鉴. ①无意中使用自动装箱导致多创建对象. public class Sum { public static void main ...

  7. Best Time to Buy and Sell Stock I II III

    Best Time to Buy and Sell Stock Say you have an array for which the ith element is the price of a gi ...

  8. OpenCV视屏跟踪

    #include <stdio.h> #include <iostream> #include "opencv2/imgproc/imgproc.hpp" ...

  9. UVA10487(二分)

    Given is a set of integers and then a sequence of queries. A query gives you a number and asks to fin ...

  10. mongoDB global,startUplog

    1,show logs (Ruiy看到一些人干些事就跟拉shit一样(磨叽),就来气,娃儿的,还不知所谓,抱怨,叫器一大堆!!!,喝喝,就这它还三心二意学一个故事中的主人公小小明上课偷打`皮卡`玩,三 ...