Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, August 1, 2012

Show Custom field in WordPress Post, Custom Field in WordPress

Custom fields are very useful in WordPress and we can attach lot of information as metadata in our Post. Now we may have requirement in which we want to display these Meta data or Custom field in our Post. If you want to display the custom field data in your post or anywhere just use the following code snippet.

      <?php if(get_post_meta($post->ID, 'custom_field_name', true)!=null)
        {
             echo get_post_meta($post->ID, ' custom_field_name ', true);
              }
?>
If you feel any problem in this post just mention it in comment i will try to resolve it.

Limit the Post Title in WordPress


Sometime we have very long post title and we want that only some part of title should display on main page and whole title should only display on single post page. Following code you can use to perform this task.
<?php
            foreach( $myposts as $post ):setup_postdata($post)

                          $post_title=get_the_title();
  $title_length=strlen($post_title);

  $title_sub=substr($post_title,0,29);
if($title_length<30)
the_title();
else
 echo $title_sub."...";
?>
If you will find any problem just mention it in comments, I will try to resolve it.

Use jQuery or JavaScript in PHP, Hide or Show a DIV


How to use jQuery or JavaScript in PHP
Some time we have a such type of logic in which we want to use jQuery within PHP code. It is very useful and sometime you can perform
a very difficult task by using jQuery and PHP in no time. Here i am using jQuery/JavaScript code for showing and hiding of any div.
Basically here i am trying to show errors after validating a form on server side.

 if($first_name=="" || $first_name==null)
       {
     ?>
<script>
$(document).ready(function(){

        $(".contact2").hide();
$(".names2").show();
});
    </script>

<?php
  }
 elseif($last_name=="" || $last_name==null)
       {
    ?>
<script>
$(document).ready(function(){

        $(".contact2").hide();
$(".name2").show();
});
    </script>

<?php
       }


     elseif(!preg_match($email_exp,$enq_email))
{
         ?>
<script>
$(document).ready(function(){

        $(".contact2").hide();
$(".emails2").show();
});
    </script>
If you still find any problem just mention it in comment i will try to resolve it.