Posts Tagged PHP

Why Use Code Hacks

Over the past couple of days I have been sharing some of the better hacks that I have encountered and ones that I have adapted for Wordpress.  It occurred to me as I was re-reading those that not all the readers of this blog might realize why its a good idea to use these.

There are actually a few reasons to become familiar with these code snippets. Firstly, if you are going to be a serious blogger then getting to know what the various files within your theme do is a good way to be able to customize your theme as your blog develops and make it standout from others, especially if you are using a free theme.

Second reason to use code is that it is faster than plugins, especially for the more simple effects that we have been showing you. For example adding the code to request readers subscribe to your blog at the end of every post can be done with a plugin, but every plugin you add to your blog slows it down just a little. Leave your readers waiting too long for your blog to load and they will move on.

So go ahead, back up your files, and don’t be afraid to start playing around to see what you can achieve.

Reblog this post [with Zemanta]

, , , ,

Separate Your Comments

Giving your readers the opportunity to see comments separate from pingbacks/trackbacks makes your comments section much more readable and more useful for your readers.

The following is a Wordpress coding tip - so before you do this, make sure you back up your comments.php as we will be editing it.

In your comments.php file find the following code:

<?php foreach ($comments as $comment) : ?>

Insert this after it:

<?php $comment_type = get_comment_type(); ?>
<?php if($comment_type == 'comment') { ?>

Then find the following piece of code:

<?php endforeach; /* end for each comment */ ?>

and insert this before it

<?php } else { $trackback = true; } /* End of is_comment statement */ ?>

Finally find this section of code:

<?php else : // this is displayed if there are no comments so far ?>

And insert this before it:

<?php if ($trackback == true) { ?>
  <h3>Trackbacks</h3>
  <ol>
  <?php foreach ($comments as $comment) : ?>
  <?php $comment_type = get_comment_type(); ?>
  <?php if($comment_type != 'comment') { ?>
  <li><?php comment_author_link() ?></li>
  <?php } ?>
  <?php endforeach; ?>
  </ol>
<?php } ?>

That’s it, now your readers can clearly see which are comments and which are pingbacks/trackbacks.

Reblog this post [with Zemanta]

, ,

List Your Upcoming Posts

A sure way to bring readers back to your blog is to tell them what is coming up for release. This is an especially useful feature if you use the post scheduling feature in WordPress. While there are probably plugins that will do this for you, all you actually need is this code snippet. You can just copy and paste it into one of your theme files where you want the list to appear - e.g. the sidebar.

WARNING: Do not do this if you are not comfortable altering the files in your theme. Make sure you make a back up of the file that you are going to change before making the change.

<div id=“crystal_ball”>
<div id=“crystal_ball_header”><p>Future events</p></div>
<?php query_posts(’showposts=10&post_status=future’); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div >
<p class><b><?php the_title(); ?></b><?php edit_post_link(‘e’,‘ (’,‘)’); ?><br />
<span class=“datetime”><?php the_time(‘j. F Y’); ?></span></p>
</div>
<?php endwhile; else: ?><p>No future events scheduled.</p><?php endif; ?>
</div>

This willl produce a list of posts that are marked Future. If you want to show less or more than 10 upcoming posts just change the number in the line “ <?php query_posts(’showposts=10&post_status=future’); ?> ” to the number of posts you want to display.

Its as easy as that.

Reblog this post [with Zemanta]

, ,