获取子帖子 ID 数组,wordpress 3.0,php

发布于 2024-10-18 07:16:55 字数 930 浏览 3 评论 0原文

好的,这是给您的一个...

在自定义模板上,我使用此代码来检索 &显示子页面/帖子列表

$args = array(
                    'depth'        => 1,
                    'show_date'    => '',
                    'date_format'  =>     get_option('date_format'),
                    'child_of'     => $post->ID,
                    'exclude'      => '',
                    'include'      => '',
                    'title_li'     => '',
                    'echo'         => 1,
                    'authors'      => '',
                    'sort_column'  => 'menu_order, post_title',
                    'link_before'  => '',
                    'link_after'   => '',
                    'walker' => '' );

                    wp_list_pages( $args );

这非常有用,我还想知道如何访问/创建子帖子 ID 的数组。我的目标是通过每个子帖子的 get_post_meta() 函数使用其 ID 来访问一些自定义字段元数据。

谢谢你们。

Ok here's one for ya...

On a custom template I'm using this code to retrieve & display a list of child pages/posts

$args = array(
                    'depth'        => 1,
                    'show_date'    => '',
                    'date_format'  =>     get_option('date_format'),
                    'child_of'     => $post->ID,
                    'exclude'      => '',
                    'include'      => '',
                    'title_li'     => '',
                    'echo'         => 1,
                    'authors'      => '',
                    'sort_column'  => 'menu_order, post_title',
                    'link_before'  => '',
                    'link_after'   => '',
                    'walker' => '' );

                    wp_list_pages( $args );

This works great, I'm also wondering how I can access/create an array of child post ID's. My goal is to access some custom fields meta data through the get_post_meta() function of each child post using it's ID.

Thanks guys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

掩耳倾听 2024-10-25 07:16:55

我想我对此不是很清楚,因为这是我第一次从未收到过这样的答复。

我设法找到了我需要的信息,并将其放在这里,供其他有相同请求的人浏览。

好的 - 获取所有孩子的 ID..

$pages = get_pages('child_of=X');
    foreach($pages as $child) {

    // Now you have an object full of Children ID's that you can use for whatever
    // E.G 
    echo $child->ID . "<br />";
}

I guess I wasn't very clear with this one as it's the first time I've never recieved an answer from SO.

I managed to find the information I needed and will place it here for anyone else browsing with the same request.

ok - To get all child IDs..

$pages = get_pages('child_of=X');
    foreach($pages as $child) {

    // Now you have an object full of Children ID's that you can use for whatever
    // E.G 
    echo $child->ID . "<br />";
}
紙鸢 2024-10-25 07:16:55

如果你想构建一个帖子 id 数组供以后使用,你可以这样做:

$pageids = array();
$pages = get_pages('child_of=X');
    foreach($pages as $page){
     $pageids[] = $page->ID;
}

并且你有一个干净的页面 id 数组。

If you want to build an array of post ids for later use you can do this:

$pageids = array();
$pages = get_pages('child_of=X');
    foreach($pages as $page){
     $pageids[] = $page->ID;
}

And you have a clean array of just page ids.

霞映澄塘 2024-10-25 07:16:55
$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish');
foreach($children as $child)
{
echo '<br/>ID:'.$child->ID;
}

您可以使用其他属性(即 $child->post_content)...
如果您需要定义 post_type,则也添加此参数:&post_type=POST_TYPE_NAME

$children = get_posts('post_parent=SLUG_OF_PARENT_POST&post_status=publish');
foreach($children as $child)
{
echo '<br/>ID:'.$child->ID;
}

you can use other attributes (i.e. $child->post_content)...
if you need to define post_type, then add this argument too : &post_type=POST_TYPE_NAME

美羊羊 2024-10-25 07:16:55

另一种方法可以做到这一点:

$my_page_id = 12345;

$child_query_args = array(
    'post_parent'         => $my_page_id,
    'post_type'           => 'page',
    'posts_per_page'      => -1,
    'fields'              => 'ids',
);

$child_query  = new WP_Query($child_query_args);

if ( $child_query && $child_query->have_posts() && $child_query->posts ) {  

    // (Since fields=ids, $child_query->posts is just an array of IDs)
    $child_ids = $child_query->posts;

    foreach ( $child_ids as $child_id ) {

        $whatever = get_post_meta( $child_id, 'whatever', true );

        echo esc_html($whatever);

    }

}

Another way to do this:

$my_page_id = 12345;

$child_query_args = array(
    'post_parent'         => $my_page_id,
    'post_type'           => 'page',
    'posts_per_page'      => -1,
    'fields'              => 'ids',
);

$child_query  = new WP_Query($child_query_args);

if ( $child_query && $child_query->have_posts() && $child_query->posts ) {  

    // (Since fields=ids, $child_query->posts is just an array of IDs)
    $child_ids = $child_query->posts;

    foreach ( $child_ids as $child_id ) {

        $whatever = get_post_meta( $child_id, 'whatever', true );

        echo esc_html($whatever);

    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文