Archived topic
Move Comments section before the footer
12 replies · Started by Alejandro on February 17, 2021
Hi,
My blog section is organized in two columns (content + sidebar). At the end of the content column I have the comments section which I would like to move outside and place it before the footer (along with other elements such breadcrumbs, social share buttons, related posts, etc).
Apart from the comments, I can move the rest of the items via Hook Element but I do not know how to deal with the comments.
Could you provide some help here?
Thank you
Hi there,
currently it is baked into the single.php
Tom explains what to do here:
https://generatepress.com/forums/topic/move-comments-to-different-area/#post-1274565
Then further down on the same topic how to hook the form back in:
https://generatepress.com/forums/topic/move-comments-to-different-area/#post-1277317
Oh, wonderful!
I am using a child theme so the only thing I have to do is to replicate the file with Tom's indications on the child theme folder in my server right?
thank you
Thats correct!
Mmmm, I was looking for the piece of code you mention (link) but does not look like my single.php file:
<?php
/**
* The Template for displaying all single posts.
*
* @package GeneratePress
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
get_header(); ?>
<div id="primary" <?php generate_do_element_classes( 'content' ); ?>>
<main id="main" <?php generate_do_element_classes( 'main' ); ?>>
<?php
/**
* generate_before_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_before_main_content' );
if ( generate_has_default_loop() ) {
while ( have_posts() ) :
the_post();
generate_do_template_part( 'single' );
endwhile;
}
/**
* generate_after_main_content hook.
*
* @since 0.1
*/
do_action( 'generate_after_main_content' );
?>
</main>
</div>
<?php
/**
* generate_after_primary_content_area hook.
*
* @since 2.0
*/
do_action( 'generate_after_primary_content_area' );
generate_construct_sidebars();
get_footer();
Could you tell me which part of the code I should get rid off?
Thanks!
As of 3.0.0, it's actually hooked into the template: https://github.com/tomusborne/generatepress/blob/3.0.2/inc/structure/comments.php#L177
So we can move it like this:
add_action( 'wp', function() {
remove_action( 'generate_after_do_template_part', 'generate_do_comments_template', 15 );
} );
add_action( 'generate_before_footer', function() {
if ( is_page() || is_single() ) {
if ( comments_open() || '0' != get_comments_number() ) :
/**
* generate_before_comments_container hook.
*
* @since 2.1
*/
do_action( 'generate_before_comments_container' );
?>
<div class="comments-area">
<?php comments_template(); ?>
</div>
<?php
endif;
}
} );
Thanks Tom.
I've added this code to my functions.php file in my child theme and I can see the comments right before the footer.
But I still don't know how to get rid of the comments in their original position... Where (and which) lines I have to delete?
Best, A.
That's what this line should do:
remove_action( 'generate_after_do_template_part', 'generate_do_comments_template', 15 );
Can you confirm that it's been added?
Yep, confirmed!
I mean, confirmed that has been added but is not working well as said before...
Can you try the updated code here?: https://generatepress.com/forums/topic/move-comments-section-before-the-footer/#post-1663535
Works flawlessly! Thanks Tom & David :-)
Glad we could help! :)