wp查询以获取当前页面的子页面
-
-
试试这个解决方案 == 获得职位的孩子-http://wordpress.stackexchange.com/a/123143/42702Try this solution == get children of a post - http://wordpress.stackexchange.com/a/123143/42702
- 0
- 2013-11-13
- T.Todua
-
3 个回答
- 投票数
-
- 2012-07-31
您必须将
child_of
更改为post_parent
,并添加post_type => 'page'
:WordPress Codex Wp_query 发布并发布;页面参数
<?php $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $parent = new WP_Query( $args ); if ( $parent->have_posts() ) : ?> <?php while ( $parent->have_posts() ) : $parent->the_post(); ?> <div id="parent-<?php the_ID(); ?>" class="parent-page"> <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> <p><?php the_advanced_excerpt(); ?></p> </div> <?php endwhile; ?> <?php endif; wp_reset_postdata(); ?>
You have to change
child_of
topost_parent
and also addpost_type => 'page'
:WordPress codex Wp_query Post & Page Parameters
<?php $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $parent = new WP_Query( $args ); if ( $parent->have_posts() ) : ?> <?php while ( $parent->have_posts() ) : $parent->the_post(); ?> <div id="parent-<?php the_ID(); ?>" class="parent-page"> <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> <p><?php the_advanced_excerpt(); ?></p> </div> <?php endwhile; ?> <?php endif; wp_reset_postdata(); ?>
-
感谢花花公子,我尝试了`post_parent`原始的,但是关键是`'post_type'=>'page'`-WordPress的查询默认会发布吗?当它允许我时,我会接受答案.Thanks dude, I tried `post_parent` original but it's `'post_type' => 'page'` that is the key - does wordpress querys default to post then? I will accept answer when it lets me.
- 1
- 2012-07-31
- Joshc
-
是的,默认为`'post_type'=>'post'`.Yes, `'post_type' => 'post'` is default.
- 0
- 2019-03-26
- mrwweb
-
- 2020-02-26
我知道这是一个非常老的问题,但是由于我着手解决这个问题,其他人也应该这样做.
Wordpress提供了一种非常简单的列表页面解决方案,您还可以在其中添加一些参数.
这就是显示页面的子代所需的全部内容
wp_list_pages(array( 'child_of' => $post->ID, 'title_li' => '' ))
查看 wp_list_pages的参考页,了解您可以应用的所有选项.
I know this is a very old question, but since I landed on it, others might as well.
Wordpress has a very simple solution for listing pages, where you can add some arguments as well.
This is all you will need to display a page's children:
wp_list_pages(array( 'child_of' => $post->ID, 'title_li' => '' ))
Look at the reference page for wp_list_pages for all options you can apply.
-
这将返回HTML字符串,而不是post对象的列表,因此可能不是OP想要的.This will return an HTML string rather than a list of post objects, so probably not what the OP wants.
- 0
- 2020-07-27
- Alexander Holsgrove
-
- 2020-02-05
将其重写为functions.php中的函数,您需要添加 全球$post;
function page_summary() { global $post; $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $parent = new WP_Query( $args ); if ( $parent->have_posts() ) : while ( $parent->have_posts() ) : $parent->the_post(); ?> <div id="parent-<?php the_ID(); ?>" class="parent-page"> <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> </div> <?php endwhile; endif; wp_reset_postdata(); }
Rewriting this to a function in functions.php you need to add global $post;
function page_summary() { global $post; $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $parent = new WP_Query( $args ); if ( $parent->have_posts() ) : while ( $parent->have_posts() ) : $parent->the_post(); ?> <div id="parent-<?php the_ID(); ?>" class="parent-page"> <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> </div> <?php endwhile; endif; wp_reset_postdata(); }
任何人都可以帮助我使用wp_query.
我正在制作一个模板文件/循环来创建和归档当前页面子页面的页面.
该查询必须是自动的,因为我在几页中都会用到.
这是我在下面的查询,但是它只返回我的帖子,而不是子页面.
在此先感谢您的帮助.
乔什