《多站点随机调用》由会员分享,可在线阅读,更多相关《多站点随机调用(4页珍藏版)》请在金锄头文库上搜索。
1、/*多站点函数方法 *$showpost 每个子站点显示的最新文章数量 *使用方法 lei_multsite_recent_post(1) 每个子站点显示一篇最新文章 */ function lei_multsite_recent_post($showpost) global $wpdb, $post, $table_prefix;/首先将所有的子站点 ID 号显示出来$most_post = $wpdb-get_col(“SELECT blog_id FROM $wpdb-blogs WHERE public = 1AND archived = 0AND mature = 0AND spam
2、 = 0AND deleted = 0ORDER BY last_updated DESC“);foreach ($most_post as $key = $v)if($v !=1 ) /除主站点 ID$b_id$key = $v;$newid = array_flip($b_id); /php 数组函数对换键值$new_post_id = array_rand($newid,2);/php 数组函数随机显示 2 条,这里的数值不能大于你创建子站点的个数。foreach ( $new_post_id as $k = $vol )$blogPostsTable = $wpdb-base_pref
3、ix.$vol.“_posts“; $recent_post = $wpdb-get_results(“SELECT * from $blogPostsTable where post_status = publish and post_type = post ORDER BY post_date desc LIMIT 0,$showpost“); foreach ($recent_post as $ks = $vs) $thispermalink = get_blog_permalink($vol, $vs-ID); $count1 += 1; echo .$vs-post_title.;
4、/*参数说明$how_many: 要显示的多少篇最新文章$how_long: 显示时间区间 0 为禁止该功能$titleOnly:如果是 true(只显示文章标题)或 false(显示文章标题和站点名称)$begin_wrap: 自定义 HTML 标签,如:$end_wrap: 自定义 HTML 标签,如:使用方法: wpmu_recent_posts_mu(5, 30, true, , ); 在过去的 30 天显示最新的 5 篇文章, 并且只显示文章标题。 */ function wpmu_recent_posts_mu($how_many=10, $how_long=0, $titleOn
5、ly=true, $begin_wrap=“n“, $end_wrap=“) global $wpdb; global $table_prefix; $counter = 0;/首先通过判断是否显示时间区间来分别使用不同的 SQl 语句 if ($how_long 0) $blogs = $wpdb-get_col(“SELECT blog_id FROM $wpdb-blogs WHERE public = 1 AND archived = 0 AND mature = 0 AND spam = 0 AND deleted = 0 AND last_updated = DATE_SUB(CU
6、RRENT_DATE(), INTERVAL $how_long DAY) ORDER BY last_updated DESC“); else $blogs = $wpdb-get_col(“SELECT blog_id FROM $wpdb-blogs WHERE public = 1 AND archived = 0 AND mature = 0 AND spam = 0 AND deleted = 0 ORDER BY last_updated DESC“); /如果存在多站点 ID if ($blogs) foreach ($blogs as $blog)/ 下面是需要使用的数据表
7、$blogOptionsTable = $wpdb-base_prefix.$blog.“_options“; $blogPostsTable = $wpdb-base_prefix.$blog.“_posts“; $options = $wpdb-get_results(“SELECT option_value FROM blogOptionsTable WHERE option_name IN (siteurl,blogname) ORDER BY option_name DESC“);/ 为最新文章获取标题和 ID 号 if ($how_long 0) $thispost = $wpdb
8、-get_results(“SELECT ID, post_title FROM $blogPostsTable WHERE post_status = publish AND ID 1 AND post_type = post AND post_date = DATE_SUB(CURRENT_DATE(), INTERVAL $how_long DAY) ORDER BY id DESC LIMIT 0,1“); else $thispost = $wpdb-get_results(“SELECT ID, post_title FROM $blogPostsTable WHERE post_
9、status = publish AND ID 1 AND post_type = post ORDER BY id DESC LIMIT 0,1“); / 如果存在将输入内容 if($thispost) $thispermalink = get_blog_permalink($blog, $thispost0-ID); if ($titleOnly = false) echo $begin_wrap.$thispost0-post_title. by option_value.“ .$options1-option_value.$end_wrap; $counter+; else echo
10、$begin_wrap.$thispost0-post_title.$end_wrap; $counter+; / 对文章数量进行判断。 if($counter = $how_many) break; echo “; /根据时间显示最新的分类文章内容,每个站点显示一篇内容 /$blog_id 子站点 ID /$catid 分类 ID function get_cat_blogposts_wpmu($blog_id,$catid) wp_reset_query(); switch_to_blog($blog_id); global $post;? have_posts() : $my_query2-the_post(); ? “ rel=“bookmark“ ?php restore_current_blog(); 中文分类最好不用