Created
April 15, 2015 20:44
-
-
Save davidperezgar/005e650e1d28018ce4b2 to your computer and use it in GitHub Desktop.
Posts 2 posts functions for WordPress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//In functions .php | |
function my_connection_types() { | |
p2p_register_connection_type( array( | |
'name' => 'posts_to_pages', | |
'from' => 'post', | |
'to' => 'page' | |
) ); | |
} | |
add_action( 'p2p_init', 'my_connection_types' ); | |
?> | |
<?php | |
// In Single.php | |
// Find connected pages | |
$connected = new WP_Query( array( | |
'connected_type' => 'posts_to_pages', | |
'connected_items' => get_queried_object(), | |
'nopaging' => true, | |
) ); | |
// Display connected pages | |
if ( $connected->have_posts() ) : | |
?> | |
<h3>Related pages:</h3> | |
<ul> | |
<?php while ( $connected->have_posts() ) : $connected->the_post(); ?> | |
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> | |
<?php endwhile; ?> | |
</ul> | |
<?php | |
// Prevent weirdness | |
wp_reset_postdata(); | |
endif; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment