
Sửa lỗi Đối tượng “Comment” phải được lồng vào trong đối tượng “CreativeWork”
GeneratePress là một theme WordPress nhẹ gần như top 1 hiện nay, chúng linh hoạt và tối ưu hóa cao, phù hợp cho cả người dùng phổ thông và lập trình viên. Với dung lượng chưa đến 30KB, GeneratePress giúp trang web tải nhanh, cải thiện hiệu suất và điểm số trên Google PageSpeed.
Nếu bạn cần một theme nhanh, ổn định và dễ sử dụng, GeneratePress là lựa chọn hàng đầu! 🚀
Tuy nhiên GeneratePress gặp 2 lỗi sau mà khá lâu rồi tác giả không fix nên dưới đây là tip để bạn sửa nhanh 2 lỗi Đối tượng “Comment” phải được lồng vào trong đối tượng “CreativeWork” và Trường “url” bị thiếu (nằm trong “author”) khi sử dụng theme GeneratePress
The “Comment” object must be nested within the “CreativeWork” object and the “url” field is missing (located within “author”) GeneratePress theme
Sau khi xác thực chờ đợi là xong
Thực chất cái này chỉ là warning và bạn có thể bỏ qua nhưng do nhiều khách hàng mình cần fix triệt để để pass qua warning trong Google search console nên tiện thể mình share cách fix hy vọng sẽ giúp ích được cho nhiều người.
Bài viết liên quan
Dạo qua các diễn đàn hỗ trợ GeneratePress thì nhiều người báo cáo mà ông làm theme này cứ chày cối bảo là không ảnh hưởng tới ranking, thì đúng là thực tế không ảnh hưởng tới ranking nhưng có warning thì fix vài nhát là xong, cứ để người ta tạo vấn đề mãi thì không hay
Cách 2
Vào theme mẹ GeneratePress và mở file comments.php
lên tìm đến đoạn
wp_list_comments( array( 'callback' => 'generate_comment', ) );
Thay thế nguyên code trên bằng code dưới đây là xong
wp_list_comments( array( 'callback' => function($comment, $args, $depth) { ?> <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>"> <article id="div-comment-<?php comment_ID(); ?>" class="comment-body" itemscope itemtype="https://schema.org/CreativeWork"> <div itemprop="comment" itemscope itemtype="https://schema.org/Comment"> <footer class="comment-meta" aria-label="Siêu dữ liệu bình luận"> <div class="comment-author-info"> <div class="comment-author vcard" itemprop="author" itemscope itemtype="https://schema.org/Person"> <?php printf('<cite itemprop="name" class="fn">%s</cite>', get_comment_author_link() ); ?> </div> <div class="entry-meta comment-metadata"> <a href="<?php echo esc_url( get_comment_link() ); ?>"> <time datetime="<?php comment_time( 'c' ); ?>" itemprop="datePublished"> <?php printf('%s vào lúc %s', get_comment_date(), get_comment_time()); ?> </time> </a> </div> </div> </footer> <div class="comment-content" itemprop="text"> <?php comment_text(); ?> <span class="reply"> <?php comment_reply_link(array_merge($args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ))); ?> </span> </div> </div> </article> </li> <?php } ) );
Cách 2
Cách fix, bạn tạo 1 file có tên là generate_comment.php
trong child theme GeneratePress
function custom_generate_comment($comment, $args, $depth) { $GLOBALS['comment'] = $comment; ?> <li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>"> <article id="div-comment-<?php comment_ID(); ?>" class="comment-body" itemscope itemtype="https://schema.org/CreativeWork"> <div itemprop="comment" itemscope itemtype="https://schema.org/Comment"> <footer class="comment-meta" aria-label="<?php esc_attr_e( 'Comment meta', 'generatepress' ); ?>"> <?php if ( 0 != $args['avatar_size'] ) : ?> <div class="comment-author-image"> <?php echo get_avatar( $comment, $args['avatar_size'] ); ?> </div> <?php endif; ?> <div class="comment-author-info"> <div class="comment-author vcard" itemprop="author" itemscope itemtype="https://schema.org/Person"> <?php printf('<cite itemprop="name" class="fn">%s</cite>', get_comment_author_link() ); ?> </div> <?php if ( ! generate_is_using_wp_block_comments() ) : ?> <div class="entry-meta comment-metadata"> <a href="<?php echo esc_url( get_comment_link() ); ?>"> <time datetime="<?php comment_time( 'c' ); ?>" itemprop="datePublished"> <?php printf( /* translators: 1: date, 2: time */ _x( '%1$s at %2$s', '1: date, 2: time', 'generatepress' ), get_comment_date(), get_comment_time() ); ?> </time> </a> </div> <?php endif; ?> </div> </footer> <div class="comment-content" itemprop="text"> <?php comment_text(); ?> </div> <?php if ( ! generate_is_using_wp_block_comments() ) : ?> <?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?> <?php endif; ?> </div> </article> </li> <?php }
Sau đó, thêm code sau vào file functions.php
của child theme
function override_generate_comment() { require get_stylesheet_directory() . '/generate_comment.php'; return 'custom_generate_comment'; } add_filter('generate_comment_callback', 'override_generate_comment');