Sunday, July 21, 2013

Wordpress custom code in php, add custom field in wordpress

Add custom field in wordpress

wp-admin-posts->edit post-> Screen options -> Custom field on

add value and title:

<?php the_meta(); ?>


<?php
require('blog/wp-blog-header.php');

function limit_words($string, $word_limit)
{
    $words = explode(" ",$string);
    return implode(" ", array_splice($words, 0, $word_limit));
}
//db parameters
$db_username = '';
$db_password = ';
$db_database = '';

//connect to the database
mysql_connect(localhost, $db_username, $db_password);
@mysql_select_db($db_database) or die("Unable to select database");

//get data from database -- !IMPORTANT, the "LIMIT 5" means how many posts will appear. Change the 5 to any whole number.
$query = "Select * FROM wp_posts WHERE post_type='post' AND post_status='publish'  ORDER BY id DESC LIMIT 1";

$query_result = mysql_query($query);
$num_rows = mysql_fetch_array($query_result);
$blog_id=$num_rows['ID'];
$blog_date = $num_rows['post_date'];
$blog_date= date('jS F, Y', strtotime($blog_date));
$blog_tags=wp_get_post_tags($blog_id,'name');

foreach($blog_tags as $term){
    $tag=$term->name;
    $tag1.=$tag.",";
}


   

$tag1=rtrim($tag1, ",");
//$blog_date=date('l jS  F Y ',$blog_date);

$blog_title = $num_rows['post_title'];
$blog_content = $num_rows['post_content'];
$blog_permalink =$num_rows['guid'];
$blog_post_author=$num_rows['post_author'];




//close database connection


function getAuthor($blog_post_author)
{
$query1=mysql_query("select * from wp_users where ID='$blog_post_author' ");
$ruser=mysql_fetch_array($query1);
return $ruser['user_login'];

}
function getaddress($id)  /// custom field value...
{

$query=mysql_query("select * from wp_postmeta where post_id='$id' and meta_key='Address' ");
$row=mysql_fetch_array($query);
return $row['meta_value'];
}

?>

Image

<?php $image1 = wp_get_attachment_url(get_post_thumbnail_id($blog_id) ); ?>
                    <img alt="img" src=" <?php echo $image1; ?>">

Comment
<?php
                        $comments = $wpdb->get_row("SELECT comment_count as count FROM wp_posts WHERE ID = '$blog_id'");
                        $commentcount = $comments->count;
                        echo $commentcount;?>

Tags

   <?php
                    $exp1=explode(",",$tag1);
                    foreach($exp1 as $v)
                    {
                    ?>
                        <li class="skin-background-color7 skin-background-hover2"><a href="blog/?tag=<?php echo $v; ?>" class="skin-color8"><?php echo $v; ?></a></li>
                       
                    <?php } ?>


Content limit

<?php
   
    $cont=$blog_content;
    echo limit_words($cont,100);  ?>


2 comments:

  1. Wow, this piece of writing is nice, my sister is analyzing such things, thus I am going to convey her.


    Also visit my homepage woodworking plans

    ReplyDelete
  2. Please let me know if you're looking for a writer for your site.
    You have some really good posts and I feel I would be a good asset.
    If you ever want to take some of the load off, I'd really
    like to write some material for your blog in exchange for a link back to mine.
    Please shoot me an email iff interested. Many thanks!

    Also visit my blog post - mohegan sun casino

    ReplyDelete

Thank you for your Comment....

Popular Posts