• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2665

Original Genesis Tutorials & 5000+ Guaranteed Code

Snippets

  • Support
  • Newsletter
  • Videos
  • Log in

Premium Member? - Request custom code

Add Widget Before Post Content In Any WordPress Theme

Paste this PHP code at the end of your child themes functions.php file.

You’ll find a new widget which displays before the content on single posts and pages.

The code has been tested on the Twenty Eleven default theme for WordPress as well as the Genesis 2.0 Sample child theme.

Add Widget Before Single Posts & Pages In Genesis

Its preferable to use one of the code snippets on this page when using child themes built on the Genesis theme framework which are using the old XHTML markup and loop hooks.

Here’s an example of what works in themes running Genesis 2.0 with the new HTML 5 loop hooks & markup:

//* Register widget areas
genesis_register_sidebar( array(
	'id'          => 'before-content',
	'name'        => __( 'Before Content', '$text_domain' ),
	'description' => __( 'Before Content widget On Single Posts & Pages Only', '$text_domain' ),
) );


add_action( 'genesis_entry_content', 'add_widget_before_content', 5 );

function add_widget_before_content() {

	if( is_singular(array('post', 'page')) && is_active_sidebar('before-content') ) {
	
		genesis_widget_area( 'before-content', array(
			'before' => '<div class="before-content" class="widget-area">',
			'after'	 => '</div>',
		) );

}}

Style your widget using the .before-content class in your child themes style sheet.

Add New Widget Before Loop On Home Page Of Woo Theme

woo_main_before hook in Canvas theme

Paste this code in the custom code section of the Canvas themes functions.php file or your child themes functions file:

Need to change position?

Here’s a list of all Woo Hooks you can use in any theme.

Positioning Priority

Simply change the 3rd parameter in the action for positioning priority if needed.

Related Posts

  • Add Text After Title Or Before Content In Any Theme

Reader Interactions

Comments

  1. sherry says

    January 26, 2014 at 10:21 pm

    I tried to do this, but when I did, it cleared out all of the content from my pages…any idea how to get it to work?

    Log in to Reply
    • Brad Dalton says

      January 27, 2014 at 4:15 am

      Hello Sherry

      It depends on what theme you are using as all themes are coded differently.

      Try removing the 3rd parameter from the filter so the last line of code reads:

      add_filter( 'the_content', 'before_post_widget' );
      Log in to Reply
  2. Colin Crawford says

    September 29, 2013 at 10:39 pm

    Hi Brad
    I have had some help from Pippin and I think I have solved it…well it works anyway. You need to use the Output Buffer as it normally just echo’s out.

    Can show you the code but don’t know how to on here. I can tweet it to you and see what you think.

    Colin

    Log in to Reply
    • Brad Dalton says

      September 29, 2013 at 10:50 pm

      Sure

      Log in to Reply
      • Colin Crawford says

        September 29, 2013 at 11:03 pm

        It is just a bit more added code to yours to make it work…simple when you know how

        Log in to Reply
        • Brad Dalton says

          September 29, 2013 at 11:04 pm

          Yes i do know how to code widgets. Just interested in how you would do it?

          Log in to Reply
          • Colin Crawford says

            September 29, 2013 at 11:28 pm

            Brad I’m not a WordPress programmer or a PHP programmer but I can read through someone elses code and get a general idea or understanding from what that code is suppose to do.

            I was a little bit lost why it was adding it to the beginning of the post and why it wasn’t adding it to the end.

            The extra code is simple if you know how and when to use it and in no way I’m I up to your standard.

            I’ve sent the code via Twitter, it’s no big deal and I’m not trying to undermine you in anyway.

            It was a challenge to me to try and solve it even though I needed help elsewhere.

            Colin

          • Brad Dalton says

            September 29, 2013 at 11:47 pm

            Everything’s cool Colin.

            Stay positive as its all good and constructive.

  3. Colin Crawford says

    September 29, 2013 at 4:14 pm

    Thanks for the code Brad, I understand the first function to create the widget area but how does it add the widgets before the post, can we add after the post as well.

    Can’t seem to work the second function out.

    Cheers

    Colin

    Log in to Reply
    • Brad Dalton says

      September 29, 2013 at 7:48 pm

      The code is a working example of what you can use to add a widget before post content in any WordPress theme not just Genesis.

      If you’re wanting code for after post widget i have written a post about 5 ways to code after widgets in Genesis.

      Has your child theme been built on Genesis or another parent theme framework?

      Log in to Reply
      • Colin Crawford says

        September 29, 2013 at 8:34 pm

        Hi Brad
        Just trying to do the same but after the post and at the moment I’m using twentyeleven theme.

        I’ve added the widget area with no problems and have adjusted the before_post_widget function to after_post_widget. I have read that I need to append this code to the $content but with no luck.

        I just thought it would be useful after the Post content as well or on it’s own.

        Thanks

        Colin

        Log in to Reply
        • Brad Dalton says

          September 29, 2013 at 10:05 pm

          Are you blogging about WordPress Colin?

          Log in to Reply
          • Colin Crawford says

            September 29, 2013 at 10:20 pm

            I use WordPress to put websites together and want to learn how it works so I follow people like you to show me how it works and how to do it properly.

            I understand you have to create a child theme and not to touch any original theme files so adding extra functionality to the new functions.php is the way to go.

            I just feel having the widget after the post might also be useful as my customers will be able to alter the widgets there or any content. I know you can add extra content there but it has to be placed in functions.php and my customers won’t have a clue how to alter it.

            Widgets is the way to go, once set up.

            Colin

          • Brad Dalton says

            September 29, 2013 at 10:51 pm

            I will write a post about how to add a widget after posts soon.

  4. Zimbrul says

    September 29, 2013 at 6:27 am

    This is a great way to add pretty much anything before your blog post. I think the best serves AdSense placements. I’ve seen some blogs put in there even related posts.

    Log in to Reply
    • Brad Dalton says

      September 29, 2013 at 6:59 am

      Should work with any theme as well because it uses a WordPress hook.

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

PHP Code

template_include

get_body_class

if else

array

class_exists

foreach

sprintf

add_action

printf

variable

Advertise · WPEngine · Genesis · Log in

  • How Premium Membership Works
  • Sign Up
  • Support
  • Subscription Details/Invoice
  • Tagged Tutorials
  • Access-Download Problems