Forum Replies Created

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

    (@dlukez)

    So you want to display writers connected to the article, then blurbs connected to both that article AND each writer who is also connected to that article?

    A short description of what each content type and relationship means, then why you want to display them on the article might help. e.g. “a blurb is an excerpt for one article” (blurb->article).

    I think p2p can do what you want, it’s more a matter of working out the best way to fit things together.

    Thread Starter dlukez

    (@dlukez)

    ok, thanks!

    dlukez

    (@dlukez)

    Yeah I find each_connected() confusing in some cases. Could you clarify the relationships between Blurb, Writer and Article? I don’t really understand how your connections are set up so I probably have mine set up differently!

    dlukez

    (@dlukez)

    Hi Arlo. Try this in single-article.php:

    // get writers connected to blurbs connected to this article
    $writers = p2p_type( 'blurb_to_writer' )->set_direction( 'to' )->get_related( $post );
    
    // get blurbs connected to this article (by way of being connected to a related writer so that we can use p2p_distribute_connected to merge the arrays)
    $blurbs = p2p_type( 'blurb_to_writer' )->set_direction( 'from' )->get_related( $post );
    
    // match the blurbs with the writers and store in each writer
    p2p_distribute_connected( $writers->posts, $blurbs->posts, 'blurbs' );
    
    echo '<ul>';
    foreach( $writers->posts as $writer ):
    	echo '<li>';
    	echo '<a href="' . get_permalink($writer) . '">' . get_the_title($writer) . '</a>'; // display the title of this writer (with a permalink)
    	p2p_list_posts( $writer->blurbs ); // list blurbs for this writer
    	echo '</li>';
    endforeach;
    echo '</ul>';

    This should display writers connected to blurbs that are connected to this article. It ignores the writer to article relationship since I assumed that would be for specifying the author or writers the article talks about, or something ultimately unrelated to blurbs.

    dlukez

    (@dlukez)

    Hi, I managed to get something like this to work with the following:

    p2p_register_connection_type( array(
    		'name' => 'parent_child',
    		'from' => 'person',
    		'to' => 'person',
    		'reciprocal' => false,
    		'title' => array(
    			'from' => 'Parents',
    			'to' => 'Children'
    			)
    		)
    	);

    This will create a connection between only parents and children, but by filling out each link in the chain you allow grandchildren, grand-grand-children, grand-grand-grand-children… and so on. You can then walk through these connections when displaying them. E.g you could put something like this in single.php:

    //get parents and children
    $children = p2p_type('parent_child')->set_direction('to')->get_connected( $post );
    $parents = p2p_type('parent_child')->get_connected( $post );
    
    if( $parents->have_posts() ):
    	echo 'Parents';
    	p2p_list_posts($parents);
    endif;
    
    if( $children->have_posts() ):
    	echo 'Children and grandchildren:';
    	if( $children->have_posts() ):
    		foreach( $children->posts as $child ):
    			$permalink = get_permalink( $child );
    			$name = get_the_title( $child );
    
    			echo "<li><a href='$permalink'>$name</a>";
    
    			// get grandchildren for this child
    			$grandchildren = p2p_type('parent_child')->set_direction('to')->get_connected( $child );
    
    			// display grandchildren
    			if( $grandchildren->have_posts() ):
    				p2p_list_posts($grandchildren);
    			endif;
    
    			echo "</li>";
    
    		endforeach;
    	endif;
    endif;

    You could keep looping through children for as many times as you like, although there should be a more efficient way.

    I tried to get something like that to work with each_connected(), but couldn’t get it to work.

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