From the Blogabout me

Customize your wp_get_archives() in WordPress

Usually I am very pleased with the WordPress community about how helpful they are with users who have questions.  Occasionally though, there is a question where for whatever reason people attack the user the question.

Thus I wanted to write about this one question/issue in particular because I have had multiple clients ask.  Although it’s not hard, it is not well documented at WordPress.

Have you ever been to a 404 WordPress page or even used a plugin/widget that displays Recent Posts using <ul> and <li> tags?
Have you ever wanted or wondered if this could be changed?  Great news, it can be.

If you are wanting to remove the <li> and </li> around your listing and customize the list, you are in luck.

In this tutorial I will cover how to take wp_get_archives and do some customization to it.

Open your 404.php or archive.php file and let’s get started.  Disclosure, I did use a Genesis child theme when doing this tutorial so if you see anything referring to Genesis, it’s not core to WordPress and my functions may be slightly different from what you see.

Okay, now you want to look near where your archive page has the header “Recent Posts” and you should see something like

Step #1


<ul>
<?php wp_get_archives( 'type=postbypost&limit=100' ); ?>
</ul>

Here you will want to modify the code by adding your modifications

format=custom  (this is required to take away the default format=html to be overwritten, and you now have two options.  A ‘before‘ and an ‘after‘ parameter.

Step #2

In my example I change the default

<?php wp_get_archives( 'type=postbypost&limit=100' ); ?>

to be

<?php wp_get_archives( 'format=custom&before=<p id="test">&after=</p>&type=postbypost&limit=100' ); ?>

Step #3

Now instead of a unordered list using <li></li>, the code will now be <p></p> and you are ready to go to town on customizations.  Endless possibilities.

I know this might be confusing with all of the code, so I’ve included a short video tutorial doing the same steps.

YouTube Preview Image

Comments

  1. excellent stuff. most extensive wp_get_archives treatment found after plenty of searching.

    any idea how html (e.g., link to an archives page) can be appended to the output of the Archives (sidebar) widget in Genesis? struggling to do this within functions.php.

    • k.shoffner says:

      I would recommend looking at Genesis Simple Hooks as an option. This will keep you from messing with the themes function.php or other files too much

  2. MigsMigallos says:

    Whoops, moderator, looks like the tags got formatted out of my reply. Sorry about that. Please just delete my comment to avoid confusion for future readers, and kindly review the posted code again. Tnx.

    • k.shoffner says:

      Thanks for your feedback, and I have adjusted accordingly as I did have a typo. I am not listing the old info, as it would probably cause some confusion, and have just adjusted the code within the Post.

Leave a Reply

%d bloggers like this: