如果你看看source code 的get_the_content()
函数检索帖子内容,您将看到以下条件:
if ( post_password_required( $post ) )
return get_the_password_form( $post );
在
get_the_password_form()
函数中,有一个过滤器可用于自定义默认密码表单的HTML:
return apply_filters( \'the_password_form\', $output );
因此,根据您的代码,它应该是这样的,并放置在主题的
functions.php
文件:
function get_the_v1i1_password_form( $output ) {
// If not in the v1-i1 category, return the $output as-is.
if ( ! in_category( \'v1-i1\' ) ) {
return $output;
}
// Here you can customize the form HTML.
$post = get_post();
$label = \'pwbox-\' . ( empty($post->ID) ? rand() : $post->ID );
$output = \'<form action="\' . esc_url( site_url( \'wp-login.php?action=postpass\', \'login_post\' ) ) . \'" class="post-password-form" method="post">
<p>\' . __( \'This is exclusive content from v1:i1. Please enter password below:\' ) . \'</p>
<p><label for="\' . $label . \'">\' . __( \'Password:\' ) . \' <input name="post_password" id="\' . $label . \'" type="password" size="20" /></label> <input type="submit" name="Submit" value="\' . esc_attr_x( \'Enter\', \'post password form\' ) . \'" /></p></form>
\';
return $output;
}
add_filter( \'the_password_form\', \'get_the_v1i1_password_form\' );
注:该
get_the_v1i1_password_form()
现在是筛选器回调,因此不应使用
return apply_filters( \'the_password_form\', $output );
这将导致页面停止工作。
你也不需要这个:
if (post_password_required( $post ) && in_category( \'v1-i1\' )) {
return get_the_v1i1_password_form( $post );
} else {
return get_the_password_form( $post );
}
附加说明以上代码可让您完全控制表单HTML,但如果您只想替换文本”
This content is password protected. To view it please enter your password below:
,那么您只需使用
str_replace()
像这样:
function get_the_v1i1_password_form( $output ) {
// If is in the v1-i1 category, replace the text.
if ( in_category( \'v1-i1\' ) ) {
return str_replace(
\'This content is password protected. To view it please enter your password below:\',
\'This is exclusive content from v1:i1. Please enter password below:\',
$output
);
}
return $output;
}
add_filter( \'the_password_form\', \'get_the_v1i1_password_form\' );
但当然,这只适用于
get_the_password_form()
作用