Create WordPress Page that redirects to another URL

前端 未结 9 1848
被撕碎了的回忆
被撕碎了的回忆 2021-02-01 02:40

I wanted to create a new WordPress page that is actually a link to another site. The goal is to have the page show up in a list of my pages, but actually send the web user to t

9条回答
  •  攒了一身酷
    2021-02-01 03:15

    Alternately, use a filter.

    Create an empty page in your WordPress blog, named appropriately to what you need it to be. Take note of the post_id. Then create a filter that alters its permalink.

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

    This way the url will show up correctly in the page no funny redirects are required.

    If you need to do this a lot, then think about using the custom postmeta fields to define a postmeta value for "offsite_url" or something like that, then you can create pages as needed, enter the "offsite_url" value and then use a filter like the one above to instead of checking the post_id you check to see if it has the postmeta required and alter the permalink as needed.

提交回复
热议问题