Useful Time-Saving WordPress Coding Techniques and Tricks for Post styling worth paying attention

  • 3 mins
  • 111 views

Wordpress Tips and Tricks

As per market reports, a constant boom have been outlined in the demand of PSD to WordPress related services in the web development industry. The reason is simple – WordPress has become one of the most powerful content management solution that offer great functionalities. With its latest releases such as WordPress version 3.0.5, the WordPress market has been hit by new WordPress coding tricks and techniques very useful for WordPress developers at the time of post styling. In today’s post of CSSCHOPPER, we would like to present some useful time-saving WordPress Coding techniques and tricks for Post Styling worth paying attention.

Trick 1: You can take the foremost advantage of both the post_class() function and the post ID to provide special styling to the posts individually. To apply style post individually open single.php file and replace the loop with

<?php if (have_posts()) : ?>

<?php while (have_posts()) : the_post(); ?>

<div <?php post_class() ?> id=”post-<?php the_ID(); ?>”>

<h3><a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></h3>

<?php the_content(); ?>

</div>

<?php endwhile; else: ?>

<?php _e(‘Sorry, no posts matched your criteria.’); ?>

<?php endif; ?>

Trick 2: Simply paste the below mentioned code after “ the _ content ()” function in the “ single.php file “ to display your related posts with a thumbnails below a particular post

<?php

$original_post = $post;

$tags = wp_get_post_tags($post->ID);

if ($tags) {

echo ‘<h2>Related Posts</h2>’;

$first_tag = $tags[0]->term_id;

$args=array(

‘tag__in’ => array($first_tag),

‘post__not_in’ => array($post->ID),

‘showposts’=>4,

‘caller_get_posts’=>1

);

$my_query = new WP_Query($args);

if( $my_query->have_posts() ) {

echo “<ul>”;

while ($my_query->have_posts()) : $my_query->the_post(); ?>

<li><img src=”<?php bloginfo(‘template_directory’); ?>/timthumb/timthumb.php?src=<?php echo get_post_meta($post->ID, “post-img”, true); ?>&h=40&w=40&zc=1″ alt=”” /><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”Permanent Link to <?php the_title_attribute(); ?>”><?php the_title(); ?></a></li>

<?php endwhile;

echo “</ul>”;

}

}

$post = $original_post;

wp_reset_query();

?>

Trick 3: Simply replace the already existing loop in the ” index.php file ” with the below mentioned easy-to-implement code to display all the latest article or blog post on the homepage.

<?php

$postnum = 0;

while (have_posts()) : the_post(); ?>

<?php if ($postnum <= 3){ ?>

<div <?php post_class() ?> id=”post-<?php the_ID(); ?>”>

<div class=”date”><span><?php the_time(‘M j’) ?></span></div>

<h2>(<?php echo $postnum;?>)<a href=”<?php the_permalink() ?>”><?php the_title(); ?></a></h2>

<div class=”post-image” style=”text-align:center;”>

<a href=”<?php the_permalink() ?>”><img src=”<?php bloginfo(‘template_directory’ ); ?>/timthumb.php?src=<?php echo catch_that_image(); ?>&amp;w=500&amp;h=200&amp;zc=1″ alt=”<?php the_title(); ?>” /></a>

</div>

<p><?php the_content(‘Read the rest of this entry &raquo;’); ?></p>

<p class=”more”><a href=”#”>Read More</a></p>

</div>

</div>

<?php } else {

<div <?php post_class( ‘single ‘ . $end ); ?> id=”post-<?php the_ID(); ?>”>

<div class=”post-content”>

<h3><a href=”<?php the_permalink() ?>”>(<?php echo $postnum; ?>)<?php the_title(); ?></a> <?php edit_post_link(‘_’, ”, ”); ?></h3>

<p><?php the_excerpt( ” ); ?></p>

<p class=”more”><a href=”#”>Read More ?</a></p>

</div>

</div><!– End post –>

<?php }

endwhile;

?>

 

Trick 4: Replace the “ the_title() ” function with the following code in the single.php file to overwrite post or page titles without any hassle.

<?php $title = get_post_meta($post->ID, “custom-title”, true);

if ($title != “”) {

echo “<h1>”.$title.”</h1>”;

} else { ?>

<h1><?php the_title(); ?></h1>

<?php } ?>

Trick 5: Paste this code wherever you like to display popular post in the sidebar of the homepage.

<h2>Popular Posts</h2>

<ul>

<?php $result = $wpdb->get_results(“SELECT comment_count,ID,post_title FROM $wpdb->posts ORDER BY comment_count DESC LIMIT 0 , 5”);

foreach ($result as $post) {

setup_postdata($post);

$postid = $post->ID;

$title = $post->post_title;

$commentcount = $post->comment_count;

if ($commentcount != 0) { ?>

<li><a href=”<?php echo get_permalink($postid); ?>” title=”<?php echo $title ?>”>

<?php echo $title ?></a> {<?php echo $commentcount ?>}</li>

<?php } } ?>

</ul>

The Conclusion

These useful WordPress coding techniques help WordPress developer to address common set of CMS implementation and customization challenges faced by them in day-to-day work.

you can visit https://www.csschopper.com/ to get more related article and blog stuffs.

Posted By: CSSChopper Team

One Response to “ Useful Time-Saving WordPress Coding Techniques and Tricks for Post styling worth paying attention ”

  1. Shenbagam

    Very nice and useful tricks

Comments

17 February 2011
  • *

    Enter the Text from the image below:
    (Text on image is not case sensitive)

Recent Posts