5 PHP snippets for Wordpress

(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
Wordpress theme customization toolkit


Assign an image to post ID

This snip assigns an image to each unique post ID from Wordpress, which in this case I have made a gif. If you put a file in the image folder in your root directory of your Wordpress install called 8.gif - the 8th post you made on your blog will be assigned that image wherever you place this code. If there is no image for that id (you haven’t put a file called 8.gif in yet) it assigns the image called logo.gif. You can change any of the directories, file extensions, and the alternative image name to suit your own needs. You can find the post ID by looking in manage posts from your dash. You should use this code in template files.

<?php 
$image = “$id.gif”;
if(is_readable(“./images/$image”)) 
{
echo “<img src=’/images/$image’ alt=’alt tag’  />”; 

else 

echo “<img src=’/images/logo.gif’ alt=’alt tag’ />”; 

?>


To Separate the Trackbacks

Open comments.php, and search for the following line:

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

After it, paste the following:

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

Now look for:

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

And before it, paste:

<?php } /* End of is_comment statement */ ?>

This will display your comments without any trackbacks or pingbacks. To add a second comments loop for the trackbacks.

Look for the following line:

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

And before it, paste this:

<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>


(Either JavaScript is not active or you are using an old version of Adobe Flash Player. Please install the newest Flash Player.)
Wordpress theme customization toolkit

Grab Category Name

This PHP snippet will get the category of the current post and place it in line where the code is inserted. It would be useful to add a heading or phrase that relates to the category.

<? $cat = get_the_category(); $cat = $cat[0]; echo $cat->cat_name;?>

Dynamic List That Pulls The Most Recent Posts

A Wordpress function often used in sidebars that displays the most recent posts as a list. Replace 3 with how many items you want in your list.

<?php get_archives(’postbypost’, 3); ?>

Make Your Wordpress Header Image Clickable

Replace the starting div tag for the header with this:

<div id="header" onclick="location.href='http://www.pingable.org/';" style="cursor: pointer;">

www.pingable.org

18 Responses to “5 Wordpress PHP Code Snippets”

  1. Steven Snell on January 2nd, 2008 7:48 pm

    I think the “assign an image to a post” code could be very useful with some creativity. Thanks.

  2. Mike on January 4th, 2008 12:35 am

    Thanks for the great PHP information. I have been looking for a way to separate trackbacks. I will be implementing this into my blog.

  3. Video Games on January 7th, 2008 1:58 am

    I don’t quite get the “Grab Category Name” thing but I’m sure I’ll figure it out.

  4. Simon on January 7th, 2008 2:24 am

    @video Game: If you wanted to add a title or text in your template that had the category name of the category of page it was on, you could use that code.

  5. suicidalsam on January 12th, 2008 9:55 am

    I used to use blogger but recently moved to my own hosted wordpress and I am loving all the plugins and options in it.

    it was a bit hard for me to adjust to php codes but this kind of tips would surely help me out. thanx, great post. I will be trying out some of the tricks.

  6. Watson on February 6th, 2008 12:10 pm

    Thank you very much I appreciate the tips in here and will be implementing some into my blog. I love wordpress. It is awesome.

  7. LukeG on April 8th, 2008 12:36 am

    Thanks the get archives function was exactly what I was after (took about 90 mins of searching!).

    Great post.

  8. Yazılım on June 22nd, 2008 3:30 pm

    very good sharing thanks

  9. Ikki on June 23rd, 2008 6:15 pm

    I found the first one to be the most useful one :)

    Very cool! I’ll try to implement something similar with my new soon-to-code-by-myself WP theme :D

  10. Ryse on June 26th, 2008 12:34 pm

    Man I’ve been searching far and wide for the way to seperate trackbacks from comments. Thanks for finally putting my mind at ease brother.

  11. Artist Easel on June 29th, 2008 5:21 pm

    Bookmarked! Some excellent snippets of code here. Nice to have them all in one place.

  12. Used Woodworking Tools on July 4th, 2008 2:50 am

    Very useful stuff. I don’t know how many times I have had to go searching for the same little code snippets!

  13. dimon from best credit cards on July 16th, 2008 12:13 pm

    thanks for the great PHP info man

Trackbacks:

  1. roScripts - Webmaster resources and websites
  2. Il Blog di Marchino » Archivio » links for 2008-01-04
  3. Ejemplos de código para Wordpress « Semanticode
  4. Weekly Links - January 6th | Vandelay Website Design
  5. 5 recortes de codigo PHP para wordpress | frogx.three

Leave a Reply




This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.