【问题标题】:Redirect if a post is in draft status如果帖子处于草稿状态,则重定向
【发布时间】:2012-06-30 16:30:38
【问题描述】:

我有两种自定义帖子类型,分别称为“project”和“project-won”。 如果用户转到处于草稿状态的单个“项目”,我希望他们被重定向到相应的“项目获胜”帖子。

每个帖子都将在两种帖子类型中复制,因此如果有一个“项目”具有以下 URL:
mydomain.com/?project=test-project/ ,那么将是一个带有 URL 的“项目获胜”: mydomain.com/?project-won=test-project/。

我可以在“single-project.php”模板页面的顶部放一些东西吗,例如

<?php if($post->post_status  == "draft"){ 
    //add the redirect here

我不知道最好的方法是什么?

【问题讨论】:

    标签: php wordpress redirect


    【解决方案1】:
    First of all define global variable $post.
    than check the if loop.
    
    global $post;
    if( $post->post_status == 'draft' ) {
    /* Your Redirect code Here */
    }
    
    Try this code may your problem be solved by defining global variable $post.
    

    【讨论】:

    • 谢谢,但我主要想知道重定向代码是什么?
    【解决方案2】:
     global $post;
        if( $post->post_status == 'draft' && is_singular('project') ) {
        wp_redirect( <the_url>, 301 ); 
        }
    

    您需要替换为代码才能找到您的自定义帖子类型的分类。你可以向this寻求帮助

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-27
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      相关资源
      最近更新 更多