<?php bloginfo('stylesheet_directory'); // Theme path ?>
Category Archives: Wordpress
Wordpress
Function that displays the categories – WordPress
<?php wp_list_categories(); // List all categories ?>
WordPress Query to fetch the Categories
global $wpdb; $categoriesData = $wpdb->get_results("SELECT c.*,ct.* FROM {$wpdb->prefix}terms as c JOIN {$wpdb->prefix}term_taxonomy as ct ON c.term_id=ct.term_id WHERE ct.taxonomy='category' "); foreach($categoriesData as $key=>$category) { echo $category->name; }
WordPress Query to fetch the subcategories
<?php global $wpdb; $cat="2"//Parent category id $categoriesData = $wpdb->get_results("SELECT c.*,ct.* FROM {$wpdb->prefix}terms as c JOIN {$wpdb->prefix}term_taxonomy as ct ON c.term_id=ct.term_id WHERE ct.taxonomy='category' AND ct.parent ='$cat' "); ?>
WordPress query to fetch the pages
<?php global $wpdb; $querystr = " SELECT distinct(post_title) ,ID,post_title FROM $wpdb->posts WHERE post_type='page' AND post_status='publish' "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
WordPress query to fetch the posts
<?php global $wpdb; $querystr = " SELECT distinct(post_title) ,ID,post_title FROM $wpdb->posts WHERE post_type='post' AND post_status='publish' "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
WordPresss query to fetch the posts under the given categories
<?php global $wpdb; $const="4,8,7"; //category Ids $querystr = "SELECT distinct(wposts.post_title),wposts.ID FROM $wpdb->posts wposts LEFT JOIN $wpdb->term_relationships ON (wposts.ID = $wpdb->term_relationships.object_id) LEFT JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) WHERE $wpdb->term_taxonomy.taxonomy = 'category' AND $wpdb->term_taxonomy.term_id IN($const) ORDER BY rand() "; $pageposts = $wpdb->get_results($querystr, OBJECT); ?>
How to filter the page title – WordPress
<?php add_filter( 'wp_title', 'filter_wp_title', 10, 2 ); function filter_wp_title( $title, $separator ){ $title ="New Title"; } ?>
Template Redirection – WordPress
add_action('template_redirect','theme_function'); function theme_function(){ if(isset($_GET['sports']) { include(TEMPLATEPATH."/sports.php"); exit(); }}
Category url from its ID – WordPress
<?php echo get_category_link(categoryID); //Category URL ?>