My Top WordPress Code Snippets

I have been developing WordPress themes for about 3 years now and there are a few snippets I use in every theme I build and some that I have used only a handful of times. They have come in handy and have saved my rear end countless times when a client wants something completely custom and the normal WordPress functionality can’t give that to me.  My first snippet I use on every portfolio site or gallery website that I build.

1. Get Attached Post Images In A List

This snippet allows the developer to pull the post images like they were a gallery but manipulate them more then the gallery short code allows you to. You can create an image slider, or a custom gallery. First post this snippet into your functions.php file.

After you have added that to your function.php file you will add the next code snippet to your single.php or which ever file is pulling the post you are editing.  This snippet will output the list on to your page.

you can place the above snippet inside a image slider or just float the images left to create a gallery.

2. Add Support for Thumbnails

Add this snippet into your functions.php file to add different thumbnail sizes to use in your theme files.

3. Related Posts By Category

Use this code snippet to show related posts by category at the bottom of your posts page. Great for blogs, stores, or portfolio sites. Add this to the bottom of your single.php file.

4. Related Posts By Tag

Use this snippet if you you are using tags to relate your posts. Add this snippet to the bottom of your single.php file.

5. Pull The Featured Post

You can use this code snippet any where just change the cat=4 to whatever the ID of your featured category is. This is great if you just want to display 1 post and you want to choose what post that is!

6. Get The Featured Image Of The Next Post

I don’t use this code snippet in every theme I build but i use it in most portfolio websites. I like to make the portfolio themes function so that when you are on a project at the end of the slider you have the chance to go to the next post. This code snippet allows you to do that but instead of just saying next post you see the

7. Get Featured Image and Use as Background

This is another plugin i don’t use all of the time but has come in handy more then once. It takes your post’s featured image and makes it the background image of the site for that page. Add this code snippet to your header.php file.

8. Adding Post Formats and Create Custom Templates

Post Formats have become a necessity in WordPress themes especially the blogs. They allow you to choose between multiple formats and display content differently based on those formats. First we need to register the formats, we do this by pasting this snippet into the functions.php file.

Next we can add this code snippet into our single.php file to allow us to create custom templates based on which format the user has chosen. with this format the single.php file will look for template files that begin with content and then end with the format, so for example the gallery format would be content-gallery.php, add this snippet to your single.php file.

9. Custom Failed Login Message

This snippet is good to add to every theme you create as it can help massively with your websites security.

As most WordPress developers know if WordPress recognizes the username but not the password it will tell you the password is incorrect leaving your site open to hackers who now know your username and can just hit your site trying to login with millions of password variations which is why i also recommend the plugin limit login attempts.

So there are my top Wordpres Code snippets, I love building Wordpres themes and would probably be lost without my collection of code snippets. I also use a very helpful site when I am beginning a new theme this site has all of the functions you need to add into your functions.php file to get started visit and check it out.

If you have any of your own code snippets you’d like to share or have any questions or comments about the ones I’ve posted let us know in the comments! Send us some of your WordPress Code Snippets and we will add them to our next WordPress Code SNippet Round Up: Add them in the comments or send them to us via the contact form

[info]This isn’t something I would normally place on my blog but my family is in need of some help. My uncle needs a double lung transplant and he does not have the money to get it done. My uncle was a huge part of my life growing up and it would be amazing if you could find it in your heart to help him out! They are raising money on a site called Give Forward[/info]

Related Posts

sublime_text Top 5 Drupal 7 Code Snippets blogfile A Simple Blogging Platform: BlogFile
  • Gerald@WPcustoms

    I grabbed #9 and added it to the archive at – credit link to your site is set – hope you don’t mind :)

    • Web Dev Snippets

      Thank you, glad we could contribute a snippet to your site!

  • Cosmin Tataru

    Hi Josh and thanks for your useful articles. I was wondering which sharing plugin you are using. Those buttons look really good. Could you please share that information with us ? Thanks again :D

    • Web Dev Snippets

      Hi Cosmin, thank you for the kind words and the share buttons are custom built into the theme.