با عرض سلام خدمت کاربران محترم سایت پی وی لرن.
با جلسه ای دیگر از سری جلسات دوره جامع آموزش ساخت قالب وردپرس در خدمت شما هستیم.
در این جلسه قصد داریم به آموزش فایل قالب Comment در وردپرس که تحت عنوان comments.php استفاده میشود، بپردازیم.
وردپرس نظرات سایت را با استفاده از تنظیمات و کد های فایل comments.php قالب سایت، نمایش میدهد.
در کد زیر، با استفاده از یک کوئری لیست نظرات تایید شده را به دست آورده و آن ها را با استفاده از یک حلقه (loop) نمایش میدهیم.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | $args = array( 'status' => 'approve' ); // The comment Query $comments_query = new WP_Comment_Query; $comments = $comments_query->query( $args ); // Comment Loop if ( $comments ) { foreach ( $comments as $comment ) { echo '<p>' . $comment->comment_content . '</p>'; } } else { echo 'No comments found.'; } |
فایل comments.php تمام داده های مورد نیاز برای نمایش نظرات را شامل بوده و آن ها را از دیتابیس استخراج میکند تا بتوانیم نظرات را نمایش دهیم.
قبل از اینکه از فایل قالب comment برای نمایش نظرات استفاده کنیم، بهتر است تا با استفاده از تگ های شرطی بررسی کنیم کخ آیا نظرات باز هستند و یا آیا اصالا نظری وجود دارد یا نه.
1 2 3 4 | // If comments are open or we have at least one comment, load up the comment template. if ( comments_open() || get_comments_number() ) : comments_template(); endif; |
کد زیر نیز مثال دیگری از نمایش نظرات در قالب است.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | <?php /** * The template for displaying Comments. * * The area of the page that contains comments and the comment form. * * @package WordPress * @subpackage Twenty_Thirteen * @since Twenty Thirteen 1.0 */ /* * If the current post is protected by a password and the visitor has not yet * entered the password we will return early without loading the comments. */ if ( post_password_required() ) return; ?> <div id="comments" class="comments-area"> <?php if ( have_comments() ) : ?> <h2 class="comments-title"> <?php printf( _nx( 'One thought on "%2$s"', '%1$s thoughts on "%2$s"', get_comments_number(), 'comments title', 'twentythirteen' ), number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' ); ?> </h2> <ol class="comment-list"> <?php wp_list_comments( array( 'style' => 'ol', 'short_ping' => true, 'avatar_size' => 74, ) ); ?> </ol><!-- .comment-list --> <?php // Are there comments to navigate through? if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?> <nav class="navigation comment-navigation" role="navigation"> <h1 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h1> <div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'twentythirteen' ) ); ?></div> <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'twentythirteen' ) ); ?></div> </nav><!-- .comment-navigation --> <?php endif; // Check for comment navigation ?> <?php if ( ! comments_open() && get_comments_number() ) : ?> <p class="no-comments"><?php _e( 'Comments are closed.' , 'twentythirteen' ); ?></p> <?php endif; ?> <?php endif; // have_comments() ?> <?php comment_form(); ?> </div><!-- #comments --> |
در ادامه هر یک از قسمت های کد بالا را جداگانه توضیح خواهیم داد.
فایل قالب بالا با توضیحاتی در مورد اطلاعات قالب شروع میشود.
1 2 3 4 5 6 7 8 9 10 | <?php /** * The template for displaying Comments. * * The area of the page that contains comments and the comment form. * * @package WordPress * @subpackage Twenty_Thirteen * @since Twenty Thirteen 1.0 */ |
در ادامه بررسی میکند که آیا پست توسط رمز عبور محافظت شده است یا خیر. اگر محافظت شده باشد، از اجرای ادامه کد جلوگیری میشود:
1 2 3 4 5 6 7 | /* * If the current post is protected by a password and the visitor has not yet * entered the password we will return early without loading the comments. */ if ( post_password_required() ) return; ?> |
در نهایت نیز بررسی میشود که پست حاوی نظرات است یا نه.
1 2 3 | <div id="comments" class="comments-area"> <?php if ( have_comments() ) : ?> |
این بخش از کد، قسمت عنوان و بالای نظرات را نمایش میدهد:
1 2 3 4 5 6 | <h2 class="comments-title"> <?php printf( _nx( 'One thought on "%2$s"', '%1$s thoughts on "%2$s"', get_comments_number(), 'comments title', 'twentythirteen' ), number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' ); ?> </h2> |
کد زیر نیز لیست مرتبی از نظرات ایجاد میکند:
1 2 3 4 5 6 7 8 9 | <ol class="comment-list"> <?php wp_list_comments( array( 'style' => 'ol', 'short_ping' => true, 'avatar_size' => 74, ) ); ?> </ol><!-- .comment-list --> |
این بخش نیز بررسی میکند که نظرات نیاز به صفحه بندی دارند یا خیر. اگر داشته باشند، آن ها را صفحه بندی میکند.
1 2 3 4 5 6 7 8 9 10 | <?php // Are there comments to navigate through? if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : ?> <nav class="navigation comment-navigation" role="navigation"> <h3 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', 'twentythirteen' ); ?></h3> <div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', 'twentythirteen' ) ); ?></div> <div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', 'twentythirteen' ) ); ?></div> </nav><!-- .comment-navigation --> <?php endif; // Check for comment navigation ?> |
اگر نظرات باز نباشد، پیامی مناسب به کاربر نمایش میدهد:
1 2 3 | <?php if ( ! comments_open() && get_comments_number() ) : ?> <p class="no-comments"><?php _e( 'Comments are closed.' , 'twentythirteen' ); ?></p> <?php endif; ?> |
در این بخش نیز حلقه مربوط به پردازش نظرات بسته شده و فرم ایجاد دیدگاه نمایش داده میشود.
1 2 3 4 5 | <?php endif; // have_comments() ?> <?php comment_form(); ?> </div><!-- #comments --> |
گاهی اوقات میخواهید تا قسمت نظرات را متفاوت از قالب نمایش دهید.
برای این کار یک فایل دیگر ساخته (به عنوان مثال : short-comments.php) و آن را با استفاده از تابع comments_tempalte(“/short-comments.php”) نمایش دهید.
جلسه آموزش فایل قالب Comment نیز به پایان رسید.
در جلسه بعد به آموزش استفاده از صفحات ۴۰۴ در وردپرس خواهیم پرداخت.