Advertisement
FrancoisL

Post to Post Shortcode (plugin wordpress)

Apr 12th, 2012
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.96 KB | None | 0 0
  1. `
  2. <?php
  3. /**
  4. * Plugin Name: Display Posts to Posts
  5.  
  6. // Create the shortcode
  7. add_shortcode(‘p2p’, ‘display_post2post’);
  8.  
  9. function display_post2post($atts) {
  10.  
  11. extract( shortcode_atts( array(
  12. ‘relation_post’ =>  ”,  //posts_to_pages or relationship targeted
  13.                         // other attributes to pass?
  14. ), $atts ) );
  15.  
  16. $args = array(
  17. ‘relation_post’ => $connected_type,
  18. ‘connected_type’ => $connected_type,
  19. ‘connected_items’ => get_queried_object_id(),
  20. ‘nopaging’ => true,
  21. // other filters ?
  22. );
  23.  
  24. $return = ";
  25. $connected = new WP_Query($args);
  26. if ( $connected->have_posts() ):
  27. $return .= "<h3>Related posts:</h3>;
  28. $return .= "
  29. <ul>";
  30. while ( $connected->have_posts() ): $connected->the_post();
  31. $return .= "
  32. <li><a>"><?php the_title(); ?></a></li>
  33. ";
  34. endwhile;
  35. $return .= "</ul>
  36. ";
  37.  
  38. // Prevent weirdness
  39. wp_reset_postdata();
  40. $return .= ”;
  41. endif; wp_reset_query();
  42.  
  43. if (!empty($return)) return $return;
  44. }
  45. ?>
  46. `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement