elcontraption/wp-post-inspector

用于交互和检查 WordPress 帖子对象的工具。

0.1.5 2018-05-03 16:30 UTC

This package is not auto-updated.

Last update: 2024-09-29 21:58:10 UTC


README

用于交互和检查 WordPress 帖子对象的工具。

安装

通过 composer 将其安装为你的主题的依赖项

composer require elcontraption/wp-post-inspector

检索帖子对象

use \WpPostInspector\PostInspector;

// Get the current post object:
$currentPost = new PostInspector();

// Get a specific post object by ID
$post1 = new PostInspector(1);

// Get a specific post object by slug:
$helloWorldPost = new PostInspector('hello-world');

方法

ancestors

以 PostInspector 对象的形式返回祖先数组。

$currentPost->ancestors();

descendants

以 PostInspector 对象的形式返回后代数组。

$currentPost->descendants();

parent

访问父级 PostInspector 对象。

$currentPost->parent();

permalink

get_permalink($currentPost->id()) 的快捷方式。

$currentPost->permalink();

siblings

以 PostInspector 对象的形式返回兄弟数组。

$currentPost->siblings();

top

以 PostInspector 对象的形式访问顶级祖先。

$currentPost->top();

访问帖子属性

您可以使用标准的 WP_Post 属性(作为方法)或任何内置到该类中的快捷方法。

// Display the current post title using a shortcut method:
echo $currentPost->title(); // "Hello world!"

// Using a standard WP_Post attribute name:
echo $currentPost->post_title(); // "Hello world!"

遍历帖子层次结构

您可以使用 parenttopancestorsdescendantssiblings 方法遍历帖子层次结构

// Get the parent of the current post object:
$parent = $currentPost->parent();

// Accessing attributes on the parent object:
echo $parent->slug();

// Display the title of the top ancestor post object
echo $currentPost->top()->title();

// The 'ancestors', 'descendants', and 'siblings' methods all return arrays of PostInspector objects:
$ancestors = $currentPost->ancestors();

foreach ($ancestors as $ancestor)
{
    var_dump($ancestor->title());
}

// Method chaining is possible:
$grandParent = $currentPost->parent()->parent();
$grandAunts = $currentPost->parent()->parent()->siblings();