【问题标题】:Password protect a page template which gets posts?密码保护获取帖子的页面模板?
【发布时间】:2014-10-09 14:59:15
【问题描述】:

我已经构建了一个模板,该模板从一个类别中获取帖子并添加了它,因为我希望它受到密码保护。它似乎不起作用,我不知道为什么。

有好心人帮帮我吗?

<?php
if ( ! post_password_required( $post ) ) {

$temp = $wp_query; $wp_query= null;
$wp_query = new WP_Query(); $wp_query->query('cat=495' . '&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

<div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent          Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<div class="entry">
 <?php the_content() ?>
 </div>

 <br>

 </div>

  <?php endwhile; ?>

 }else{
// we will show password form here
echo get_the_password_form();
 }

 ?>

【问题讨论】:

  • 愚蠢的问题,但你真的用密码保护了帖子本身吗?
  • 不,我没有对帖子进行密码保护,因为这会非常耗时。我真的希望该页面被锁定,然后当您输入密码时,它会显示该类别的帖子。目前还没有在网上找到任何解决方案...
  • 不,我的意思是您是否在运行此模板的“页面”上添加了密码?这个页面正在拉不同的帖子......但是页面本身需要密码......
  • 哦,是的,对不起,我有。测试页面时,我收到此错误消息“ Parse error: syntax error, unexpected $end in /home/fhlinux221/j/jenningsrugs.co.uk/user/htdocs/pages/wp-content/themes/default/priviledged-customers .php 在第 65 行"
  • 好吧,看来这个故事还有更多内容!上面的代码不是 65 行,所以请将代码发布到“privileged-customers.php”页面,我会找到你的错误。

标签: php wordpress passwords


【解决方案1】:
`<?php
/*
Template Name: Privileged Customers Template
*/

get_header();
?>
<table width="1000" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><img src="../../../images/blank_middle/blank_middle_01.jpg" width="57" height="20"></td>
<td><img src="../../../images/blank_middle/blank_middle_02.jpg" width="886" height="20"></td>
<td><img src="../../../images/blank_middle/blank_middle_03.jpg" width="57" height="20"></td>
 </tr>
 <tr>
<td background="../../../images/blank_middle/blank_middle_04.jpg"><img src="../../../images/spacer.gif" width="57" height="416" /></td>
<td valign="top" background="../../../images/blank_middle/blank_middle_05.jpg"><table width="886"   border="0" cellspacing="0" cellpadding="0">
    <tr>
      <td><img src="../../../images/spacer.gif" width="30" height="1" /></td>
      <td><img src="../../../images/spacer.gif" width="826" height="1" /></td>
      <td><img src="../../../images/spacer.gif" width="30" height="1" /></td>
    </tr>
    <tr>
      <td><img src="../../../images/spacer.gif" width="30" height="10" /></td>
      <td align="left">
      <div id="content" class="narrowcolumn" role="main">
 <h2><?php the_title();?></h2>
 <?php do_action('posts_logout_link'); ?>
 <?php
 if ( ! post_password_required( $post ) ) {

 $temp = $wp_query; $wp_query= null;
 $wp_query = new WP_Query(); $wp_query->query('cat=495' . '&paged='.$paged);
 while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

 <div <?php post_class() ?>>
<h3 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent               Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<div class="entry">
<?php the_content() ?>
 </div>

 <br>

 </div>

<?php endwhile; ?>

}else{
// we will show password form here
echo get_the_password_form();
}

?>



  </div>
      </td>
      <td><img src="../../../images/spacer.gif" width="30" height="10" /></td>
    </tr>
  </table></td>
<td background="../../../images/blank_middle/blank_middle_06.jpg"><img    src="../../../images/spacer.gif" width="57" height="416" /></td>

`

【讨论】:

    【解决方案2】:

    在您发布的代码的第 46-53 行,您有:

    <?php endwhile; ?>
    
    }else{
    // we will show password form here
    echo get_the_password_form();
    }
    
    ?>
    

    第 46 行,您在同一行打开和关闭 PHP,然后尝试在之后运行更多 PHP(第 48 行)。由于您关闭了 PHP 并且没有再次打开它,因此从第 48 行开始的 PHP 被呈现为 HTML...因此您的 if/else 语句永远不会正确关闭...因此出现Parse error: syntax error, unexpected $end 错误。

    将第 46 行更改为:

    <?php endwhile; ?>
    

    到:

    <?php endwhile;
    

    【讨论】:

    • 太棒了!您能否将我的答案标记为正确,以便我获得学分?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    相关资源
    最近更新 更多