$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp query - inserting content of 1 Post to in another with a template hierarchy|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp query - inserting content of 1 Post to in another with a template hierarchy

matteradmin8PV0评论

I have a page template I use for landing pages for various ad campaigns. The template uses custom fields for things like headlines and other shorter pieces of content within the page. However the template includes an entire section of FAQs. In many cases, the FAQs are the same across landing pages, but the requirement arose to have the ability to use a different set of FAQs (or different, structured/formatted content altogether) in that space if needed. I have built some logic into the template that will fetch the_content of a post containing a default set of FAQs and add it on to the_content of the landing page itself. Within this, I'm trying to add some additional logic that works similar to WP's template hierarchy... 'if you find a post named this use it, if not look for a post named that and use IT and if you don't find either, use this default'.

I'm basing it off of the slug of the landing page. So if the landing page's slug is holiday1 and there's a post with a slug of landing-faq-holiay1, it will be inserted into the landing page. And if not, the default post with a slug of landing-faq will be inserted.

I'm using the following code to insert the_content:

global $post;
$post_slug=$post->post_name;

$faq_id = get_page_by_path( $post_slug, OBJECT, 'land' )->ID;

$query = new WP_query( array('page_id' => $faq_id) );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        $content = apply_filters('the_content', get_the_content());
        ?>
        <div class="content">
            <?php echo $content; ?>
        </div>
        <?php
    endwhile;
endif;

This all works well and good (for the sake of simplicity, I left out the conditional logic to fall back on the default FAQ post), but I've come to realize that calling get_page_by_path() returns an entire Post object... including post_content.

In this case, rather than running a separate WP_query and looping through could I just do something like:

$faq_post = get_page_by_path( $post_slug, OBJECT, 'land' );
$content = $faq_post->post_content;

??

I know get_template_part() has this built in, but the the "partial" in this case must be a Post that the client can edit like the rest of their content using a visual page builder plugin.

I have a page template I use for landing pages for various ad campaigns. The template uses custom fields for things like headlines and other shorter pieces of content within the page. However the template includes an entire section of FAQs. In many cases, the FAQs are the same across landing pages, but the requirement arose to have the ability to use a different set of FAQs (or different, structured/formatted content altogether) in that space if needed. I have built some logic into the template that will fetch the_content of a post containing a default set of FAQs and add it on to the_content of the landing page itself. Within this, I'm trying to add some additional logic that works similar to WP's template hierarchy... 'if you find a post named this use it, if not look for a post named that and use IT and if you don't find either, use this default'.

I'm basing it off of the slug of the landing page. So if the landing page's slug is holiday1 and there's a post with a slug of landing-faq-holiay1, it will be inserted into the landing page. And if not, the default post with a slug of landing-faq will be inserted.

I'm using the following code to insert the_content:

global $post;
$post_slug=$post->post_name;

$faq_id = get_page_by_path( $post_slug, OBJECT, 'land' )->ID;

$query = new WP_query( array('page_id' => $faq_id) );

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) : $query->the_post();
        $content = apply_filters('the_content', get_the_content());
        ?>
        <div class="content">
            <?php echo $content; ?>
        </div>
        <?php
    endwhile;
endif;

This all works well and good (for the sake of simplicity, I left out the conditional logic to fall back on the default FAQ post), but I've come to realize that calling get_page_by_path() returns an entire Post object... including post_content.

In this case, rather than running a separate WP_query and looping through could I just do something like:

$faq_post = get_page_by_path( $post_slug, OBJECT, 'land' );
$content = $faq_post->post_content;

??

I know get_template_part() has this built in, but the the "partial" in this case must be a Post that the client can edit like the rest of their content using a visual page builder plugin.

Share Improve this question asked Nov 9, 2018 at 5:15 Daveh0Daveh0 1912 silver badges13 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Yes, get_page_by_path() will return a post object or array on success, and not the post ID.

$faq_post = get_page_by_path( $post_slug, OBJECT, 'land' );  // object
$faq_post = get_page_by_path( $post_slug, ARRAY_A, 'land' ); // array

So there is no need to make the custom new WP_Query call, and your code below is good:

$faq_post = get_page_by_path( $post_slug, OBJECT, 'land' );
$content = $faq_post->post_content;

However, if I understand you properly, the $post_slug should be:

$post_slug = 'landing-faq-' . $post->post_name;

And check that $faq_post is not a null (which indicates a failure):

$content = $faq_post ? $faq_post->post_content : '';

And it might be faster to perform a custom SQL query to search for the post with that slug:

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;

    // If you need to apply all filters to the content.
    $content = apply_filters( 'the_content', $content );
    $content = str_replace( ']]>', ']]&gt;', $content );
} else {
    // Here you can grab the content of the default post.
}
Post a comment

comment list (0)

  1. No comments so far