Skip to content

Instantly share code, notes, and snippets.

Created December 31, 2012 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4419975 to your computer and use it in GitHub Desktop.
Save anonymous/4419975 to your computer and use it in GitHub Desktop.
/** Register connection type (required for Posts 2 Posts plugin
https://github.com/scribu/wp-posts-to-posts/wiki/Basic-usage */
function my_connection_types() {
p2p_register_connection_type( array(
'name' => 'classes_to_events',
'from' => 'class',
'to' => 'event',
'cardinality' => 'one-to-many'
) );
}
add_action( 'p2p_init', 'my_connection_types' );
/** Modify output for connections
https://github.com/scribu/wp-posts-to-posts/wiki/Actions-and-filters */
add_filter( 'p2p_widget_html', 'my_p2p_template_handling', 10, 4 );
add_filter( 'p2p_shortcode_html', 'my_p2p_template_handling', 10, 4 );
function my_p2p_template_handling( $html, $connected, $ctype, $mode ) {
$template = locate_template( "p2p-{$mode}-{$ctype->name}.php" );
if ( !$template )
return $html;
ob_start();
$_post = $GLOBALS['post'];
foreach ( $connected->items as $item ) {
$GLOBALS['post'] = $item;
load_template( $template, false );
}
$GLOBALS['post'] = $_post;
return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment