函数文档

wp_list_post_revisions()

💡 云策文档标注

概述

wp_list_post_revisions() 函数用于显示文章修订版本的列表,支持以无序列表或表格格式输出,并提供编辑和恢复链接。

关键要点

  • 函数输出文章修订列表,可配置为 UL 或 TABLE 格式,包含 diff 界面和恢复操作链接。
  • 参数 $post 可选,接受文章 ID 或 WP_Post 对象,默认为全局 $post。
  • 参数 $type 可选,默认为 'all',可指定为 'revision' 或 'autosave' 以过滤修订类型。
  • 内部调用 wp_get_post_revisions() 获取修订,并使用 wp_post_revision_title_expanded() 格式化输出。
  • 自 WordPress 3.6 起,$args 数组参数已弃用,建议直接使用 $type 参数。
  • 函数检查当前用户权限(使用 current_user_can()),仅显示用户有权访问的修订。

代码示例

function wp_list_post_revisions( $post = 0, $type = 'all' ) {
    $post = get_post( $post );

    if ( ! $post ) {
        return;
    }

    // $args array with (parent, format, right, left, type) deprecated since 3.6.
    if ( is_array( $type ) ) {
        $type = ! empty( $type['type'] ) ? $type['type'] : $type;
        _deprecated_argument( __FUNCTION__, '3.6.0' );
    }

    $revisions = wp_get_post_revisions( $post->ID );

    if ( ! $revisions ) {
        return;
    }

    $rows = '';
    foreach ( $revisions as $revision ) {
        if ( ! current_user_can( 'read_post', $revision->ID ) ) {
            continue;
        }

        $is_autosave = wp_is_post_autosave( $revision );
        if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) ) {
            continue;
        }

        $rows .= "t" . wp_post_revision_title_expanded( $revision ) . "n";
    }

    echo "" . __( 'JavaScript must be enabled to use this feature.' ) . "n";

    echo "n";
    echo $rows;
    echo '';
}

注意事项

  • 确保 JavaScript 已启用以使用此功能,否则可能影响界面交互。
  • 自 WordPress 3.6 起,避免使用已弃用的 $args 数组参数,改用 $type 字符串参数。
  • 函数依赖于多个相关函数,如 wp_get_post_revisions() 和 wp_post_revision_title_expanded(),需确保这些函数正常工作。

📄 原文内容

Displays a list of a post’s revisions.

Description

Can output either a UL with edit links or a TABLE with diff interface, and restore action links.

Parameters

$postint|WP_Postoptional
Post ID or WP_Post object. Default is global $post.
$typestringoptional
'all' (default), 'revision' or 'autosave'

Source

function wp_list_post_revisions( $post = 0, $type = 'all' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	// $args array with (parent, format, right, left, type) deprecated since 3.6.
	if ( is_array( $type ) ) {
		$type = ! empty( $type['type'] ) ? $type['type'] : $type;
		_deprecated_argument( __FUNCTION__, '3.6.0' );
	}

	$revisions = wp_get_post_revisions( $post->ID );

	if ( ! $revisions ) {
		return;
	}

	$rows = '';
	foreach ( $revisions as $revision ) {
		if ( ! current_user_can( 'read_post', $revision->ID ) ) {
			continue;
		}

		$is_autosave = wp_is_post_autosave( $revision );
		if ( ( 'revision' === $type && $is_autosave ) || ( 'autosave' === $type && ! $is_autosave ) ) {
			continue;
		}

		$rows .= "t<li>" . wp_post_revision_title_expanded( $revision ) . "</li>n";
	}

	echo "<div class='hide-if-js'><p>" . __( 'JavaScript must be enabled to use this feature.' ) . "</p></div>n";

	echo "<ul class='post-revisions hide-if-no-js'>n";
	echo $rows;
	echo '</ul>';
}

Changelog

Version Description
2.6.0 Introduced.