Add widget before content area in post

How would one be able to achieve this?

I want to display a widget on posts before content.

Ideally, there would be an added “before content” widget area under appearance > widgets

Thanks!

Hi @cjavier,

There are 2 ways to achieve it. Either add a code inside a child theme functions.php or edit the template the post is using. This guide I will give you will be adding it using the template.

I can see you are using PRO RENEW stack. So the template single post are using is this one:
/wp-content/themes/pro/framework/views/renew/wp-single.php
You have a child theme already, please copy that file on the same folder on your child theme here:
/wp-content/themes/pro-child/framework/views/renew/wp-single.php
In case the exact folder path on your child theme is missing, please create it.
Then open the copied file, add the code of your widget. See below sample:

<?php

// =============================================================================
// VIEWS/RENEW/WP-SINGLE.PHP
// -----------------------------------------------------------------------------
// Single post output for Renew.
// =============================================================================

$fullwidth = get_post_meta( get_the_ID(), '_x_post_layout', true );

get_header();

?>

  <div class="x-container max width offset">
    <div class="<?php x_main_content_class(); ?>" role="main">
      <?php dynamic_sidebar( 'sidebar-main' ); ?> 
      <?php while ( have_posts() ) : the_post(); ?>

        <?php x_get_view( 'renew', 'content', get_post_format() ); ?>
        <?php x_get_view( 'global', '_comments-template' ); ?>
      <?php endwhile; ?>

    </div>

    <?php if ( $fullwidth != 'on' ) : ?>
      <?php get_sidebar(); ?>
    <?php endif; ?>

  </div>

<?php get_footer(); ?>

See where I added this widget line:
<?php dynamic_sidebar( 'sidebar-main' ); ?>
In case it is a custom widget that you have added on Appearance > Sidebar, you just need to use the ID of the widget:

The code will be:
<?php dynamic_sidebar( 'ups-sidebar-my-custom-widget' ); ?>

Then you can add any widgets on Appearance > Widgets.

Hope this helps.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.