Add Custom Field Before Sidebar On Any Post

In this post i’ll teach you how to create a new custom content box on every edit post screen.

The new meta box enables you to add any type of unique content before your primary sidebar of every post you publish.

Here’s what it looks like on the front end of 1 single post:

demo

And here’s the new box which is added to all your edit post screens:

custom meta box

You can use this solution to:

  • Display different adverts on different posts, above the fold.
  • Any type of content including videos, text, HTML etc. before your sidebar

There’s only 2 steps you need to follow to add your own mini sidebar:

  1. Add PHP code to your child themes functions file.
  2. Create your custom field using the incredible Advanced Custom Fields (ACF) plugin.

Step 1

There’s only 4 lines of PHP code you need to paste at the end of your child themes functions.php file.

No CSS code is needed unless you want to style your custom field differently to what your themes default primary sidebar uses.

Step 2

Once you install the ACF plugin, click to enlarge this image and use exactly the same settings as this screenshot includes.

new custom field

That’s all you need to do in order to add a new box which enables you to display different content above the sidebar on different posts on your site.

More Tutorials Which Include Using Custom Fields


Comments

12 responses to “Add Custom Field Before Sidebar On Any Post”

  1. Thank you for this! I used it and it works great! I was wondering if there’s a way to code in a title? (I’m new to this!)

  2. Dave Reasons Avatar
    Dave Reasons

    Thank you Brad, the custom sidebar works great and your instructions were concise and easy to follow.

    1. Brad Dalton Avatar
      Brad Dalton

      Thanks for the comment Dave.

  3. Keith Davis Avatar
    Keith Davis

    Hi Brad
    Nice and neat and oh so useful for adding unique content for each post.

    Agree with Zimbrul…
    “And I think is smarter solution than using Simple Sidebars as you can implement this on a post by post basis without ending up with tens of sidebars…”

    Simple sidebars would be useful if you use the same sidebar for a particular post category – that would limit the number of sidebars needed.

    1. Brad Dalton Avatar
      Brad Dalton

      That’s right Keith nd you can easily change the hook, conditional tag and field type to suit your needs.

  4. This is a neat trick! And I think is smarter solution than using Simple Sidebars as you can implement this on a post by post basis without ending up with tenth of sidebars (one for each post if required).
    So this is the way you are doing the index of your tutorials on this site? I thought you either use Simple sidebars or some plugin like Widget logic.

    1. Brad Dalton Avatar
      Brad Dalton

      Simple sidebars is a good solution for creating a handful of custom sidebars but not if you want totally unique content on each post or page.

      That’s where custom fields take over and also enable you to choose from a large range of field types using the ACF plugin.

      1. I agree with you, would be quite uneconomical to have such many custom sidebars.
        I guess we can use the same technique like in this tutorial to build an “after post” custom content block

        1. Brad Dalton Avatar
          Brad Dalton

          Exactly. Simply change the hook

  5. Hi Brad! Nice feature.

    I see you are using genesis_get_custom_field() from genesis instead the get_field() from ACF or the get_post_meta() from WordPress. Why is that? Just a preference, or it gives you more options or flexibility than the others?

    1. Brad Dalton Avatar
      Brad Dalton

      Just for something different.

Leave a Reply

Join 5000+ Followers

Get The Latest Free & Premium Tutorials Delivered The Second They’re Published.