Bootstrap Templates Bundle

Showing posts with label Theme. Show all posts
Showing posts with label Theme. Show all posts

Friday, March 1, 2013

Automatically Set the Featured Image in WordPress

If no featured imaged is set then this code will add a default post thumbnail
add this code into functions.php file


function autoset_featured() {
          global $post;
          $already_has_thumb = has_post_thumbnail($post->ID);
              if (!$already_has_thumb)  {
              $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
                          if ($attached_image) {
                                foreach ($attached_image as $attachment_id => $attachment) {
                                set_post_thumbnail($post->ID, $attachment_id);
                                }
                           } else {
                                set_post_thumbnail($post->ID, '77');
                           }
                        }
      }  //end function
add_action('the_post', 'autoset_featured');
add_action('save_post', 'autoset_featured');
add_action('draft_to_publish', 'autoset_featured');
add_action('new_to_publish', 'autoset_featured');
add_action('pending_to_publish', 'autoset_featured');
add_action('future_to_publish', 'autoset_featured');



Change the red mark by your own attachement ID

Thursday, February 28, 2013

Setting default post image for different categories

If no image is attach to the post then rhis code will help us to show different post image for different categories


<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())) : ?>

<?php the_post_thumbnail('thumbnail'); ?>

<?php else :?>

<img src="<?php bloginfo('template_directory'); ?>/category/<?php $category = get_the_category(); echo $category[0]->cat_name; ?>.jpg" />

<?php endif;?>
</div>



make a folder category by name in your theme directory. then add some image for your every category. such as news.jpg,  uncategorized.jpg etc

Floating Share Bar without Plugin


  • First of all, Login to your WordPress Account, this will take you to the dashboard.
  • Now go to Appearance >> Widgets.
  • Now copy the following code from below [ Make sure you copy the entire code]

<!–TheWordpressBlog SideBar Floating Share Buttons Code Start–>
<style>

#pageshare {position:fixed; bottom:15%; left:10px; float:left; border: 1px solid black; border-radius:5px;-moz-border-radius:5px;-webkit-border-radius:5px;background-color:#eff3fa;padding:0 0 2px

0;z-index:10;}

#pageshare .sbutton {float:left;clear:both;margin:5px 5px 0 5px;}

.fb_share_count_top {width:48px !important;}

.fb_share_count_top, .fb_share_count_inner {-moz-border-radius:3px;-webkit-border-radius:3px;}

.FBConnectButton_Small, .FBConnectButton_RTL_Small {width:49px !important; -moz-border-radius:3px;-webkit-border-radius:3px;}

.FBConnectButton_Small .FBConnectButton_Text {padding:2px 2px 3px !important;-moz-border-radius:3px;-webkit-border-radius:3px;font-size:8px;}

</style>
<div id='pageshare' title="Share This With Your Friends">
<div class='sbutton' id='gb'><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like layout="box_count" show_faces="false" font=""></fb:like></div>
<div class='sbutton' id='rt'><a href="http://twitter.com/share" data-count="vertical" >Tweet</a><script src='http://platform.twitter.com/widgets.js' type="text/javascript"></script></div>
<div class='sbutton' id='gplusone'><script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script><g:plusone size="tall"></g:plusone></div>
<div class='sbutton' id='su'><script src="http://www.stumbleupon.com/hostedbadge.php?s=5"></script></div>
<div class='sbutton' id='digg' style='margin-left:3px;width:48px'><script src='http://widgets.digg.com/buttons.js' type='text/javascript'></script><a></a></div>
<br/><div style="clear: both;font-size: 9px;text-align:center;"><a href="http://thewordpressblog.org/how-to-add-floating-share-bar-to-wordpress-without-plugin" target="blank"><font color="black">[Share Bar]<font></font></font></a></div></div>

<!– TheWordpressBlog SideBar Floating Share Buttons Code End–>

http://thewordpressblog.org/how-to-add-floating-share-bar-to-wordpress-without-plugin/

alternate-single-post-template-for-specific-categories


Paste this below code into single.php file and create post-single.php and post-custom.php

<?php
        if ( have_posts() ) { the_post(); rewind_posts(); }
        if ( in_category(1) || in_category(2) || in_category (3) ) {
            include(TEMPLATEPATH . '/post-single.php');
        }
        else {
            include(TEMPLATEPATH . '/post-custom.php');
        }
    ?>


or



<?php
  $post = $wp_query->post;
  if (in_category('portfolio')) {
      include(TEMPLATEPATH.'/single_portfolio.php');
  } elseif (in_category('news')) {
      include(TEMPLATEPATH.'/single_news.php');
  } elseif(in_category('wordpress')) {
      include(TEMPLATEPATH.'/single_wordpress.php');
  }
  else{
      include(TEMPLATEPATH.'/single_default.php');
  }
?>

PHP code for getting thumbnail and default post image

This code will help us to set default thumbnail and getting thumnail


   <div style="margin: 12px;">
         
         <?php if( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail(array(350,250), array ('class' => 'alignleft')); ?>

<?php else : ?>
<img src="<?php echo get_bloginfo('template_directory');?>/images/defaultthumb.png" alt="Default Icon" title="Default Thumb" class="alignright" height="300px" width="460px"/>
<?php endif; ?>
         
       </div>

Thursday, February 14, 2013

Setting post image automatically as featured image for post thumbnail

Before using this code I use different types of plugins for showing post thumbnail. But this below code helps me to set my post image automatically as my thumbnail/ featured image, not only that , this code also set a default thumbnail
paste this code into functions.php file


// Get URL of first image in a post
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];

// no image found display default image instead
if(empty($first_img)){
$first_img = "http://newsprotidin.com/wp-content/uploads/2013/02/thumb-150x150.jpg";
}

 $first_img = vt_resize( $first_img, '', 460, 250, true );


return $first_img;
}

Tuesday, December 25, 2012

how-to-display-time-ago-for-posts-and-comments/


Open functions.php file of your theme and paste following code and Save:
?
1
2
3
4
function time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
}
Now place the following snippet in either single.php or comments.php (wherever you want to show it) within loop.
?
1
<?php echo time_ago(); ?>

Wokiee React eCommerce Template