2012-09-30 23 views
2

我正在使用类别模板列出一系列自定义帖子,其中每个自定义帖子都列出了指向文档的链接。有些文件是公开的,有些是私人文件。目前,如果该帖子是公开的,则显示链接;但如果链接是私密的,我回应一个声明“私人资源 - 请登录阅读。”在wordpress中,如何在必要时将链接重定向到登录,然后返回到同一页面

我想让单词“登录”一个链接,将任何未登录的用户发送到登录页面,然后将用户重定向回此页面。但是我一直无法使它工作。我想可能auth_redirect()是要走的路,但也许我是以此为基础的。

下面是循环的一个稍微简化的版本,因为它目前代表。

if (have_posts()) : 
    while (have_posts()) : the_post(); 
    // the loop 
    $private = get_post_custom_values('private'); // read custom field 

    if (isset($private[0]) && $private[0] == 'yes') { // if the post is private 

    if (is_user_logged_in()) {// and if the user is logged in 
     // display private post ?> 
     <div> 
      <h4><?php the_title(); ?></h4> 
      <?php get_post_custom_keys();?> 
      <p><?php the_content(); ?></p> 
      <p><a href="<?php echo get_post_meta($post->ID, "resourcelink", true); ?>">Resource Link</a></p> 
      <?php if (get_post_meta($post->ID, "private", true)==yes) {?> 
      <p class="private">This document private.</p> 
      <?php } ?> 
     </div> 
    <?php } 

    else { // if the user is not logged in 
     // want make the a login link, with redirect back to this page 
     // but now just tells user to log in ?> 
     <div> 
      echo the_title('<h4>','</h4>'); 
      get_post_custom_keys();?> 
      <p><?php the_content(); ?></p> 
     <?php echo '<p>Private resource &mdash; please log in to read.</p>'; 
    } 
    } 
    else { // if the post is public 
    // display public post, for every visitor ?> 
     <div> 
     <h4><?php the_title(); ?></h4> 
     <?php get_post_custom_keys();?> 
     <p><?php the_content(); ?></p> 
     <p><a href="<?php echo get_post_meta($post->ID, "resourcelink", true); ?>">Resource Link</a></p> 
     </div> 
    <?php } 
    endwhile; 
endif; 
?> 

回答

0

如果用户没有登录,你可以只显示他们没有权限和wp_login_form()它处理重定向和所有。 Reference

+0

我会试试看。 – Beth

+0

[wp_login_url()](http://codex.wordpress.org/Function_Reference/wp_login_url) –

相关问题