Change user avatar throughout the site

Comments Off

<?php

add_filter(‘get_avatar’, ‘site_get_avatar’, 10, 5);

function site_get_avatar($avatar, $id_or_email, $size, $default, $alt){

$imgpath = get_the_author_meta( ‘pic’, $id_or_email ) ;

$my_avatar = “<img src=’”.$imgpath.”‘ alt=’”.$alt.”‘ height=’”.$size.”‘ width=’”.$size.”‘ />”;

return $my_avatar;
}

 

?>




Tags page showing no post for custom posts type

Comments Off

just put this code in functions.php and you are good to go ;)

 

function myTagFilter($query) {
    $post_type = $_GET['type'];
    if (is_tag()){
        if (!$post_type) {
            $post_type = 'any';
        }
        $query->set('post_type', $post_type);
    }
    return $query;
};
add_filter('pre_get_posts','myTagFilter');



Show child pages of current parent page in sidebar

Comments Off

Many a time u get situation where you want to show child pages of current parent page in the sidebar… here you go give it a try ;)

<?php if ( is_page() ) { ?>

<?php
if($post->post_parent)
$children = wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0'); else
$children = wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if ($children) { ?>

<li>

<ul>
<?php echo $children; ?>
</ul>
</li>

<?php } } ?>



Delete Default Pages on Theme Activation

Comments Off
$post = get_page_by_path('page-slug',OBJECT,'page');
if ($post)
  wp_delete_post($post->ID,true);



Delete Hello World Post

Comments Off
$post = get_page_by_path('hello-world',OBJECT,'post');
if ($post)
  wp_delete_post($post->ID,true);



Page Creation on theme activation

Comments Off
if (isset($_GET['activated']) && is_admin()){
	$new_page_title = 'Page Title';
	$new_page_content = 'Page Content';
	$new_page_template = ''; // template name

	$page_check = get_page_by_title($new_page_title);
	$new_page = array(
		'post_type' => 'page',
		'post_title' => $new_page_title,
		'post_content' => $new_page_content,
		'post_status' => 'publish',
		'post_author' => 1,
	);
	if(!isset($page_check->ID)){
		$new_page_id = wp_insert_post($new_page);
		if(!empty($new_page_template)){
			update_post_meta($new_page_id, '_wp_page_template', $new_page_template);
		}
	}
}



Get all the categories ids

0

<?php
$category_ids = get_all_category_ids();
foreach($category_ids as $cat_id) {
$cat_name = get_cat_name($cat_id);
echo $cat_id . ': ' . $cat_name;
}
?>




Get Child Subcategories from parent Category

0

<?php
$childcategories = get_categories(‘child_of=’ . ’6′ . ‘&hide_empty=1′);

foreach ($childcategories as $childcategory) {
// if (in_category($childcategory)) {
echo ‘<li><h2><a href=”‘;
bloginfo(‘url’);
echo ‘/category/’.$cat_id->category_nicename.”.$childcategory->category_nicename.’”>’;
echo $childcategory->cat_name . ‘</a></h2>’;
echo ‘<p>’.$childcategory->category_description.’</p>’;
echo ‘<p>’.$childcategory->cat_ID.’</p>’;
echo ‘</li>’;
//}
}
?>




Browser conditional CSS hack

0

My Handy Browser detection code in PHP…………….

<? $msie = strpos($_SERVER["HTTP_USER_AGENT"], ‘MSIE’) ? true : false; $firefox = strpos($_SERVER["HTTP_USER_AGENT"], ‘Firefox’) ? true : false; $safari = strpos($_SERVER["HTTP_USER_AGENT"], ‘Safari’) ? true : false; $chrome = strpos($_SERVER["HTTP_USER_AGENT"], ‘Chrome’) ? true : false;

if ($msie) { echo ‘ ‘; } if ($safari) { echo ”; }

?>

<br>
<?php
if ($firefox) { //Firefox?
echo 'you are using Firefox!';
}

if ($safari || $chrome) { // Safari?
echo 'you are using a webkit powered browser';
}

if (!$msie) { // Not IE?
echo '<br>you are not using Internet Explorer<br>';
}
if ($msie) { // IE?
echo '<br>you are using Internet Explorer<br>';
}
?>



How to generate dynamic id in wordpress like dynamic class

0

WordPress has got a function called <?php body_class(); ?>to generate dynamic class based on the page you are in….Everything is good abt this class and am not against :) but what if u want to show dynamic id based on the page…..

 

here you go just paste this under body tag

 

 

$post_obj = $wp_query->get_queried_object(); echo $post_obj->post_name

 

 

Hope this is helpful for all