Wordpress Archives | Page 2 of 3 | Codeexecute

WordPress Content Functions

These are the pre-define function defined in wordpress we just need to use 
it directly where we wanted.
the most common functions we normally use is get title , get site url etc.
So basically  these are for beginners  and you become expert while regular using this.
there are comment with every function to explain its functionality. 


// title of the post


<?php the_title(); ?>


// web site url 


<?php echo site_url(); ?>


// web site name 


<?php echo get_bloginfo(); ?>

// post body 

<?php 
if ( have_posts() ) : the_post();
the_content();
endif;
?> 


// get custom value of the post


<?php 
$cusomVal = get_post_custom_values('name_of_custom_value', $post->ID);
echo $cusomVal[0] ;
?>


// get post thumbnail 

<?php the_post_thumbnail(); ?>


// get thumbnail url

<?php  
$post_thumbnail_id = get_post_thumbnail_id($post->ID);
$post_thumbnail_url = wp_get_attachment_url( $post_thumbnail_id );
?> 


// post per page attributes (give the required number of post on the page)
<?php
  $args = array( 'posts_per_page' => 5, 'offset'=> 0 );
  $myposts = get_posts( $args );
?>

// limited post text on the page (put this on the place the_content())and add ellipse in the end


<?php the_excerpt(); ?>


// get published date 
<?php echo get_the_date(); ?>

</pre>
<pre>
 
 
 
 

Custom file Upload in (spicifiec directory) WordPress

<?php
$dir = wp_upload_dir();

$target_dir = $dir['basedir'] .'/';

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file))
{
    $url = $dir['baseurl'].basename($_FILES["fileToUpload"]["name"]);

//  update if want to update image 

    update_user_meta( $user_id, 'upload_field', $url);
}
?>

<form  action="" method="post" enctype="multipart/form-data">

<input type="file" name="fileToUpload" id="fileToUpload">

<input type="submit" name="uploade" value="Uplaode Image" />

</form>