Method 2
- From the WordPress admin menu, select Genesis > Theme Settings.
- Scroll down to the Comments and Trackbacks section and click Enable comments on pages.
- The previous step turns on comments for all pages. Now we will turn comments off for all pages except for specific pages; in this case, the page with ID 58. Add the following code to your Genesis theme’s functions.php file.
// Remove page comments except for specific pages add_action( 'genesis_before', 'wnd_remove_comments' ); function wnd_remove_comments() { if ( is_page() && (!is_page(58))) { remove_action( 'genesis_after_entry', 'genesis_get_comments_template' ); } }
Note: The code above is for Genesis 2.0. If you are using a Genesis 1.x template, change genesis_after_entry to genesis_after_post
Also Note: the code is for one page only: page ID = 58. If you want to add the comments form to several pages, replace (!is_page(58))) with (!is_page(array(42, 56, 85))) where the page IDs are 42, 56, and 85. To find the page ID, from WordPress admin menu, click Pages and then put your cursor over the page title link to view the ID. In the image below, the browser shows the post ID of 2002 when I put my cursor over page title.
- Test your pages. You should have comments form only on pages whose IDs you specified in the code. No other pages should have the comments form.
Pages: Page 1 Page 2
Mark says
Thanks for the insight on using the ‘genesis_after_entry’ hook, I was trying to use the ‘genesis_comments’ hook but that’s obviously executed to late.
However you are using the wp_enqueue_scripts hook to remove the action, that hook should only be used to load scripts and stylesheets; you should use the ‘wp’ hook for these kind of functions, so your code would be:
// Remove page comments except for specific pages
add_action( ‘wp’, ‘wnd_remove_comments’ );
function wnd_remove_comments() {
if ( is_page() && (!is_page(58))) {
remove_action( ‘genesis_after_entry’, ‘genesis_get_comments_template’ );
}
}
Pat Fortino says
Hi Mark,
Thanks for the comment. You are correct the
wp
hook works. Also, I figured out thatgenesis_before
works too.and says
You can also use a page template and a
genesis_get_option
filter, like this:<?php
/*
Template Name: Page With Comments
*/
add_filter( 'genesis_pre_get_option_trackbacks_pages', 'custom_page_with_comments' );
add_filter( 'genesis_pre_get_option_comments_pages', 'custom_page_with_comments' );
function custom_page_with_comments() {
return true;
}
genesis();
Pat Fortino says
Thanks. I’ll give that a try. Looks like a more simple solution.