• In a blog (like a photoblog) you could very well make use of category pages to show corresponding objects. Now some of these posts may have individual comments. On the other hand you could want to comment the category itself.
    Since a category isn’t a post and so can’t have comments, you could make the “headline” a post for instance…

    Now for my blog I would like to have such “category pages” with

    • the loop showing the objects
    • a second loop showing all the comments
    • a comment form where submission would not leave this (category) page

    Since WP will only show comments and the comment form on single pages, this requires quite a lot of redirects…

    Is there a plugin already which could help?

    Concerning WordPress: Are category pages a good base for establishing such pages?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter schoe

    (@schoe)

    Since there are quite a lot of users struggling with (or against?) similar problems… and very few answers up to now, I try to share the results I found which seem to work (for me at least):

    Associated with every category I have a (empty) post titled “my category title” which belongs to this category and to a “title_cat”

    After the main loop of the category template I put wp_reset_postdata() to start a second loop:

    $query = new WP_Query( array( 'category__and' => array( $cat_id, $title_cat_id) ) );
    
    global $withcomments;
    $withcomments = 1;
    
    comments_template();
    
    wp_reset_postdata();

    We now have a category page showing the “category comments” and the “category comment form”.

    When we submit a comment by this form, WP realizes that it ( is_single and ) belongs to the title_cat post. So WP tries to leave this category page, but we want to keep it…

    Now the action hook ‘template_redirect’ seems to be to late…
    …but the ‘get_comment_link’ filter works. So in our functions.php we put:

    add_filter('get_comment_link', 'ms_get_comment_link', 10, 3 );
    
    function ms_get_comment_link($link, $comment, $args) {
    	$cats = get_the_category();
    	foreach ($cats as $cat) {
    		if ($cat->term_id == $title_cat_id) $title_cat = true;
    		else $category = $cat;
    	}
    	$cat_ID = $category->term_id;
    
    	if ($title_cat) {
    		$category_link = get_category_link( $cat_ID );
    		return $category_link . "#comment";
    	}
    	else return $link;
    }

    Up to now the category page will only show the “category page comments”.
    If we want to show the comments of all posts within the main category another loop is needed…

    If there is a better (simpler) solution please let me know. Thanks!

    Thread Starter schoe

    (@schoe)

    Sorry, I forgot that in the category template there must(?) be a (maybe empty) loop after the new WP_Query:

    $query = new WP_Query( array( 'category__and' => array( $cat_id, $title_cat_id) ) );
    
    global $withcomments;
    $withcomments = 1;
    
    while ($query->have_posts()) : $query->the_post();
    
    endwhile;
    
    comments_template();
    wp_reset_postdata();

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘handling comments on category pages?’ is closed to new replies.