【问题标题】:How to redirect the post/page id by giving custom redirect url in wordpress如何通过在 wordpress 中提供自定义重定向 url 来重定向帖子/页面 ID
【发布时间】:2014-05-03 06:16:06
【问题描述】:

在我的 wordpress 主题中,我包含了标题、描述和重定向部分的元框。我在每个帖子和页面 ID 下都有一个重定向框的输入字段。如果我将任何 url 添加到该重定向框的特定页面/帖子 id,则特定页面/帖子 id 应重定向到该重定向框中给定的我自己的 url。有什么功能可以做到这一点吗?我在 stackies 中进行了搜索,得到了以下代码。但它不起作用。

function my_permalink_redirect($permalink) {
    global $post;
    if ($post->ID == your_post_id_here) {
        $permalink = 'http://new-url.com/pagename';
    }
    return $permalink;
}
add_filter('get_the_permalink','my_permalink_redirect');

【问题讨论】:

    标签: php wordpress redirect


    【解决方案1】:

    试试:

    add_filter( 'the_permalink', 'filter_function_name_7062', 10, 2 );
    
    function filter_function_name_7062( $permalink, $post ){
    
      global $post;
    
      if ($post->ID == 684) {
    
        $permalink = 'http://sample.com/1';
      }
    
      if ($post->ID == 444) {
    
        $permalink = 'http://sample.com/2';
      }
    
      return $permalink;
    }
    

    【讨论】:

      【解决方案2】:

      试试:

      function my_permalink_redirect($permalink) {
           global $post;
           if ($post->ID == your_post_id_here) {
              $permalink = 'http://new-url.com/pagename';
              wp_redirect("'.$permalink.'", 301); 
              exit; 
           }
      
      }
      add_filter('get_the_permalink','my_permalink_redirect');
      

      【讨论】:

      • 它不起作用..我已将代码粘贴到我的 functions.php 文件中。
      • 现在检查,刚刚从出口中删除了()!
      • 我已经用额外的引号和 301 重定向参数更新了代码,请立即尝试。
      • 它应该可以工作,请确保您指定了正确的帖子 ID,并尝试使用和不带 ''(单引号)的帖子 ID。
      • localhost/testing/?p=1。这是我的链接。这里 1 是我的帖子 ID。所以我将 if ($post->ID == your_post_id_here) 更改为 if ($post->ID == 1).. 但它没有重定向..
      猜你喜欢
      • 2020-10-24
      • 2015-04-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多