Advertisement
certainlyakey

Posts 2 Posts 1.4.1 each_connected bug

Sep 6th, 2012
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. //Functions.php
  3. function connect_posts2artists() {
  4.     // Make sure the Posts 2 Posts plugin is active.
  5.     if ( !function_exists( 'p2p_register_connection_type' ) )
  6.         return;
  7.  
  8.     p2p_register_connection_type( array(
  9.         'name' => 'posts_to_artists',
  10.         'from' => 'post',
  11.         'to' => 'artist'
  12.     ) );
  13. }
  14. add_action( 'wp_loaded', 'connect_posts2artists' );
  15. ?>
  16.  
  17. <?php
  18. //Template file
  19.         $recentPosts = new WP_Query(array('showposts' => 5, 'post_type' => array('artist')));
  20.         p2p_type( 'posts_to_artists' )->each_connected( $recentPosts );
  21.             while( $recentPosts->have_posts() ) : $recentPosts->the_post(); ?>
  22. //...
  23. <?
  24.         // Display connected posts
  25.         if ($post->connected) {echo '<ul>';}
  26.         else {echo '<p>This artist has got no connected news items yet</p>';};
  27.         foreach ( $post->connected as $post ) : setup_postdata( $post );
  28. ?>
  29.         <li><a href="<? the_permalink(); ?>"><? the_title(); ?></a></li>       
  30. <?      endforeach;
  31.         if ($post->connected) {echo '</ul>';};
  32.         wp_reset_postdata(); // set $post back to original post
  33. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement