【问题标题】:How to create a redirect for WordPress posts with PHP?如何使用 PHP 为 WordPress 帖子创建重定向?
【发布时间】:2018-06-17 04:49:54
【问题描述】:

https://github.com/markjaquith/page-links-to

这是一个简单的 WordPress 插件,可在您单击帖子到自定义页面时重定向。

如何使用此函数或wordpress中的函数将帖子ID更改为带有PHP函数的重定向链接(基本上跳过用户界面)?

我会为这个插件使用这些功能之一吗(在同一页面或新标签中打开)?

或者是否有另一种方式我不必使用这个插件或它的功能并使用 wordpress 内置功能?

function set_link( $post_id, $url ) {
        return $this->flush_links_if( (bool) update_post_meta( $post_id, self::LINK_META_KEY, $url ) );
}


    /**
     * Tell an custom URL post to open in a new tab
     *
     * @param int $post_id post ID
     * @return bool whether anything changed
     */
function set_link_new_tab( $post_id ) {
        return $this->flush_targets_if( (bool) update_post_meta( $post_id, self::TARGET_META_KEY, '_blank' ) );
}

【问题讨论】:

  • 你能提供一个你想做的例子吗,因为有很多方法可以解释这个问题。
  • 我想让post id ie 27去www.example.com/folder
  • 每个帖子的永久链接,但使用函数来完成

标签: php wordpress


【解决方案1】:

您应该可以使用wp_redirect() 函数来执行此操作。

你的例子:

function example_redirect() {
    if ( is_page( 27 ) ) {
        wp_redirect( 'www.example.com/folder' );
        die();
    }
}
add_action( 'template_redirect', 'example_redirect' );

【讨论】:

  • 这适用于页面,但似乎不适用于帖子
  • is_page 不适用于帖子,您需要为此使用is_single()。因此,如果您需要检查其中任何一个,if 语句将如下所示:if ( is_page( 27 ) || is_single( 27 ) )
【解决方案2】:

您可以使用 WordPress 中内置的 get_permalink 函数将 Wordpress 帖子 ID 转换为永久链接。文档:https://developer.wordpress.org/reference/functions/get_permalink/

【讨论】:

  • 你能给我一个如何使用它的编码示例吗?
猜你喜欢
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-01
  • 2016-05-13
  • 2016-12-17
相关资源
最近更新 更多