如何从函数中获取帖子的永久链接。php在我的主题中?我知道这个代码:
get_permalink($post->ID);
但这会产生这样的结果:
mydomain.com/index.php?p=123
我需要这样的东西:
mydomain.com/post-name
有可能吗?
我编辑以发布我的代码:
add_filter(\'redirect_post_location\', \'redirect_to_post_on_publish_or_save\');
function redirect_to_post_on_publish_or_save($location)
{
global $post;
if (
(isset($_POST[\'publish\']) || isset($_POST[\'save\'])) &&
preg_match("/post=([0-9]*)/", $location, $match) &&
$post &&
$post->ID == $match[1] &&
(isset($_POST[\'publish\']) || $post->post_status == \'publish\') && // Publishing draft or updating published post
$pl = get_permalink($post->ID)
) {
// Always redirect to the post
$location = "http://mydomain.com/post-type-slug/".$post->post_name;
}
return $location;
}
以下是我的重写规则:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
SO网友:cybmeta
看到您的代码后,我认为您正在尝试在发布或更新已发布的帖子后将用户重定向到帖子。
你的问题不在于get_permalink()
功能或永久链接设置。您的问题在于代码的逻辑。查看如何设置$pl
等于的结果get_permlink
但返回的值是$location
. 此外,您正在设置$pl
内部if
比较语句,这是你不应该做的。
尝试以下操作:
add_filter(\'redirect_post_location\', \'redirect_to_post_on_publish_or_save\');
function redirect_to_post_on_publish_or_save($location) {
global $post;
if (
(isset($_POST[\'publish\']) || isset($_POST[\'save\'])) &&
preg_match("/post=([0-9]*)/", $location, $match) &&
$post &&
$post->ID == $match[1] &&
(isset($_POST[\'publish\']) || $post->post_status == \'publish\') // Publishing draft or updating published post
) {
// Always redirect to the post
$location = get_permalink($post->ID);
}
return $location;
}