Site logo

Archived topic

Replace comments title with ACF

3 replies · Started by Michal on April 27, 2021

Viewing posts 1–4 of 4

hello,
How to replace comments title with advanced custom fields output? In the old comments.php file it was something like

<?php if ( have_comments() ) { ?>

		<h2 class="comments-title">
					<?php the_field('acf'); ?>
				</h2>

		<?php the_comments_navigation(); ?>

I can see that there is some new comments.php file with the following content

do_action( 'generate_inside_comments' );

	if ( have_comments() ) :
		$comments_number = get_comments_number();
		$comments_title = apply_filters(
			'generate_comment_form_title',
			sprintf(
				esc_html(
					/* translators: 1: number of comments, 2: post title */
					_nx(
						'%1$s thought on &ldquo;%2$s&rdquo;',
						'%1$s thoughts on &ldquo;%2$s&rdquo;',
						$comments_number,
						'comments title',
						'generatepress'
					)
				),
				number_format_i18n( $comments_number ),
				get_the_title()
			)
		);

		// phpcs:ignore -- Title escaped in output.
		echo apply_filters(
			'generate_comments_title_output',
			sprintf(
				'<h3 class="comments-title">%s</h3>',
				esc_html( $comments_title )
			),
			$comments_title,
			$comments_number
		);

Where and how should I add <?php the_field('acf'); ?>
??

Hi there,

you can use this specific filter:

https://docs.generatepress.com/article/generate_comment_form_title/

So you could do something like this - including a fallback text:

add_filter( 'generate_comment_form_title', function() {
    // Get ACF Field 
    $acf_comment = get_field( 'your-acf-field' );
    if ( !empty($acf_comment) ) {
        return $acf_comment;
    } else {
        // Fall back text if ACF is empty
        return 'fallback text';
    }
} );

Super, it works. Thanks a ton

You're welcome

This archived topic is closed to new replies.