如何检索 Wordpress 页面的兄弟页面列表? [英] How can I retrieve a list of a Wordpress page's sibling pages?

查看:28
本文介绍了如何检索 Wordpress 页面的兄弟页面列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 WordPress 中创建一个兄弟页面(不是帖子)列表来填充页面的侧边栏.我成功编写的代码返回页面的父标题.

I am trying to create a list of sibling pages (not posts) in WordPress to populate a page's sidebar. The code I've written successfully returns a page's parent's title.

<?php
$parent_title = get_the_title($post->post_parent);
echo $parent_title; ?>

据我所知,您需要一个页面的 id(而不是标题)来检索页面的兄弟(通过 wp_list_pages).如何获取页面的父ID?

As I understand it, you need a page's id (rather than title) to retrieve a page's siblings (via wp_list_pages). How can I get the page's parent's id?

欢迎使用其他方法.目标是列出页面的兄弟姐妹,不一定只是检索父页面的 id.

Alternate approaches are welcome. The goal is to list a page's siblings, not necessarily just retrieving the parent's id.

推荐答案

$post->post_parent 正在给你父 ID,$post->ID会给你当前的页面 ID.因此,以下将列出页面的兄弟:

$post->post_parent is giving you the parent ID, $post->ID will give you the current page ID. So, the following will list a page's siblings:

wp_list_pages(array(
    'child_of' => $post->post_parent,
    'exclude' => $post->ID
))

这篇关于如何检索 Wordpress 页面的兄弟页面列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆