2016-12-27 46 views
1

我正在使用WooCommerce添加产品页面计算产品的自定义标题。用户发布产品信息后,标题将生成并通过save_post过滤器钩子进行保存。WordPress - 发布更新后自动更新固定链接

add_filter('save_post', 'modify_post_title', '99', 1); 
function modify_post_title($post_id) 
{ 
    // some logic to form a new $title 
    // ... 

    if (!empty($title)) { 
     // update the title in database 
     $wpdb->update($wpdb->posts, array('post_title' => $title), array('ID' => $post_id));  

     // UPDATE PERMALINK 
    } 
} 

我需要知道使用什么功能来重新生成更新标题后的永久链接。

在此先感谢

回答

0
add_filter('wp_insert_post_data', 'custom_slug_change', 50, 2); 
function custom_slug_change($data, $postarr) { 
    //Check for the post statuses you want to avoid 
    if (!in_array($data['post_status'], array('draft', 'pending', 'auto-draft'))) {   
     $data['post_name'] = sanitize_title($data['post_title']); 
    } 
    return $data; 
} 

请您在您的functions.php添加上面的代码?

+0

当现有的职位时,它的工作完美。但是对于新的帖子,它不起作用。 – Hamid

+0

@哈米德我们检查了一下,它工作正常。你能重新检查一下吗? – purvik7373

+0

您是否意味着当您创建新产品并保存时?那么你必须改变产品标题和你的产品永久链接没有改变?我对吗? – purvik7373

-1

我想你可以用window.history.pushState去maipulate浏览器历史记录。

我认为这些可能会对您有所帮助。

window.history.pushState("object or string", "Title", surl[0]); 
+0

什么时候该调用这个javascript?有没有任何PHP代码可以做到这一点? – Hamid