5 Wordpress PHP Code Snippets

5 Wordpress PHP Code Snippets

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 [...]

Author : Simon

Author's Website | Articles from Simon

The creator of Pingable. We write about everything Wordpress.

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>

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

Like this post? Share it!

  • Tweet
  • Facebook
  • Diggit
  • Delicious
  • Diggit
  • Diggit
  • Diggit

Use the same Wordpress theme as us:


User Comments


  1. Steven Snell
    January 2, 2008

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



  2. Mike
    January 4, 2008

    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
    January 7, 2008

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



  4. Simon
    January 7, 2008

    @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
    January 12, 2008

    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
    February 6, 2008

    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
    April 8, 2008

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

    Great post.



  8. Yazılım
    June 22, 2008

    very good sharing thanks



  9. Ikki
    June 23, 2008

    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
    June 26, 2008

    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
    June 29, 2008

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



  12. Used Woodworking Tools
    July 4, 2008

    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
    July 16, 2008

    thanks for the great PHP info man



  14. Crashx
    July 25, 2008

    Thanks for sharing! I believe there is a few more of these snippets at programminghelp.com. Cya!



  15. Spyder Paintball Guns
    August 7, 2008

    Perfect. Just the thing I have been looking for all in one place. Thanks!



  16. Fight A Speeding Ticket
    August 18, 2008

    This is a little jem of a link. I find myself searching for these same code snippets from time to time!



  17. Kevin from Web hosting Ireland.
    September 22, 2008

    Some good snippets. I like the fact you also told how to modify the wordpress code for your own use. Most people list code or link and never go through how you can change them for your own use.

    Keep up the good work always something good on your site.



  18. Nick from no win no fee claims
    November 10, 2008

    Very useful information. Making your header logo clickable as a homepage link is certainly worth doing. Thanks



  19. tony from Register domian names
    December 6, 2008

    Making image a link with alt tags is one i would always do. I also like the code for trackbacks.



  20. malcolm coles from wordpress duplicate content
    December 31, 2008

    Presumably you can combine the image tip and category tip to add a unique image to each category page?



  21. marry from datingservices
    May 17, 2009

    I like the one that pulls the last post. Seems to help alot in seo and makes it easier for people to find more posts they like.



  22. jack from exon mobil
    June 16, 2009

    Thanks i like the post title and last post codes. They help make site more seo and easier to navigate.



  23. Ezuca
    October 7, 2009

    It’s not working.



  24. DV from Pico RG
    October 10, 2009

    Thanks for the code dude, it can be very useful since many have Wordpress installed as the default CMS script



  25. emil from internet marketing ireland
    December 4, 2009

    Thanks for the codes amigo. Would implement it to my two sites.Looking forward for more easy to follow codes.Cheers!


Leave a Reply

Comment Policy: This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage. Comments that use keywords in the name field in the incorrect format will not be approved. Links to inappropriate sites will also not be approved. Do not bother wasting my time by thanking me for a post. Add something to the discussion or your comment will be deleted

Rss Feeds   Twitter Followers Email Updates