当您未登录到WP管理员时可以使用的个人主页

时间:2016-12-28 作者:AdamJones

我对私人帖子/页面的理解是,只有当您以管理员或编辑的身份登录到WP管理系统时,它们才起作用。

我有一个网站,我需要偶尔通过链接向用户共享自定义帖子,我不希望他们出现在网站的其他地方。将帖子设置为“private”几乎可以满足我的需要,因为它可以立即将帖子从网站主页和其他通常包含的区域中删除。

唯一的问题是,私有帖子功能假设我想在登录到管理员时查看帖子,但事实并非如此,因为我在未登录时收到404错误。我想在没有密码的情况下手动与陌生人分享这篇文章,让他们通过标准的永久链接正常显示。

这很可能是插件领域,但令我惊讶的是,我还没有找到一个可以做到这一点的插件。

为了澄清这一点,我需要在其上实现的帖子类型是一个自定义的帖子类型,由站点运行所需的插件定义。

5 个回复
最合适的回答,由SO网友:Jami Gibbs 整理而成

如果您不想使用插件(或者找不到一个可以满足您需要的插件),您可能需要这样做:

添加custom meta box 允许您将帖子标记为hidden.pre_get_posts 从站点中删除标记为隐藏的帖子(但可以通过直接链接获得)

UPDATE

根据上述建议,这里有一个可能的解决方案。

Create a custom meta box

首先,注册一个自定义元框,创建自定义元框:

function yourtextdomain_add_custom_meta_box() {
  add_meta_box("demo-meta-box", "Custom Meta Box", "yourtextdomain_custom_meta_box_markup", "post", "side", "high", null);
}
add_action("add_meta_boxes", "yourtextdomain_add_custom_meta_box");
将标记添加到元框(本例中为复选框):

function yourtextdomain_custom_meta_box_markup($object) {
  wp_nonce_field(basename(__FILE__), "meta-box-nonce"); ?>
    <div>
      <br />
      <label for="meta-box-checkbox">Hidden</label>

      <?php $checkbox_value = get_post_meta($object->ID, "meta-box-checkbox", true);
          if($checkbox_value == "") { ?>

          <input name="meta-box-checkbox" type="checkbox" value="true">

      <?php } else if($checkbox_value == "true") { ?>

          <input name="meta-box-checkbox" type="checkbox" value="true" checked>

      <?php } ?>

      <p style="color: #cccccc"><i>When selected, the post will be removed from the WP loop but still accessible from a direct link.</i></p>
    </div>
  <?php
}
这将为每个帖子提供一个元框,如下所示:

最后保存元框值:

function yourtextdomain_save_custom_meta_box($post_id, $post, $update) {
    if (!isset($_POST["meta-box-nonce"]) || !wp_verify_nonce($_POST["meta-box-nonce"], basename(__FILE__)))
        return $post_id;

    if(!current_user_can("edit_post", $post_id))
        return $post_id;

    if(defined("DOING_AUTOSAVE") && DOING_AUTOSAVE)
        return $post_id;

    $slug = "post";

    if($slug != $post->post_type)
        return $post_id;

    $meta_box_checkbox_value = "";

    if(isset($_POST["meta-box-checkbox"])) {
      $meta_box_checkbox_value = $_POST["meta-box-checkbox"];
    }
    update_post_meta($post_id, "meta-box-checkbox", $meta_box_checkbox_value);
}
add_action("save_post", "yourtextdomain_save_custom_meta_box", 10, 3);
wp_postmeta 表中,您现在应该看到分配给已检查为隐藏和保存的帖子的元值“true”:

Post Meta Table

Modifying the query with pre_get_posts

现在只需过滤掉那些在主查询中标记为隐藏的帖子。我们可以用pre_get_posts:

add_action( \'pre_get_posts\', \'yourtextdomain_pre_get_posts_hidden\', 9999 );
function yourtextdomain_pre_get_posts_hidden( $query ){

  // Check if on frontend and main query.
    if( ! is_admin() && $query->is_main_query() ) {

    // For the posts we want to exclude.
    $exclude = array();

    // Locate our posts marked as hidden.
    $hidden = get_posts(array(
      \'post_type\' => \'post\',
      \'meta_query\' => array(
        array(
          \'key\' => \'meta-box-checkbox\',
          \'value\' => \'true\',
          \'compare\' => \'==\',
        ),
      )
    ));

    // Create an array of hidden posts.
    foreach($hidden as $hide) {
      $exclude[] = $hide->ID;
    }

    // Exclude the hidden posts.
    $query->set(\'post__not_in\', $exclude);

    }
}

SO网友:swissspidy

在WordPress core中,私人帖子可能是不使用插件就能获得的最接近的帖子。既然你提到你需要将私人帖子的链接发送给其他人,我建议你不要再发明轮子,而是使用一个插件。

例如,我可以强烈推荐Public Post Preview WordPress核心提交人Dominik Schilling(ocean90)。我认为这正是你想要的:

允许您向匿名用户提供链接,以便在发布帖子之前对其进行公开预览。

它可以为您处理所有链接过期信息、功能检查等,这样您就不必担心在使用自定义构建的解决方案时意外暴露太多内容。

SO网友:jetyet47

可能是插件领域,因为正如您提到的私有页面的工作方式,它们只对登录的管理员或编辑器级别的用户可见。

看见https://codex.wordpress.org/Content_Visibility

对您的用例并不完全清楚,但您可以创建一个在索引页上不可见的自定义帖子类型(将public设置为false,不要为其添加存档等),以便只有具有永久链接的人才能看到它。不过,有人可以猜到permalink,所以它不会完全是“私人的”。

https://codex.wordpress.org/Function_Reference/register_post_type#Arguments

SO网友:codiiv

1) 创建非公共的自定义帖子类型(例如“hidden\\u posts”)。

2) 在当前模板中创建自定义模板,并运行自定义$wpdb查询,将post\\u type参数传递给模板中的“hidden\\u posts”

3) 使用刚刚创建的模板创建常规页面。

4) 将(3)中的页面添加到您的robots文本中,并拒绝访问以确保其未被索引!

5) 投票给我最好的答案:)

NB:如果你需要这方面的详细代码,请告诉我!

SO网友:Pratik bhatt

您可以为此类帖子创建一个自定义帖子类型,该类型的帖子只显示给未登录的用户。

在中,您可以自定义单曲。通过在自定义posttype中重写自定义posttype来创建自定义posttype的php文件。php在此您可以检查用户是否未登录,否则可以显示它。

请在下面的评论中告诉我您的观点