<?php query_posts(array(‘category__and’ => array(22,27))); ?>
Category Archives: Wordpress
WordPress plugin with Ajax pagination
Create folder in wp-content/plugins/ and give a relevant name . let it be “wpcRecentPosts”.
Inside that folder create a file “wpcRecentPosts.php “ and paste the below given code in that file .
<?php
/**
* Plugin Name: WPC Recent Posts With Pagination
* Plugin URI: http://phpcodez.com/
* Description: A widget that displays Recent Posts With Pagination
* Version: 0.1
* Author: Pramod T P
* Author URI: http://phpcodez.com/
*/
add_action( ‘widgets_init’, ‘wpc_recentposts_widgets’ );
function wpc_recentposts_widgets() {
register_widget( ‘wpcrecentpostsWidget’ );
}
class wpcrecentpostsWidget extends WP_Widget {
function wpcrecentpostsWidget() {
$widget_ops = array( ‘classname’ => ‘wpcClass’, ‘description’ => __(‘A widget that displays Recent Posts With Pagination.’, ‘wpcClass’) );
$control_ops = array( ‘width’ => 300, ‘height’ => 350, ‘id_base’ => ‘wpc-recentposts’ );
$this->WP_Widget( ‘wpc-recentposts’, __(‘Recent Posts With Pagination’, ”), $widget_ops, $control_ops );
}
function widget( $args, $instance ) { ?>
function getNextPosts(pageno){
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert (“Your browser does not support AJAX!”);
return;
}
var url=”/wpcRecentPosts/”+”recent-posts.php?”;
url=url+”&page_no=”+pageno;
url=url+”&sid=”+Math.random();
//alert(url);
xmlHttp.onreadystatechange=stateChangedFeaturedMovies;
xmlHttp.open(“GET”,url,true);
xmlHttp.send(null);
}
function stateChangedFeaturedMovies(){
if (xmlHttp.readyState==4)
document.getElementById(“recentPost”).innerHTML=xmlHttp.responseText;
}
if(!window.GetXmlHttpObject) {
function GetXmlHttpObject(){
var xmlHttp=null;
try {// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e) {// Internet Explorer
try {
xmlHttp=new ActiveXObject(“Msxml2.XMLHTTP”);
}
catch (e){
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
}
return xmlHttp;
}
}
$recentPostCount = $wpdb->get_results($recentPostQuery, OBJECT);
$totalrecentPost = sizeof($recentPostCount);
$lastPage=ceil($totalrecentPost/$itemPerPage);
?>
| Next | );”>Previous |
| ID) ?>”> post_title; ?> | |
<?php
}
function update( $new_instance, $old_instance ) {}
function form( $instance ) {echo “No Backend Options Available”;}
}
?>
After that create a file that run behind the scene when we call a javascript function and here we use “recent-posts.php”. Paste the below given code in that file
<?php
require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.’..’.DIRECTORY_SEPARATOR.’..’.DIRECTORY_SEPARATOR.’..’.DIRECTORY_SEPARATOR.’wp-load.php’);
$pageNo=$_REQUEST[‘page_no’];
global $wpdb;
$itemPerPage=3;
$recentPostQuery = “SELECT distinct(post_title) ,ID,post_title FROM $wpdb->posts WHERE post_type=’post’ AND post_status=’publish’ “;
$recentPostCount = $wpdb->get_results($recentPostQuery, OBJECT);
$totalrecentPost = sizeof($recentPostCount);
$lastPage = ceil($totalrecentPost/$itemPerPage);
$start = ($pageNo-1)*$itemPerPage;
if($lastPage<$pageNo){ $start=0;$pageNo=1;}
if($totalrecentPost<$start){ $start=0;$pageNo=1; }
if($start<0){$pageNo=$lastPage;$start= ($lastPage-1)*$itemPerPage;}
?>
<table>
<tr>
<td width=”50%”><a href=”javascript:getNextPosts(<?php echo $pageNo+1 ?>);”>Next</a></td>
<td><a href=”javascript:getNextPosts(<?php echo $pageNo-1 ?>);”>Previous</a></td>
</tr>
<?php
$recentPostQuery=$recentPostQuery . ” ORDER BY post_date DESC LIMIT $start,$itemPerPage” ;
$recentPostResults = $wpdb->get_results($recentPostQuery, OBJECT);
foreach( $recentPostResults as $recentPost ) {
?>
<tr><td colspan=”2″><a href=”<?php echo get_permalink($recentPost->ID) ?>”> <?php echo $recentPost->post_title; ?></a></td></tr>
<?php } ?>
</table>
Category name from category id – WordPress
<?php
echo get_cat_name( $catID )
?>
Get current category id – WordPress
<?php
function getCurrentCatID(){
global $wp_query;
if(is_category() || is_single()){
$cat_ID = get_query_var(‘cat’);
}
return $cat_ID;
}
echo $getCurrentCatID();
?>
Print the substring of the post content – WordPress
<?php
$content = get_the_content();
echo substr(strip_tags($content), 0, 100);
?>
Categories of a post – WordPress
<?php
foreach((get_the_category(POST-ID)) as $category) {
echo $category->cat_name;
}
?>
Read custom field values of a post – WordPress
The below given code will list all the values added to the given fields
<?php
$custom_fields = get_post_custom(POST_ID);
$fieldsValue = $custom_fields[‘FIELD’];
foreach ( $fieldsValue as $key => $value )
echo $value . “<br />”;
?>
Add a new widget area – wordpress
if (function_exists(‘register_sidebar’)) {
register_sidebar(array(
‘name’=> ‘Header Area’,
‘id’ => ‘top_tabs’,
‘before_widget’ => ‘<li id=”%1$s”>’,
‘after_widget’ => ‘</li>’,
‘before_title’ => ‘<h2>’,
‘after_title’ => ‘</h2>’,
));
}
WordPress query to fetch links under a given category
<?php
// ‘links’ is the slug name of the link category link
$linkeQry=”SELECT * FROM wp_links as link INNER JOIN wp_term_relationships ON (link.link_id = wp_term_relationships.object_id)
INNER JOIN wp_term_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id) AND wp_term_taxonomy.taxonomy = ‘link_category’
INNER JOIN {$wpdb->prefix}terms as c ON c.term_id=wp_term_taxonomy.term_id
WHERE c.slug=’links'”;
$linksData = $wpdb->get_results($linkeQry);
?>
<ul>
<?php foreach($linksData as $key=>$link) { ?>
<li><a href=”<?php echo $link->link_url; ?>” target=”_blank”><?php echo $link->link_name; ?></a></li>
<?php } ?>
</ul>
WordPress query to fetch links
<?php
$linkeQry=”SELECT * FROM $wpdb->links”;
$linksData = $wpdb->get_results($linkeQry);
?>
<ul>
<?php foreach($linksData as $key=>$link) { ?>
<li><a href=”<?php echo $link->link_url; ?>” target=”_blank”><?php echo $link->link_name; ?></a></li>
<?php } ?>
</ul>