If u are reached here using search engine then I am sure that you are just fucked up đ .. is_home(), is_single(), is_category() are not workingâĻspecially in footer and other places. I faced the same problem. While working with a wordpress theme, this functions were working fine in footer but once I noticed that they are not working as they should be !âĻ.
Let me explain why and how this problem occurs and whatâs the solution :
I think if u are familiar with the famus LOOP in wordpress. The following is_home(), is_single(), is_category() functions depends on the loop. ACtually when u visit the home page then header.php, index.php, sidebar.php and footer.php files are executed and in the same way when u visit a single post then normally header.php, single.php, sidebar.php and footer.php files are executed. Actually some global page specific query is done for single.php, index.php etc. is_home(), is_single(), is_category() they will work fine if u use the default theme and donât add any custom query !
Letâs check the code for is_home() function âĻ
[code=’php’]/**
* Whether current page view is the blog homepage.
*
* @since 1.5.0
* @uses $wp_query
*
* @return bool True if blog view homepage.
*/
function is_home () {
global $wp_query;
return $wp_query->is_home;
}
[/code]
Ok, for custom query maximum time I use this function get_posts. But to get more control I used query_posts. They are same and share some common arguments but query_posts gives more controls and more arguments to pass to get the exact query. Now Letâs check the code for query_posts() function
[code=’php’]/**
* Setup the Loop based on the query variables.
*
* @uses WP::$query_vars
* @since 2.0.0
*/
function query_posts() {
global $wp_the_query;
$this->build_query_string();
$wp_the_query->query($this->query_vars);
}
[/code]
and code for get_posts
[code=’php’]/**
* Retrieve list of latest posts or posts matching criteria.
*
* The defaults are as follows:
* ‘numberposts’ – Default is 5. Total number of posts to retrieve.
* ‘offset’ – Default is 0. See {@link WP_Query::query()} for more.
* ‘category’ – What category to pull the posts from.
* ‘orderby’ – Default is ‘post_date’. How to order the posts.
* ‘order’ – Default is ‘DESC’. The order to retrieve the posts.
* ‘include’ – See {@link WP_Query::query()} for more.
* ‘exclude’ – See {@link WP_Query::query()} for more.
* ‘meta_key’ – See {@link WP_Query::query()} for more.
* ‘meta_value’ – See {@link WP_Query::query()} for more.
* ‘post_type’ – Default is ‘post’. Can be ‘page’, or ‘attachment’ to name a few.
* ‘post_parent’ – The parent of the post or post type.
* ‘post_status’ – Default is ‘published’. Post status to retrieve.
*
* @since 1.2.0
* @uses $wpdb
* @uses WP_Query::query() See for more default arguments and information.
* @link http://codex.wordpress.org/Template_Tags/get_posts
*
* @param array $args Optional. Override defaults.
* @return array List of posts.
*/
function get_posts($args = null) {
$defaults = array(
‘numberposts’ => 5, ‘offset’ => 0,
‘category’ => 0, ‘orderby’ => ‘post_date’,
‘order’ => ‘DESC’, ‘include’ => ”,
‘exclude’ => ”, ‘meta_key’ => ”,
‘meta_value’ =>”, ‘post_type’ => ‘post’,
‘suppress_filters’ => true
);
$r = wp_parse_args( $args, $defaults );
if ( empty( $r[‘post_status’] ) )
$r[‘post_status’] = ( ‘attachment’ == $r[‘post_type’] ) ? ‘inherit’ : ‘publish’;
if ( ! empty($r[‘numberposts’]) )
$r[‘posts_per_page’] = $r[‘numberposts’];
if ( ! empty($r[‘category’]) )
$r[‘cat’] = $r[‘category’];
if ( ! empty($r[‘include’]) ) {
$incposts = preg_split(‘/[\s,]+/’,$r[‘include’]);
$r[‘posts_per_page’] = count($incposts); // only the number of posts included
$r[‘post__in’] = $incposts;
} elseif ( ! empty($r[‘exclude’]) )
$r[‘post__not_in’] = preg_split(‘/[\s,]+/’,$r[‘exclude’]);
$r[‘caller_get_posts’] = true;
$get_posts = new WP_Query;
return $get_posts->query($r);
}
[/code]
get_posts will not make any problem for is_single(), is_home(), is_category() etc functions.
Solution: To avoid this problem you can use this trick:
[code=’php’]$query_backup = clone($GLOBALS[‘wp_query’]); //keep backup
……
query_posts(…..)
…
$GLOBALS[‘wp_query’] = $query_backup; //restore from backup
[/code]
Further info: Please keep in mind that the php function clone() will not work in php4, itâs just for php5 becuase Php5 handles objects in different way. Like in php5 you can copy a object in this way
$new_object = clone ($old_object);
here $new_object will not copy the object actually though it will just point to $old_object but in php4 the thing is just copy like bellow.
$new_object = $old_object;
You can check this link for better explanation.
Sometimes pain gives us a way to think đ
thanks