2024年07月07日 建站教程
WordPress如何判断当前文章是否是密码保护文章,下面web建站小编给大家简单介绍一下具体运用方法!
函数源码:
function post_password_required( $post = null ) { $post = get_post( $post ); if ( empty( $post->post_password ) ) { return apply_filters( 'post_password_required', false, $post ); } if ( ! isset( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ) ) { return apply_filters( 'post_password_required', true, $post ); } require_once ABSPATH . WPINC . '/class-phpass.php'; $hasher = new PasswordHash( 8, true ); $hash = wp_unslash( $_COOKIE[ 'wp-postpass_' . COOKIEHASH ] ); if ( 0 !== strpos( $hash, '$P$B' ) ) { $required = true; } else { $required = ! $hasher->CheckPassword( $post->post_password, $hash ); } return apply_filters( 'post_password_required', $required, $post ); } apply_filters( 'post_password_required', bool $required, WP_Post $post )
使用示例:
$pass_masterPost = get_post(); if ( post_password_required( $pass_masterPost->ID ) ) { echo get_the_password_form(); echo '<p>THIS POST IS PASSWORD PROTECTED: PLEASE ENTER IT!</p>'; } else { if ( have_posts() ) { while ( have_posts() ) { the_post(); the_content(); echo '<hr>'; } } else { echo '<p>Nothing Found!</p>'; } }
本文链接:http://so.lmcjl.com/news/8002/