Monday, June 27, 2011

get user city location

Create function in your controller file..

<?php
function _get_location()
    {
        $country=file_get_contents('http://api.hostip.info/get_html.php?ip=');
        $_country = explode ("\n", $country);
        $_country = str_replace("City: ", "", $_country);
        return $_country[1];
    }
?>


and call this function like this in your controller.

<?php echo $this->_get_location(); ?>

This will give user's location based on its IP address.

Sunday, June 19, 2011

post without image

place this code in functions.php in your theme folder.
<?php
function post_without_image($content) {
    ob_start();
    echo $content;
    $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
    ob_end_clean();
    return $postOutput;
}
?>


use post_without_image() function with 1 parameter with your content anywhere you want

<?php echo post_without_image($content); ?>

Instead of $content we use content variable where we store our content section. That's done.

Get post by category

Sometime we need get post by category in wordpress. Then just create a functions in functions.php in your theme folder with this line..

<?php
function get_post_by_category($category_id) {
    global $post;
    $args = array( 'numberposts' => 1, 'category' => $category_id );
    $myposts = get_posts( $args );
    return $myposts;
}
?>


And use this code where you want to show your post with categories id.

<?php echo get_post_by_category($id); ?>

Instead of $id we use category id. That's all

Thursday, June 16, 2011

Display all images in your site

Copy this code to functions.php

<?php
function latest_images() {   
    $images = get_children( 'post_type=attachment&post_mime_type=image&numberposts=3&order=DESC&post_parent=' );
    echo "<ul>";
    foreach($images as $image) {
        echo "<li><a href='" . get_permalink($image->ID) . "'><img src='" . $image->guid . "' width='252' height='135' /></a></li>";   
    }
    echo "</ul>";
}

?>
 
and place this to your index.php

<?php latest_images(); ?>