个性化阅读
专注于IT技术分析

带有$wp_rewrite的WordPress set_permalink_structure(‘/%postname%/’)

我如何通过functions.php更改永久链接设置(在子主题下为twentytwelve), 使其处于活动状态并且可以工作, 而无需手动进行任何操作?

我想这段代码应该可以工作-但似乎没有..我想我错过了一些东西。

if($run_when_theme_is_activated_and_user_wants_this_permalink_structure){
        global $wp_rewrite;
        $wp_rewrite->set_permalink_structure( '/%postname%/' );
        $wp_rewrite->flush_rules();
}

当我刚刚访问”永久链接设置”页面… wp-admin / options-permalink.php时, 实际上已经选择了”帖子名称”, 并且在测试时它可以正常工作。因此, 我不必保存任何内容, 只需访问此特定页面。解决方案中应跳过必须手动访问永久链接设置的步骤。


#1


这应该在激活主题时运行, 因此仅设置和刷新一次规则

add_action( 'after_setup_theme', 'reset_permalinks' );
function reset_permalinks() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}

#2


使用此代码。它将100%工作。谢谢

/**
 * Rewrite set up, when theme activate i mean
 */
if (isset($_GET['activated']) && is_admin()) {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}

/**
* Redirect to Permalink setting Page.
* Otherwise Redirect rule will not work Properly.
*/
function redirect_to_permalink() {

    wp_redirect('options-permalink.php');
}
add_action( 'after_switch_theme', 'redirect_to_permalink' );
赞(0)
未经允许不得转载:srcmini » 带有$wp_rewrite的WordPress set_permalink_structure(‘/%postname%/’)

评论 抢沙发

评论前必须登录!