admin管理员组文章数量:1130349
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.
1 Answer
Reset to default 1Yes, 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( ']]>', ']]>', $content );
} else {
// Here you can grab the content of the default post.
}
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.
1 Answer
Reset to default 1Yes, 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( ']]>', ']]>', $content );
} else {
// Here you can grab the content of the default post.
}
本文标签: wp queryinserting content of 1 Post to in another with a template hierarchy
版权声明:本文标题:wp query - inserting content of 1 Post to in another with a template hierarchy 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749195421a2330894.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论