我有一个自定义的帖子类型,我在其中创建了3个父子关系页面。页面的创建方式如下:父-子-孙
我希望子页面和孙页面获得与父页面相同的内容,并且我从内容区域中的仪表板添加的所有内容都要添加到父帖子内容之后。
例如,如果我的父帖子包含几行文本,而在我的子帖子中,我会再添加几行文本,这应该添加在父内容之后。
以下是创建CPT层次结构的函数:
function wpse121567_get_cpt_hierarchy() {
// Make sure it\'s the right CPT
if ( \'debate-parent\' != get_post_type() ) {
return false;
}
// Globalize the post object
global $post;
// Parent CPT
if ( 0 == $post->post_parent ) {
return \'debate-parent\';
}
// Not a parent CPT, so fetch current post parent
$parent = get_post( $post->post_parent );
// Child CPT
if ( 0 == $parent->post_parent ) {
return \'debate-child\';
} else {
return \'debate-grandchild\';
}
}
这是我的单曲{myCPT.php}:
<div id="content" class="span9" role="main">
<?php if ( \'debate-parent\' == wpse121567_get_cpt_hierarchy() ) {
// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( \'inc/post-format/single\', get_post_format() );
endwhile; endif;
}
// Child CPT
else if ( \'debate-child\' == wpse121567_get_cpt_hierarchy() ) {
// Globalize post object
the_post();
$parent = get_post( $post->post_parent );
echo \'<h1>\' . $parent->post_title . \'</h1>\';
$content = apply_filters( \'the_content\', $parent->post_content ) . get_the_content();
echo \'<div>\' . $content . \'</div>\';
// Fetch parent CPT comments
$parent_cpt_comments = get_comments( array(
\'post_id\' => $post->post_parent,
\'status\' => \'approve\'
) );
// Loop through parent CPT comments
foreach ( $parent_cpt_comments as $comment ) {
// Output comment list markup here
}
}
// Grandchild CPT
else if ( \'debate-grandchild\' == wpse121567_get_cpt_hierarchy() ) {
// Comment Stats code goes here
}
?>
<?php
$enable_rtl = of_get_option(\'enable_rtl\', false);
if(!of_get_option(\'disable_pagination\')){
if($enable_rtl){
$next_post = get_adjacent_post( false, \'\', true );
$prev_post = get_adjacent_post( false, \'\', false );
}else{
$next_post = get_adjacent_post( false, \'\', false );
$prev_post = get_adjacent_post( false, \'\', true );
}
?>
<?php
}
// show related posts by tag
if(!of_get_option(\'disable_related_posts\')){
get_template_part( \'inc/related-posts\' );
}
// If comments are open or we have at least one comment, load up the default comment template provided by Wordpress
if ( comments_open() )
comments_template( \'\', true );
else{ // Well, if there are no posts to display and loop through, let\'s apologize to the reader (also your 404 error) ?>
<article class="post error">
<h1 class="404"><?php _e(\'Page not found\', \'outbox\'); ?></h1>
</article>
<?php } // OK, I think that takes care of both scenarios (having a post or not having a post to show) ?>
</div><!-- #content .site-content -->
我的问题是,如何自动获取每个子元素上的父帖子内容,以及是否在父元素的内容之后从仪表板添加更多内容?
非常感谢。
最合适的回答,由SO网友:gmazzap 整理而成
我的问题是如何自动获取每个孩子的家长帖子内容
您的代码应该按原样工作:如果您有一个父级,并且您将任意数量的子级添加到此父级(wpse121567_get_cpt_hierarchy
return(对他们来说是“discome-child”),您的代码将显示父帖子的内容。
如果我在父元素的内容之后添加仪表板中的更多内容?
为此,您有:
global $post;
$parent = get_post( $post->post_parent );
echo \'<h1>\' . $parent->post_title . \'</h1>\';
echo \'<div>\' . apply_filters( \'the_content\', $parent->post_content ) . \'</div>\';
替换为
the_post();
$parent = get_post( $post->post_parent );
echo \'<h1>\' . $parent->post_title . \'</h1>\';
$content = apply_filters( \'the_content\', $parent->post_content ) . get_the_content();
echo \'<div>\' . $content . \'</div>\';
SO网友:Andrew Bartel
您需要检索父级和父级内容,然后在显示帖子本身的标准内容之前显示这些内容。
因此,在循环本身中,检查此帖子是否有父级和/或父级,然后显示它,否则直接跳过它。
get_header();
while( have_posts() ) {
the_post();
$post = get_post(get_the_ID());
// if this post has a parent, retrieve it
$parent = ($post->post_parent != 0) ? get_post($post->post_parent) : null;
// if this post has a grand parent, retrieve it by referencing the parent\'s parent
$grandparent = (is_object($parent) && $parent->post_parent !=0 ) ? get_post($parent->post_parent) : null;
// if we have a grandparent, retrieve the comments for later use and display the grandparent\'s title and content
if( is_object( $grandparent ) && $grandparent->post_content ) {
$grandparent_comments = get_comments( array(
\'post_id\' => $grandparent->ID,
\'status\' => \'approve\'
) );
echo \'<h1>\'.$grandparent->post_title.\'</h1>\';
echo \'<div>\'.apply_filters(\'the_content\',$grandparent->post_content).\'</div>\';
}
// if we have a parent, retrieve the comments for later use and display the parent\'s title and content
if( is_object( $parent ) && $parent->post_content ) {
$parent_comments = get_comments( array(
\'post_id\' => $parent->ID,
\'status\' => \'approve\'
) );
echo \'<h1>\'.$parent->post_title.\'</h1>\';
echo \'<div>\'.apply_filters(\'the_content\',$parent->post_content).\'</div>\';
}
// echo the content of the actual post we\'re on
the_content();
// if the grandparent post has comments, we check to see if it\'s an object because if there are no comments get_comments() returns an array rather than an object
if( isset( $grandparent_comments ) && is_object( $grandparent_comments ) ) {
foreach($grandparent_comments as $grandparent_comment) {
// echo comments
}
}
// if the parent post has comments
if( isset( $parent_comments ) && is_object( $parent_comments ) ) {
foreach($parent_comments as $parent_comment) {
// echo comments
}
}
$post_comments = get_comments( array(
\'post_id\' => get_the_ID(),
\'status\' => \'approve\'
) );
if( is_object( $post_comments ) ) {
foreach($post_comments as $post_comment) {
// echo comments
}
}
}
get_footer();