个性化的WordPress原创主题

WordPress文章企业网站主题模板
WordPress主题建站 > WordPress标签 > WordPress判断文档是否开启留言功能comments_open()

WordPress判断文档是否开启留言功能comments_open()

责任编辑:玲熙 更新时间: 关注:625

有的网站管理员会为网站添加评论功能,默认状态下文档评论开头是开启的;我们在WordPress管理后台文章发表/修改右侧工具条会看到讨论模块,其中有一项[允许评论],如果打开该开关,并且有配置评论功能,系统将允许用户留言。

在前端判断是否开启评论的WordPress标签:

  1. <?php comments_open(); ?>

实际应用中,根据实际测试及使用,当文档后台开启允许评论,会返回1,否则会返回假,例如:

  1. <?php
  2. $comments_open = comments_open();
  3. //$comments_open == 1
  4. if($comments_open)
  5. {
  6. echo 1;
  7. }
  8. ?>

WordPress允许管理员开启或关闭评论

comments_open()源代码:

  1. comments_open() 位于 /wp-includes/comment-template.php 行1239[5.9.3版本]
  1. /**
  2.  * Determines whether the current post is open for comments.
  3.  *
  4.  * For more information on this and similar theme functions, check out
  5.  * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/
  6.  * Conditional Tags} article in the Theme Developer Handbook.
  7.  *
  8.  * @since 1.5.0
  9.  *
  10.  * @param int|WP_Post $post_id Post ID or WP_Post object. Default current post.
  11.  * @return bool True if the comments are open.
  12.  */
  13. function comments_open( $post_id = null ) {

  14. $_post = get_post( $post_id );

  15. $post_id = $_post ? $_post->ID : 0;
  16. $open    = ( $_post && ( 'open' === $_post->comment_status ) );

  17. /**
  18.  * Filters whether the current post is open for comments.
  19.  *
  20.  * @since 2.5.0
  21.  *
  22.  * @param bool $open    Whether the current post is open for comments.
  23.  * @param int  $post_id The post ID.
  24.  */
  25. return apply_filters( 'comments_open', $open, $post_id );
  26. }

实际使用中使用方法:在模板中直接判断:

  1. <?php
  2. if(comments_open()){
  3. echo "WordPress评论开启";
  4. }
  5. ?>
  • 获赞48
  • 声明:内容版权归原作者所有,未经授权不得任意转载
  • 本文标题和链接:
    WordPress判断文档是否开启留言功能comments_open()
    https://e.69525.com/f/2637c27b1d4e0b3e/

相关阅读

Copyright © 2023 WordPress主题. All rights reserved.Powered by e.69525.com.

本站基于WordPress主题搭建,正在以新的版本流畅运行;由69525提供主题免费升级支持