Get category ID by using category name in wordpress


If you want to get category id by using category name then use the following code

function get_category_id($cat_name){
	$term = get_term_by('name', $cat_name, 'category');
	return $term->term_id;
}

then Just call the function with your category name as a parameter. for example

$category_ID = get_category_id('Books');

Now you can get the category Id in variable $category_ID.

Have any doubt, then comment here!

create an autocomplete textbox in wordpress


Just add a div under the input tag

HTML Code:

<input type="text" id="city" name="city" autocomplete="off"/> 
<div id="key"></div>

replace the div after the success on you ajax.

Ajax Code:

     var ajaxurl="<?php echo admin_url( 'admin-ajax.php' ); ?>"; 
     var data ={ action: "city_action",  city:cid    };
        $.post(ajaxurl, data, function (response){
                     $('#key').html(response);
           });

PHP Code:

       function city_action_callback() {     
       global $wpdb;
            $city=$_GET['city'];
            $result =   $mytables=$wpdb->get_results("select * from ".$wpdb->prefix . "mycity where city like '%".$city."'" );   
            $data = "";
            echo '<ul>'
            foreach($result as $dis)
            {
                         echo '<li>'.$dis->city.'</li>';
            }
            echo '</ul>'    
         die();
       }

Have any doubt, then comment here!

Get All Images From WordPress Media Gallery


Uploaded images are stored as posts with the type “attachment”. Use get_posts() and query for all attachments:

$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_mime_type' =>'image',
'post_status' => 'inherit',
'post_parent' => null, // any parent
);
$attachments = get_posts($args);
if ($attachments) {
foreach ($attachments as $post) {
setup_postdata($post);
the_title();
the_attachment_link($post->ID, false);
the_excerpt();
}
}

Now All images are displayed.

Have any doubt, then comment here!

Show All Categories as Links in WordPress


You can use the function get_the_category_list() for this process.

<?php
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
	foreach($categories as $category) {
		$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
	}
echo trim($output, $separator);
}
?>

For more : http://codex.wordpress.org/Template_Tags/get_the_category

How to sort an array of objects with javascript


Originally posted on marcalbert:

We  can sort an array using this method:

  1. This is the method to sort the array:
    var array = [(name, artist),(name, artist),(name, artist)];
    
    function SortByName(a, b){
      var aName = a.name.toLowerCase();
      var bName = b.name.toLowerCase(); 
      return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
    }
    
    
  2. How to sort the array with data? Just do:
    var arr = [{name:'bob', artist:'rudy'},{name:'johhny', artist:'drusko'},{name:'tiff', artist:'needell'},{name:'top', artist:'gear'}];
    array.sort(SortByName);

Reference link: http://stackoverflow.com/questions/5503900/how-to-sort-an-array-of-objects-with-jquery-or-javascript

View original

Get Inserted ID From Database


Originally posted on My Problem Today:

Hi, today i got another problem to get inserted record id.I remember last time i have done some modification in my code just to get inserted id using pure PHP. Today when using codeigniter, i just shot the following command to get inserted id

$id = $this->db->insert_id();

Only by using simple command, i got inserted id. This library is very helpful.

For the details, please go to the following link

https://ellislab.com/codeigniter/user-guide/database/helpers.html

Thank and happy coding

View original