SHARE
TWEET

(WordPress) get_faq_content()

5ally Nov 9th, 2018 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. // Details: https://wordpress.stackexchange.com/questions/318747/inserting-content-of-1-post-to-in-another-with-a-template-hierarchy/318753#318753
  3.  
  4. /* Add the function to the theme's functions.php file, without these comments..
  5.  
  6. And here's how you can use it from your template file:
  7.  
  8. <div id="sample-id" class="sample-class">
  9.     <?php
  10.         global $post;
  11.         $post_slug = 'landing-faq-' . $post->post_name;
  12.         echo get_faq_content( $post_slug );
  13.     ?>
  14. </div>
  15.  
  16. If a custom FAQ content is found, it will be used; else, the default one is used.
  17.  */
  18. function get_faq_content( $post_slug ) {
  19.     $default_slug = 'landing-faq'; // Slug of the default post. Set this or the ID.
  20.     $default_id = 0;               // ID of the default post. Set this or the slug.
  21.     $content = '';
  22.  
  23.     global $wpdb;
  24.  
  25.     $row = $wpdb->get_row( $wpdb->prepare( "
  26.         SELECT ID, post_content FROM {$wpdb->posts}
  27.         WHERE post_name = %s AND post_type = 'land'
  28.     ", $post_slug ) );
  29.  
  30.     if ( $row ) {
  31.         $content = $row->post_content;
  32.     } elseif ( $default_id ) {
  33.         $content = get_post_field( 'post_content', $default_id );
  34.     } elseif ( $default_slug && $post_slug != $default_slug ) {
  35.         return get_faq_content( $default_slug );
  36.     }
  37.  
  38.     if ( $content ) {
  39.         $content = apply_filters( 'the_content', $content );
  40.         $content = str_replace( ']]>', ']]&gt;', $content );
  41.     }
  42.  
  43.     return $content;
  44. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top