Mexzhouse Design Pte Ltd Singapore 10 Anson road, International plaza #29-18 Singapore 079903

Contact us for web design and development quotations

  Free Online Quotation

WordPress Hacks and tricks

wordpress are really useful things. here are some hacks that you can use for your wodpress cms development.

Get the fiirst image from the post and display it :
add these codes to your function.php, if you do not have this page, create 1.

function catch_that_image() {
  global $post, $posts;
  $first_img = '';
  ob_start();
  ob_end_clean();
  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i',
$post->post_content, $matches);
  $first_img = $matches [1] [0];

  if(empty($first_img)){ //Defines a default image
    $first_img = "/images/default.jpg";
  }
  return $first_img;
}

then you call the function inside the loop:

<?php echo catch_that_image() ?>


however if you want to exclude the image from post here is the code :

<?php
   ob_start();
   the_content('Read the full post',true);
   $postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
   ob_end_clean();
   echo $postOutput;
?>

If you want to use customer field for your posts is pretty easy too :
add these code to your function:

function get_custom_field_value($szKey, $bPrint = false) {
  global $post;
  $szValue = get_post_meta($post->ID, $szKey, true);
  if ( $bPrint == false ) return $szValue; else echo $szValue;
}

then you call your customer field:

<?php if ( function_exists('get_custom_field_value') ){
        get_custom_field_value('featured_image', true);
} ?>

This entry was posted in Blog and tagged cms, cms design, Wordpress, wordpress tricks and hacks, wordpress web design, wordpress web development. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>