Archived topic
How to add widget below the post?
7 replies · Started by Jericho on February 20, 2023
Hello GeneratePress Support,
I am reaching out to inquire about how I can place a popular posts widget before/after the comment section of my blog post.
I have been using a popular posts widget in the right sidebar of my website, but as I am not using a sidebar anymore, I would like to place it in a different location. I would like to place the widget in either generate_before_comments_container or generate_after_main_content depending on my preference.
Could you please provide me with instructions or code snippets on how to achieve this? I would appreciate any help you can provide.
Thank you very much for your time and assistance.
Best regards,
Jericho
Hi there,
you can register you're own Widget area with the following PHP Snippet:
function db_after_content_widget() {
register_sidebar( array(
'name' => 'After Content Widget',
'id' => 'after_content_widget',
'before_widget' => '<div>',
'after_widget' => '</div>',
) );
}
add_action( 'widgets_init', 'db_after_content_widget' );
add_action('generate_after_main_content', function(){
if ( is_active_sidebar( 'after_content_widget' ) && is_single() ) : ?>
<div id="after-content-sidebar" class="widget-area" role="complementary">
<?php dynamic_sidebar( 'after_content_widget' ); ?>
</div>
<?php endif;
});
In this line: add_action('generate_after_main_content', function(){ you can set which Hook you want to use.
Hi David,
It's working but the alignment is not quite right. Could you assist me with adjusting the CSS so that the width matches the content?
Thank you for your help!
Hi Jericho,
What hook did you use? Can you try generate_after_entry_content?
Hi Fernando,
The default one that David gave. I believe it's `generate_after_main_content
Try adding this through Apperance > Customize > Additional CSS:
div#after-content-sidebar {
max-width: 600px;
margin-left: auto;
margin-right: auto;
}
Perfect! Many thanks to David and Fernando for the help!
You're welcome, Jericho!