Monthly Archives: February 2018

How to add code to the header in WordPress

WordPress has a hook that lets us add arbitrary HTML code to the <header> tag of a website. Several plugins can be found that accomplish this, but it’s so easy to do that a plugin is often overkill.

Here’s how you can execute an arbitrary PHP function using the wp_head hook:

This example inserts some text into the <header> portion of the website. HTML tags can simply be written using echo, including double quotes.

How to show a list of all articles in WordPress

There’s a handy function in WordPress called wp_get_archives(). With it we can create a lot of useful output with just a few lines of code.

To list all articles ever published on a site, we can do this:

Here we setup a list of arguments and then give it to the function, which in turn gives us a nicely formatted list of every published article.

If we set the show_post_count argument to true, and replace the type argument to something like “yearly” or “monthly”, we’ll get clickable a list similar to this:


  • April 2015 (20)
  • March 2015 (11)
  • February 2015 (2)
  • January 2015 (7)
  • December 2014 (4)
  • November 2014 (12)

The number in brackets shows up when the show_post_count argument is set to true instead of false.




We can even find out how many articles have been published on the entire site by using the wp_count_posts() function. Here’s how to use it:

$totalPosts now holds the amount published posts. We can also query drafts or posts of any particular post type too if we wanted to (such as all status updates or video posts).