好文档 - 专业文书写作范文服务资料分享网站

wordpress代码调用大全更新到 - 3.0版

天下 分享 时间: 加入收藏 我要投稿 点赞

All Rights Reserved

----------------------------------------------------------------------------->

WordPress获取当前分类的根分类id函数Wordpress 2010-08-26 15:52:05 阅读77 评论0 字号:大中小 订阅

之前在做这个WordPress的Themes时,需要实现在文章内容页面(single.php)的循环体外部输出该篇文章所属分类的所有同一级分类,也就是该篇文章所属分类的上一级分类下的子分类。当然,这样只适用于只有二级分类目录的情况!虽然貌似WordPress有内置这样的函数,但是都没达到我想要的效果,因为有些WordPress的内置函数是要使用在The Loop的循环体内的,所以用WordPress内置函数无法实现我需要的功能,只有自己动手了,后来使用了以下两个方法解决了问题!

function get_category_root_id($cat) {

$this_category = get_category($cat); // 取得当前分类

while($this_category->category_parent) // 若当前分类有上级分类时,循环 {

$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬) }

return $this_category->term_id; // 返回根分类的id号 }

function fengzx_get_category_ID() { $category = get_the_category(); return $category[0]->cat_ID; }

使用的时候就这样:

-------------------------------------------------------------------->

wordpress单个文章页面的侧边栏如果还是显示和首页,分类页面一样的文章就没什么意思了。下面的代码是显示文章所属分类的文章列表,这样可以让访问者可以方便查看相关文章。回头再改改,看看能不能在分类页面上显示最受欢迎的文章。 添加下列代码在合时的位置即可

All Rights Reserved

single page?show current category articles */ ?>

if ( is_single() ) : global $post;

$categories = get_the_category(); foreach ($categories as $category) : ?>

  • term_id;?>-posts\

    name; ?>

  • endforeach; endif ; ?>

    end show current category articles */ ?>

    ------------------------------------------------------------------------>

    1. wordpress调用最新文章

    Wordpress最新文章的调用可以使用一行很简单的模板标签wp_get_archvies来实现. 代码如下:

    (显示10篇最新更新文章) 或者

    All Rights Reserved

    后面这个代码显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可以参考官方的使用说明- wp_get_archvies。(fromat=custom也可以不要,默认以UL列表显示文章标题。)

    补充: 通过WP的query_posts()函数也能调用最新文章列表,虽然代码会比较多一点,但可以更好的控制Loop的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。

    2. wordpress调用随机文章

  • 3. wordpress调用最新留言

    下面是我之前在一个Wordpress主题中代到的最新留言代码,具体也记不得是哪个主题了。该代码直接调用数据库显示一份最新留言。其中 LIMIT 10限制留言显示数量。绿色部份则是每条留言的输出样式。

    comment_ID,comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_type,comment_author_url,SUBSTRING(comment_content,1,30)

    AS com_excerptFROM $wpdb->commentsLEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =$wpdb->posts.ID)WHERE comment_approved = '1' AND comment_type = '' ANDpost_password = ''ORDER BY comment_date_gmt DESCLIMIT 10\$comment) {$output .= \get_permalink($comment->ID) .\\ $output .= $post_HTML;echo $output;?> 4.wordpress调用相关文章

    在文章页显示相关文章

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

  • All Rights Reserved

    the_permalink() ?>\

  • 5.wordpress调用指定分类的文章

  • 6.wordpress去评论者链接的评论输出

    comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type,comment_author_url, SUBSTRING(comment_content,1,14) 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\$comment) { $output .= \get_permalink($comment->ID) . \$comment->post_title . \$post_HTML; echo $output;?>

    7.wordpress调用含gravatar头像的评论输出

    comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,10) 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 comment_author != '郑 永' AND post_password = '' ORDER BY comment_date_gmt DESC LIMIT 10\$wpdb->get_results($sql); $output = $pre_HTML; foreach ($comments as $comment) { $output .= \href=\\\$comment->post_title . \上的评论\\\: \

    All Rights Reserved

    strip_tags($comment->com_excerpt) .\convert_smilies($output); echo $output; ?>

    上面代码把comment_author的值改成你的ID,18是头像大小,10是评论数量。

    8.wordpress调用网站统计大全

    1、日志总数: publish;?> 2、草稿数目: draft; ?> 3、评论总数: get_var(\COUNT(*) FROM $wpdb->comments\、成立时间: 6、页面总数: publish; ?> 7、分类总数: 8、链接总数: get_var(\COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'\、用户总数: get_var(\10、最后更新: get_results(\FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')\$last; ?> 9.wordpress判断语句 is_single()

    判断是否是具体文章的页面

    is_single(’2′)

    判断是否是具体文章(id=2)的页面

    is_single(’Beef Stew’)

    判断是否是具体文章(标题判断)的页面

    is_single(’beef-stew’)

    判断是否是具体文章(slug判断)的页面

    comments_open()

    是否留言开启

    pings_open()

  • 34nfv559oa3fmdy9vdbr
    领取福利

    微信扫码领取福利

    微信扫码分享