web-dev-qa-db-ja.com

WP_Queryで表示したときにコメントを投稿を隠す

[blog posts_per_page-"3"]を書いたときに私のサイトのホームページに 'x'個の投稿を表示するショートコードを書きました。ここにあります:

function getBlogPosts($atts) {

  extract(shortcode_atts(array(
    "posts_per_page" => '',
  ), $atts));

  $queryArgs = array(
    "posts_per_page" => $posts_per_page
  );

  $queryPosts = new WP_Query($queryArgs);
  $output = "";

  $output .= "<ul class='articles-list clearfix'>";

    if ( $queryPosts->have_posts() ) : while ( $queryPosts->have_posts() ) : $queryPosts->the_post();
      $output .= "<li class='clearfix ind-article'>
        <a class='article-container' href='". get_the_permalink() ."'>";
        if (has_post_thumbnail($post->ID)) {
            $output .= get_the_post_thumbnail($post->ID, 'medium'); // medium img size = 300x300. => make sure photos uploaded are at least 600x600
        }
        $output .= "<div class='article-content'>";
          $output .= "<h3 class='entry-title gamma uppercase'>". get_the_title() ."</h3>";
          $output .= "<span class='entry-meta'>". get_the_date('l jS F') ."</span>";
      $output .= "</div></a></li>";
    endwhile; endif;

  $output .= "</ul>";

  return $output;
} add_shortcode('blog', 'getBlogPosts');

ただし、これを実行すると、ブログ投稿のコメントフォームも表示されます(これは間違いなくページのコメントフォームであり、最後に表示されたブログ投稿では1つのコメントフォームにしか見えない)。私はコメントを有効にしておきたいが、ショートコードが使用されたときからコメントを削除したい。どのようにこれをしますか?

2
Tom Oakley

問題

グローバルな$postオブジェクトを復元するために、whileループの後に、コア関数wp_reset_postdata()を呼び出すことを忘れないでください。コメントフォームはそのオブジェクトに依存しています。あなたは$queryPosts->the_post()呼び出しでオーバーライドします。

extract()は推奨されていないことに注意してください、例えば@toschoで this answer をチェックしてください。

コメントを削除する

コメントフォームを削除するには、ショートコードを使うときに、私の 答えをここにチェックしてください

ショートコードの後に​​コメントの一覧コメントフォームを削除したい場合は、以下の方法のうちの1つを試してください。ショートコードのコールバック

方法#1フィルタを使って問い合わせたコメントを削除する:

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
{
    // Remove the comment form
    add_filter( 'comments_open', '__return_false' ); 

    // Remove the list of comments
    add_filter( 'comments_array', '__return_empty_array' );
}   

方法#2get_comments()に空の配列を返させる。

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
{
    // Remove the comment form
    add_filter( 'comments_open', '__return_false' );

    // Empty comments SQL query - Run only once
    add_filter( 'comments_clauses', function( $clauses )
    {
        static $count = 0;
        if( 0 === $count++ )
            $clauses['where'] .= ' AND 1=0 ';
        return $clauses;
     });
}   

これを防ぐために、ここではフィルタコールバックを一度だけ実行します。サイドバーの最近のコメントウィジェット。

方法#3filterでコメントテンプレートを修正する。テーマ内に空のファイルcomments-empty.phpを作成して、

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
{
    // Modify the comment template
    add_filter( 'comments_template', function( $template )   
    {
        if( $tmp = locate_template( 'comments-empty.php' ) )
            $template = $tmp;
        return $template;
    } );
}   

必要に応じてファイルパスを変更できます。

方法#4CSSで隠します。例:

if( is_singular() && post_type_supports( get_post_type(), 'comments' ) )
{
    print '<style> #comments { display: none } </style>';
}
1
birgire