[Resolved] Additional content every x comments

Home Forums Support [Resolved] Additional content every x comments

Home Forums Support Additional content every x comments

Viewing 15 posts - 1 through 15 (of 16 total)
  • Author
    Posts
  • #260336
    Michal

    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.

    #260436
    Tom
    Lead Developer
    Lead Developer

    Hmm, I actually can’t think of a way to do this. You might have luck posting over at Stack Exchange though: http://wordpress.stackexchange.com/

    #263302
    Michal

    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?

    #263480
    Tom
    Lead Developer
    Lead Developer

    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.

    #263513
    Michal

    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? ๐Ÿ˜‰

    #263586
    Tom
    Lead Developer
    Lead Developer

    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.

    #263611
    Michal

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

    #263623
    Michal

    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

    #263624
    Tom
    Lead Developer
    Lead Developer

    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.

    #263625
    Tom
    Lead Developer
    Lead Developer

    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.

    #263626
    Michal

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

    #263627
    Michal

    and here is my comments.php file

    http://pastebin.com/nRgMpUM3

    #263631
    Michal

    and custom functions regarding 'callback' => 'generate_comment'

    http://pastebin.com/8sCL4H1b

    #263636
    Tom
    Lead Developer
    Lead Developer

    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.

    #263642
    Michal

    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

Viewing 15 posts - 1 through 15 (of 16 total)
  • You must be logged in to reply to this topic.