Site logo

Archived topic

Additional content every x comments

15 replies · Started by Michal on January 3, 2017

Viewing posts 1–15 of 16

Hi,
Do you have any idea of if/how is possible to add some content every let's say 5 comments in generatepress theme? I could use "more fields" plugin but it would create content after each and every comment.

As a developer do you know any way to add some content after 5 comments on the blog?

e.g.

comment 1
comment 2
comment 3
comment 4
comment 5
MY CONTENT
comment 6
comment 7
comment 8
comment 9
comment 10
MY CONTENT

PS. It's not directly related to Generatepress but I hope your experience can help here.

Hi again,

I got a response with a proper answer which does the job ;)

http://wordpress.stackexchange.com/questions/251219/additional-content-every-x-comments/251247#251247

However I have a custom php function which changes the comments's look.

function generate_disable_comment_fields($fields) {
  unset($fields['url']);
  unset($fields['email']);
  unset($fields['author']);
  return $fields;
}
add_filter('comment_form_default_fields','generate_disable_comment_fields');
 
if ( ! function_exists( 'generate_comment' ) ) :
/**
 * Template for comments and pingbacks.
 *
 * Used as a callback by wp_list_comments() for displaying the comments.
 */
function generate_comment( $comment, $args, $depth ) {
    $GLOBALS['comment'] = $comment;
    $args['avatar_size'] = apply_filters( 'generate_comment_avatar_size', 50 );
 
    if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>
 
    <li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
        <div class="comment-body">
            <?php _e( 'Pingback:', 'generate' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'Edit', 'generate' ), '<span class="edit-link">', '</span>' ); ?>
        </div>
 
    <?php else : ?>
 
    <li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
        <article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
            <footer class="comment-meta">
                <div class="comment-author-info">
                    <div class="entry-meta comment-metadata">
                        <?php edit_comment_link( __( 'Edit', 'generate' ), '<span class="edit-link"> ', '</span>' ); ?>
                    </div><!-- .comment-metadata -->
                </div><!-- .comment-author-info -->
 
                <?php if ( '0' == $comment->comment_approved ) : ?>
                <p class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'generate' ); ?></p>
                <?php endif; ?>
            </footer><!-- .comment-meta -->
 
            <div class="comment-content">
                <?php comment_text(); ?>
                <?php
                        comment_reply_link( array_merge( $args, array(
                            'add_below' => 'div-comment',
                            'depth'     => $depth,
                            'max_depth' => $args['max_depth'],
                            'before'    => '<span class="reply"> ',
                            'after'     => '</span>',
                        ) ) );
                        ?>
            </div><!-- .comment-content -->
 
           
        </article><!-- .comment-body -->
 
    <?php
    endif;
}
endif; // ends check for generate_comment()

and a piece of code from comments.php file look like this:

wp_list_comments( array( 
					'callback' => 'generate_comment'
				) );

How to change that so the comments section look like before and I could add this additional content every x lines?

To do that you have to compose

wp_list_comments( array( 
					'callback' => 'generate_comment'
				) );

and

wp_list_comments( [
    '_inject_period'  => 5,
    '_inject_content' => [ 'AAA', 'BBB', 'CCC' ],
] );

somehow. But how? Could you help with that?

You would need to use a child theme, then copy comments.php from the parent theme and add it to the child theme.

Then you can make changes to the code in there without worrying about updates losing your changes.

ehh you don't get it. I know how it works.

I am not able (because of my php skills) to replace standard

wp_list_comments( array( 
					'callback' => 'generate_comment'
				) );

with

wp_list_comments( [
    '_inject_period'  => 5,
    '_inject_content' => [ 'AAA', 'BBB', 'CCC' ],
] );

as developer from stackexchange said because I will loose comments.php customizations (nicknames and dates)

and from my point of view it can be remained (both comments.php styling - lack nicknames and dates and additional content between them) by merging this two pieces of code together somehow.

but I don't know how because I'm not a php developer. You are so that I thougth you come with an idea because you helped me to style the comments some time ago.

DO you understand now? ;)

as developer from stackexchange said because I will loose comments.php customizations (nicknames and dates)

Not if you copy comments.php and place it in your child theme. That's the only way to edit that file and replace that function without losing your changes when you update.

But I am replacing that function in a child theme (comments.php) as I said.

We have here

'callback' => 'generate_comment'which links to my custom php file and when I remove it by replacing it with code which I got from another developer it will not work. It works with default comments.php file though

Can you show me the contents of your child theme comments.php file in a pastebin or a gist? If your child theme has that file and the function they say to replace is replaced, then it should work as long as the code in that answer is correct.

If not then you may need to find another method.

So what if you merge the args? Keep the generate_comment callback but add the other parts as well instead of fully replacing it.

Never done this so I'm not sure it will work - purely guessing.

I tried but I could make a mistake with "" ' ] etc Can you point me an example of how to merge this to work together?

So you would replace:

wp_list_comments( array(
    'callback' => 'generate_comment'
) );

With:

wp_list_comments( array(
    'callback' => 'generate_comment',
    '_inject_period'  => 5,
    '_inject_content' => array( 'AAA', 'BBB', 'CCC' ),
) );

Again, I have no idea if this will work or not.

It worked. Just as I thougth - I made the same but with a tiny mistake.

I made a small donation to thank you for your support.

Thanks again

This archived topic is closed to new replies.