Nullyのぶろぐ

仙台から東京へ転勤したエンジニアのブログ

WordPressの固定ページでコメント投稿できないようにするのを簡単にしてみた

Simple ColorsさんのWordPressの固定ページで、コメント投稿をできないようにするという投稿をみてもうちょっと簡単にできそうだったので、やってみました!

私の場合は、テーマファイルの中で、is_page() 関数で現在閲覧しているページが固定ページかどうかで判別しています。

comments.phpを以下のようにすれば固定ページの場合は、コメント一覧もコメントフォームも表示されないようになります。

※ 今回はTwentyTenのテーマで代用しています。

<?php

/**

* The template for displaying Comments.

*

* The area of the page that contains both current comments

* and the comment form. The actual display of comments is

* handled by a callback to twentyten_comment which is

* located in the functions.php file.

*

* @package WordPress

* @subpackage Twenty_Ten

* @since Twenty Ten 1.0

*/

?>

<?php

// ↓これを追加

// ページの場合はコメントフィールド及びコメント自体を表示しない。

if(is_page()): return; endif; ?>

TwentyTenの場合は、comment_template()という関数で、個別化されたcomments.phpを呼び出している設計になっているので、これを書くだけで固定ページの場合はコメント投稿をできないようにないります。

また、以下のようにすることで、指定したページのみなどもできます。

<?php

/**

* The template for displaying Comments.

*

* The area of the page that contains both current comments

* and the comment form. The actual display of comments is

* handled by a callback to twentyten_comment which is

* located in the functions.php file.

*

* @package WordPress

* @subpackage Twenty_Ten

* @since Twenty Ten 1.0

*/

?>

<?php

// ページIDが10の場合はコメントフィールド及びコメント自体を表示しない。

if(is_page(10)): return; endif; ?>

的はずれな解答でしたらすみません...。