web-dev-qa-db-ja.com

配列が存在する場合はコメントを無効にする

これはrewrite_endpoints()の関数を登録しているコードです。

function wpa121567_rewrite_endpoints(){

add_rewrite_endpoint( 'comments', EP_PERMALINK );

add_rewrite_endpoint( 'stats', EP_PERMALINK );

}

add_action( 'init', 'wpa121567_rewrite_endpoints' );

NOTEこれが問題になるかどうかわかりませんが、それを書きます。

私は/ inc/post-format/single-CPT.phpを呼び出しているsingle-CPT.phpを持っています。

    <?php while ( have_posts() ) : the_post(); ?>

<?php 

get_template_part( 'inc/post-format/single-debate'); 
    ...

これは、配列キーが存在するかどうかをチェックするコードです。これは、my/ inc/post-format/single-cpt.php

?php
if( array_key_exists( 'comments', $wp_query->query_vars ) ){
 the_field('comments_section'); 

} elseif( array_key_exists( 'stats', $wp_query->query_vars ) ) {

 the_field('stats_section'); 
 } else {
// the request is for the main post
 }
  ?>

これは私のcomments.phpです:

     <div id="comments" class="comments-area">
<?php
// If CPT and not logged in, display a message:
if ( 'debate' == get_post_type() && ! is_user_logged_in() ) {
echo '<p class="must-log-in" style="padding-left:20px; font-size:20px;">You must be logged in to post a comment.' . '</p>';

echo do_shortcode("[upme_login]");

 }
?>
    <?php if ( have_comments() ) : ?>
        <h3 class="comments-title">

            <?php
                printf( _n('%d comment', '%d comments', get_comments_number(), 'outbox' ),
                    number_format_i18n( get_comments_number() ) );
            ?>
        </h3>




        <ol class="commentlist">

            <?php
                wp_list_comments( array( 'callback' => 'outbox_comment' ) );
            ?>
        </ol><!-- .commentlist -->

        <?php if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) : // are there comments to navigate through? If so, show navigation ?>
        <nav role="navigation" id="comment-nav-below" class="site-navigation comment-navigation clearfix">
            <div class="nav-previous"><i class="icon-left-open-1"></i>&nbsp;<?php echo get_previous_comments_link( __( 'Older Comments', 'outbox' ) ); ?></div>
            <div class="nav-next"><?php echo get_next_comments_link( __( 'Newer Comments', 'outbox' ) ); ?>&nbsp;<i class="icon-right-open-1"></i></div>
        </nav><!-- #comment-nav-below .site-navigation .comment-navigation -->
        <?php endif; ?>

    <?php endif; // have_comments() ?>

    <?php
        // If comments are closed and there are comments, let's leave a little note, shall we?
        if ( ! comments_open() && '0' != get_comments_number() && post_type_supports( get_post_type(), 'comments' ) ) :
    ?>
        <p class="nocomments"><?php _e( 'Comments are closed.', 'outbox' ); ?></p>
    <?php endif; ?>

   <?php
 // Don't output the comment form if CPT and user isn't logged in
if ( 'debate' != get_post_type() || is_user_logged_in() ) {
 comment_form();
}

?>

</div><!-- #comments .comments-area -->
<?php } ?> 

これが、コメントテンプレートがmysingle-CPT.phpの中に含まれる方法です。

if ( comments_open()  )

comments_template( '', true );

if( array_key_exists( 'comments', $wp_query->query_vars ) ){の場合はコメントを無効にしたい

これどうやってするの?

1
agis

コメント返信フォームを出力するために comment_form() を適切に使用していると仮定します。

if( ! array_key_exists( 'comments', $wp_query->query_vars ) ) {
    // Output the comment form
    comment_form();
}

編集する

あなたのcomments.phpコードに基づきます。

// If the user isn't logged in, don't display comment form
if ( is_user_logged_in() ) {
    global $wp_query;
    // If the post type isn't 'debate',
    // Or if the 'comments' array key exists in the query
    // Display the comment form
    if ( 'debate' != get_post_type() || ! array_key_exists( 'comments', $wp_query->query_vars ) ) {
        comment_form();
    }
}

コメントテンプレートがテンプレートにどのように含まれているかは完全には明らかではないため、ここでは完全に暗闇の中で突き刺しています。

編集2

これが意図したとおりに機能するとは思わない。

if ( comments_open()  )

comments_template( '', true );

その余分な改行を見ますか?あなたのコンディショナルの周りに中括弧がなければ、2つの行は関係ありません。次のいずれかを実行する必要があります。

if ( comments_open() )
    comments_template( '', true );

または、もっと良いことに:

if ( comments_open() ) {
    comments_template( '', true );
}
1
Chip Bennett