【发布时间】:2018-12-11 04:08:52
【问题描述】:
有没有办法在 Wordpress 中为特定内容类型创建备用永久链接?
例如,我想要一个别名 url:
指向现有的永久链接,例如:
如果需要,我不介意编写代码。
【问题讨论】:
标签: wordpress url alias permalinks slug
有没有办法在 Wordpress 中为特定内容类型创建备用永久链接?
例如,我想要一个别名 url:
指向现有的永久链接,例如:
如果需要,我不介意编写代码。
【问题讨论】:
标签: wordpress url alias permalinks slug
玩了一会儿,我想出了一个方法。首先,我在 init 钩子上设置了一个动作。然后我使用下面的代码来确认来自 URL 的 id 是有效的,并重定向到永久链接。
function my_init() {
global $wpdb;
// Using an alias id to redirect to permalink
// split URL path into parts and look at the first part (index 0)
$path_parts = explode("/", substr(wp_parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH), 1));
// make sure we're dealing with an number
if (is_numeric($path_parts[0]))
{
// query the database using the numeric value to see if it corresponds to a post ID of the mp type and its status is publish
$results = $wpdb->get_results("SELECT post_type, post_status FROM wp_posts WHERE ID = $path_parts[0]");
if ($results[0]->post_type == 'mp' && ($results[0]->post_status == 'publish' || is_admin()))
{
// redirect to permalink url
wp_redirect(get_post_permalink($path_parts[0]));
exit;
}
}
}
【讨论】: