WordPress函数:get_edit_post_link 文章编辑链接

江河/ 2023年08月26日/ WordPress/ 浏览 665

函数原型:


get_edit_post_link( int|WP_Post $post, string $context = 'display' ): string|null


检索帖子的编辑帖子链接。


可以在WordPress循环中使用,也可以在它之外使用。可以与页面、帖子、附件、修订、全局样式、模板和模板部件一起使用。


参数说明:


$post,文章ID或文章对象。默认值为全局$post。


$context,如何输出“&”字符。默认“&”。


函数源码:


function get_edit_post_link( $post = 0, $context = 'display' ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return;
	}

	if ( 'revision' === $post->post_type ) {
		$action = '';
	} elseif ( 'display' === $context ) {
		$action = '&action=edit';
	} else {
		$action = '&action=edit';
	}

	$post_type_object = get_post_type_object( $post->post_type );

	if ( ! $post_type_object ) {
		return;
	}

	if ( ! current_user_can( 'edit_post', $post->ID ) ) {
		return;
	}

	$link = '';

	if ( 'wp_template' === $post->post_type || 'wp_template_part' === $post->post_type ) {
		$slug = urlencode( get_stylesheet() . '//' . $post->post_name );
		$link = admin_url( sprintf( $post_type_object->_edit_link, $post->post_type, $slug ) );
	} elseif ( 'wp_navigation' === $post->post_type ) {
		$link = admin_url( sprintf( $post_type_object->_edit_link, (string) $post->ID ) );
	} elseif ( $post_type_object->_edit_link ) {
		$link = admin_url( sprintf( $post_type_object->_edit_link . $action, $post->ID ) );
	}

	return apply_filters( 'get_edit_post_link', $link, $post->ID, $context );
}


包含钩子:


apply_filters( 'get_edit_post_link', string $link, int $post_id, string $context )


使用举例:


function wpdocs_remove_get_edit_post_link( $link ) {
    if ( current_user_can( 'administrator' ) ) {
        return $link;
    }

    return null;
}

add_filter( 'get_edit_post_link', 'wpdocs_remove_get_edit_post_link' );


发表评论

暂无评论,抢个沙发...

客服 工单