【问题标题】:How to display the comments_template with a shortcode, in Wordpress?如何在 Wordpress 中使用简码显示 comments_template?
【发布时间】:2015-02-18 21:05:27
【问题描述】:

我设法使用以下代码显示带有简码的comment_form(同时将其从默认位置删除):

`add_shortcode( 'wpse_comment_form', function( $atts = array(), $content = '' )
{
    if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
    {
        ob_start();
        comment_form();
        print(  '<style>.no-comments { display: none; }</style>' );
        add_filter( 'comments_open', '__return_false' );
        return ob_get_clean();
    }
    return '';
}, 10, 2 );

birgire 在此答案中建议的代码:https://wordpress.stackexchange.com/a/177289/26350

为了更清楚,这里是我想要得到的地方:我需要通过简码在不同的位置显示 cmets 表单和 cmets 列表。我设法模仿上面相同的代码来显示 cmets_template (后来编辑 cmets.php 以从中删除 comment_form,因为我真正需要显示的是 cmets 列表)但 cmets 列表显示 2x,一个在短代码中位置以及帖子底部(默认位置)。我尝试使用相同的代码独立显示 wp_list_cmets 但没有成功。

【问题讨论】:

    标签: php wordpress comments shortcode


    【解决方案1】:

    如果您不提供 cmets wo wp_list_cmets 的数组,则它希望找到已经完成的查询(即在循环中)。

    如果你想独立于循环显示帖子的 cmets,你可以使用类似的东西:

        $comments = get_comments( array( 'post_id' => $id ) );
        wp_list_comments( array( 'per_page' => how many to show ), $comments);
    

    因此,要将其放入短代码中,您可以执行类似(未测试)的操作:

    add_shortcode( 'wpse_comment_list', function( $atts = array(), $content = '' )
    {
        if( isset($atts['id']) && post_type_supports( $atts['id'], 'comments' ) )
        {
            ob_start();
            $comments = get_comments( array( 'post_id' => $atts['id'] ) );
            wp_list_comments( array( 'per_page' => how many to show ), $comments);
            return ob_get_clean();
        }
        return '';
    }, 10, 2 );
    

    你可以用[wpse_comment_list id="33"](或者任何id)调用它。

    【讨论】:

    • 您提供的代码在哪里适合?在简码功能中?我需要将其作为简码,以便可以将其放置在帖子的预定义部分中..
    • 我在我需要的地方显示了简码,并删除了对我不需要的文件中函数的调用,因为我不知道如何在简码函数中过滤它们。如果有人可以告诉我这种方法是否不那么可取,甚至可能对网站的运行有害,我愿意深入挖掘更好的解决方案。很高兴你回来详细说明你的建议。它使它更容易理解。
    【解决方案2】:

    我设法获得了我需要的结果,但部分是通过在我的子主题上删除我无法通过代码删除的部分。 我模仿了用于简码显示comment_form(在问题上方)的代码来简码显示cmets_template(在下面)。它在我需要的地方显示它,但它没有像前面的代码为 comment_form 所做的那样从帖子底部删除它的“幽灵”。所以我将 single.php 复制到我的子主题并删除了所有这些:

    <?php
    `// If comments are open or we have at least one, load up
        the comment template
        if ( comments_open() || '0' != get_comments_number() )
        comments_template( '', true );
    ?>
    

    成功了。虽然不确定这是最好的方法,但我很好奇。我不再需要帖子底部的 cmets;我的评论表单现在是一个不同的表单,并且有不同的位置,以防万一这是唯一的问题。

    这是我用来简码显示 cmets_template 的代码。我需要它只显示 cmets 而不是表单,所以我从子主题的 cmets.php 中删除了 cmets_form 调用。

    add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' )
    {
        if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
        {
            ob_start();
            comments_template();
            print(  '<style>#comments-title { display: none; }</style>' );
            return ob_get_clean();
        }
        return '';
    }, 10, 2 ); 
    

    【讨论】:

      【解决方案3】:

      如果您在主题中此部分之前运行 [wpse_comments_template] 短代码:

      <?php
          if ( comments_open() || '0' != get_comments_number() )
              comments_template( '', true );
      ?>
      

      然后它正在尝试使用:

      add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' )
      {
          if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
          {
              ob_start();
              comments_template();
              add_filter( 'comments_open',         '__return_false'             );
              add_filter( 'get_comments_number',   '__return_zero'              );
              return ob_get_clean();
          }
          return '';
      }, 10, 2 );
      

      但这可能会干扰稍后出现的与评论相关的内容,例如侧边栏中的内容。

      所以使用起来会更准确:

      /**     
       * Display the comment template with the [wpse_comments_template] 
       * shortcode on singular pages. 
       *
       * @see http://stackoverflow.com/a/28644134/2078474
       */
       add_shortcode( 'wpse_comments_template', function( $atts = array(), $content = '' )
       {
          if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
          {
              ob_start();
              comments_template();
              add_filter( 'comments_open',       'wpse_comments_open'   );
              add_filter( 'get_comments_number', 'wpse_comments_number' );
              return ob_get_clean();
          }
          return '';
      }, 10, 2 );
      
      function wpse_comments_open( $open )
      {
          remove_filter( current_filter(), __FUNCTION__ );
          return false;
      }
      
      function wpse_comments_number( $open )
      {
          remove_filter( current_filter(), __FUNCTION__ );
          return 0;
      }
      

      而且您不必从您的主题中删除任何代码。

      我在 25 岁主题上对此进行了测试,它似乎按预期工作。

      【讨论】:

      • “更准确”的方式在这里也适用,带有病毒式主题。但这并没有给我我需要的结果。 1) cmets_template 显示 cmets 及其下方的 comment_form (不在页面底部;该部分运行良好),我自己需要 cmets。 2) [wpse_comment_form] 未在我放置表单的同一页面中显示表单。但它适用于不同的页面,我将其用作提交内容的一种方式。我需要
      • 我没有到达那里的知识。我相信我需要回到 codecademy 一段时间。因此,目前,我正在考虑回到使用您在此处提供的简码函数 @birgire 并从 cmets_template (cmets.php) 中删除 comment_form 调用的策略。我开始了另一个主题/问题,关于删除函数调用或通过functions.php文件过滤它们之间是否存在缺点。还没有回复。
      • add_filter( 'comments_open', 'wpse_comments_open' );我删除了这个过滤器,以便显示comment_form的简码可以在同一页面上工作(所以这似乎是原因),所以我仍然可以在删除时使用上面的功能cmets.php中的comment_form
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 2012-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多