Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Details: https://wordpress.stackexchange.com/questions/318747/inserting-content-of-1-post-to-in-another-with-a-template-hierarchy/318753#318753
- /* Add the function to the theme's functions.php file, without these comments..
- And here's how you can use it from your template file:
- <div id="sample-id" class="sample-class">
- <?php
- global $post;
- $post_slug = 'landing-faq-' . $post->post_name;
- echo get_faq_content( $post_slug );
- ?>
- </div>
- If a custom FAQ content is found, it will be used; else, the default one is used.
- */
- function get_faq_content( $post_slug ) {
- $default_slug = 'landing-faq'; // Slug of the default post. Set this or the ID.
- $default_id = 0; // ID of the default post. Set this or the slug.
- $content = '';
- global $wpdb;
- $row = $wpdb->get_row( $wpdb->prepare( "
- SELECT ID, post_content FROM {$wpdb->posts}
- WHERE post_name = %s AND post_type = 'land'
- ", $post_slug ) );
- if ( $row ) {
- $content = $row->post_content;
- } elseif ( $default_id ) {
- $content = get_post_field( 'post_content', $default_id );
- } elseif ( $default_slug && $post_slug != $default_slug ) {
- return get_faq_content( $default_slug );
- }
- if ( $content ) {
- $content = apply_filters( 'the_content', $content );
- $content = str_replace( ']]>', ']]>', $content );
- }
- return $content;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement