Log in with username/password: lyceum/lyceum

Ticket #829 (new enhancement)

Opened 1 year ago

Codes to collect articles by categories system-widely and show them on portal page

Reported by: lyceum Assigned to:
Priority: normal Milestone: The Future
Keywords: category classify Cc: xfliu.math@gmail.com

Description

This code is built to collect articles to be shown on top pages. The code is independent one, so what you need to do is just to find a place in portal page and then insert it.

Let's assume you are the admin having a blog named "adminblog", which is needed in the following code.

Further assume that you have defined two sub-categories, "love story", "travel story" under "PUB" category. Yeah, it means you should create three categories "PUB", "love story" and "travel story" separately.

Then by the following code, all the people having articles in their own category PUB/"love story" or PUB/"travel story" will have such articles shown on top pages.

Of course, as you see, here we add limitation to the categories that can be shown on top pages.

<h2>Recommended Articles by Groups:</h2>

<?php

	$ref_blog_slug="adminblog"; //YOU NEED TO CHANGE IT!
	$ref_cat_name="PUB";

	//To determine the reference category id (REF_CAT_ID)
	$query="select cat_ID from $wpdb->categories 
	inner join $wpdb->blogs on $wpdb->categories.blog = $wpdb->blogs.id 
	where $wpdb->blogs.slug=\"$ref_blog_slug\" and $wpdb->categories.cat_name=\"$ref_cat_name\" ";

	$re=$wpdb->get_results($query);
	$ref_cat_id=$re[0]->cat_ID;


	//To get all subcategory names of given REF_CAT_ID under given REF_BLOG_SLUG.
	$query="select cat_name from $wpdb->categories 
	inner join $wpdb->blogs on $wpdb->categories.blog = $wpdb->blogs.id where $wpdb->blogs.slug=\"$ref_blog_slug\" and category_parent=$ref_cat_id ";
	$re=$wpdb->get_results($query);


	foreach ($re as $line){
		$cat_name=$line->cat_name;
		echo $cat_name,"<br>";
		$query="select cat_ID from $wpdb->categories where cat_name=\"$ref_cat_name\"";
		$re_cat_ids=$wpdb->get_results($query);
		$tmp_first_record=1; $id_list = "";
		foreach ($re_cat_ids as $key=>$val){
			if($tmp_first_record)	$id_list .= " ".$val->cat_ID;
			else	$id_list .= ", ".$val->cat_ID ;
			$tmp_first_record=0;
		}

		$my_q="SELECT `$wpdb->posts`.post_name, `$wpdb->posts`.post_title, `$wpdb->posts`.guid, 
$wpdb->blogs.slug, `$wpdb->posts`.post_date
from `$wpdb->posts`
inner join `$wpdb->post2cat` on `$wpdb->posts`.ID = `$wpdb->post2cat`.post_id
inner join `$wpdb->categories` on `$wpdb->post2cat`.category_id = `$wpdb->categories`.cat_ID
inner join `$wpdb->blogs` on `$wpdb->categories`.blog =`$wpdb->blogs`.id
where $wpdb->categories.category_parent in ($id_list) 
and $wpdb->categories.cat_name=\"$cat_name\" 
and `$wpdb->blogs`.status=\"active\" and `$wpdb->posts`.post_status=\"publish\" order by post_date desc limit 0,8";

		$i=0;
       $posts = $wpdb->get_results($my_q);
		echo "<ol>\n";
	       foreach ($posts as $p){
			$i++;
			$url=$p->guid;
			if($p->post_name){
				$url="./".$p->slug ."/". substr($p->post_date,0,4)."/".substr($p->post_date,5,2)."/".substr($p->post_date,8,2)."/".$p->post_name ."/" ;
			}
			echo "<li>", substr($p->post_date,5,5),"  <a href=\"".$url."\">";
			echo $p->post_title, "</a>(".$p->slug , ") " ;
			echo "</li>";
		}
		echo "</ol>";

		echo "<hr>";
	}
?>

Log in with username/password: lyceum/lyceum