• javiecastillo

    (@javiecastillo)


    Hi all,

    Hopefully someone can help me out with this —

    I’m trying to add comments to the taxonomy page. Currently, immediately following the loop, I have added this code:

    <?php the_content(__('(more...)')); ?>
    <?php comments_template('', true); ?>

    Based on what I found on this thread and this codex reference.

    It doesn’t seem to be working. Here is page on the site that I am working on.

    I have custom post types (products) displayed by a custom taxonomy (product categories).

    Do comments even work on dynamically generated taxonomy pages? Or would I have to create custom page templates or something? My client needs to be able to create pages on the fly, and I figured using the taxonomy.php template would work best…

    Thanks in advance for the help!

Viewing 5 replies - 1 through 5 (of 5 total)
  • johngorenfeld

    (@johngorenfeld)

    I just found this page, searching for an answer to the exact same question. I hope this bumps it up.

    I know it’s possible to leave comments underneath category sections, so there must be a way to make it work the same for the similar Taxonomy.php. I wonder if there is some way to signal that the taxonomy should support comments, analogous to how you would with a custom post type?

    I was looking to do something similar. I haven’t solved it yet, but I did do some digging into the comments table. It appears that each comment is bound to an object in the wp_posts table. So I can see how comments can be added to any post inside a loop (whether its on a single page of not), but I can’t see how comments can be added when outside the loop — unless a specific post_id is supplied to bind them to.

    Theoretically it would be possible to create a hidden post type which mirrored the taxonomy terms. Any comments on a dynamic term page would then be bound to the equivalent instance of the hidden post type. It’s not something I’ve tried yet; I’m still hoping for an easier solution to present itself.

    Moderator keesiemeijer

    (@keesiemeijer)

    comments_template(); needs the global $post object from a single Post or Page. The post object is not transferred when calling this function from another loop.
    Try setting $withcomments to true inside a loop:

    <?php
    global $withcomments;
    $withcomments = true;
    comments_template();
    ?>

    Or try it with an include (inside the loop):

    <?php include('comments.php'); ?>

    When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.

    https://php.net/manual/en/function.include.php

    I think I may have something. Following my idea above and keesiemeijer’s point about the comments_template needing a $post object. The methodology is

    1. Create a new hidden custom post-type to which the comments are bound.
    2. Provide for basic post instance creation.
    3. By default the comments for will loop back to the post-types default url and not the referring page. Therefore we need to provide filters for get_comments_link.
    4. A wp_rewrite rule addition is needed to deal with paged comments
    5. Minimal wrapper to comments_template

    The key is creating some sort of deterministic link between a single term and a single post. The following is a proof of concept.

    define( 'CAW_TYPE', 'caw_stubb' );
    
    //ACTIONS
    add_action( 'init', 'caw_init', 100);
    
    //FILTERS
    add_filter( 'get_comment_link', 'caw_comment_link', 1, 3 );
    
    //INIT Function -- setup post type and wp_rewrite riles
    function caw_init() {
    
    	$singular = 'Comments Stub';
    	$plural = 'Comments Stubs';
    
    	$args = array(
    		'label' => $plural,
    		'labels' => array( 'name' => $plural, 'singular_name' => $singular, 'add_new' => 'Add '.$singular, 'add_new_item' => 'Add New '.$singular, 'edit_item' => 'Edit '.$singular, 'new_item' => 'New '.$singular, 'view_item' => 'View '.$singular, 'search_items' => 'Search '.$plural, 'not_found' => 'No '.$singular.' found', 'not_found_in_trash' => 'No '.$singular.' found in Trash', 'parent_item_colon' =>  $singular.': ' ),
    		'description' => 'Hidden post type used to bind any comment to',
    		'public' => false,
    		'capability_type' => 'post',
    		'supports' => array( 'title', 'trackbacks', 'custom-fields', 'comments' ),
    		'query_var' => false
    		);
    
    	register_post_type( CAW_TYPE, $args );
    
    	caw_add_rewrite_rules($rules);
    }
    
    //ADD wp_rewrite rules for all public taxonomies
    function caw_add_rewrite_rules($rules) {
    	$taxes = get_taxonomies( array(), 'objects');
    	foreach ( $taxes as $tax ) {
    		if ( $tax->public ) {
    			add_rewrite_rule(
    				$tax->rewrite['slug'].'/([^/]+)/comment-page-([0-9]{1,})/?$',
    				'index.php?'.$tax->query_var.'=$matches[1]&cpage=$matches[2]',
    				'top');
    		}
    	}
    	flush_rewrite_rules();
    }
    
    //FILTER get_comments_link to redirect from the hidden post types uri to the uri
    //uri of the dynamically generated term page.
    function caw_comment_link($link,$comment,$var) {
    	global $post;
    
    	if ( get_post_type() == CAW_TYPE ) {
    		$meta = get_post_custom();
    		$postlink = get_permalink($comment_post_ID);
    		$termlink = get_term_link( intval($meta['caw_term_id'][0]), $meta['caw_taxonomy'][0] );
    		$link = str_replace( $postlink, $termlink, $link );
    	}
    	return $link;
    }
    
    //RETURN the hidden post associated with a specific term object. Optionally
    //create that post if it does not already exist
    function caw_getstubb_term($term, $create=true) {
    	$oldposts = get_posts( array( 'name' => $term->taxonomy.'-'.$term->term_id, 'post_type' => CAW_TYPE ));
    	if ( count($oldposts) == 1 ) {
    		return $oldposts[0];
    	} elseif ( $create) {
    		$newpostid = caw_addstubb_term($term);
    		if ( !is_wp_error($newpost) ) {
    			return get_post($newpost);
    		} else {
    			return false;
    		}
    	} else {
    		return false;
    	}
    }
    
    //CREATE hiiden post associated with a specific term object. Record the parent term using
    //the post custom fields
    function caw_addstubb_term($term) {
    
    	$newpost = wp_insert_post(
    		array(
    			'post_title' => $term->name,
    			'post_name' => $term->taxonomy.'-'.$term->term_id,
    			'post_status' => 'publish',
    			'post_type' => CAW_TYPE
    			)
    		);
    
    	add_post_meta($newpost->ID, 'caw_taxonomy', $term->taxonomy, true);
    	add_post_meta($newpost->ID, 'caw_term_id', $term->term_id, true);
    
    	return $newpost;
    }
    
    //DISPLAY COMMENTS function. Retrieves hidden post & runs comments_template.
    // $file and $seperate are passed directly to comments_template.
    function caw_comments_template($term, $file='', $seperate=true, $create=true) {
    	global $post, $withcomments;
    	if ( isset($post) ) $oldpost = $post;
    	$post = caw_getstubb_term($term, $create);
    	$withcomments = "1";
    	comments_template( $file, $seperate);
    	if ( isset($oldpost) ) $post = $oldpost;
    }

    This is still very generic as it adds the possibility of comments on all public taxonomies. Ideally the creation of the hidden posts would be hooked into the creation/deletion of the original terms. However, I had to do this for a site redesign where those terms were already in
    existence.

    Thanks a ton, jmkprime! Awesome. I made a few modifications, because I ran into some errors and the url rewrite didn’t work for me, but now it works a treat.

    The reason I renamed ‘caw’ to ‘taxcom’ is for no other reason than that ‘caw’ didn’t mean anything to me.

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Add comments functionality to taxonomy.php’ is closed to new replies.