gpt4 book ai didi

Changing permalinks via functions.php(通过函数更改固定链接。php)

翻译 作者:bug小助手 更新时间:2023-10-26 22:26:09 24 4
gpt4 key购买 nike



I have designed a wordpress theme where I have designed a separate page for users to log in and this page will be accessible if permalinks are selected as %pagename% %category%.

我已经设计了一个WordPress主题,我设计了一个单独的页面供用户登录,如果永久链接被选择为%Pagename%%类别%,则此页面将可访问。


For this, I know that I have to select the permanent link I want through the WordPress settings and from the unique links tab.

为此,我知道我必须通过WordPress设置和唯一链接选项卡选择我想要的永久链接。


But I want to put a piece of code inside the functions.php file so that it will be done automatically after the user activates the WordPress theme and the previously selected links will be disabled.

但我想把一段代码放在函数.php文件中,以便在用户激活WordPress主题后自动完成,并且之前选择的链接将被禁用。


I have used the following code but it did not change the permalink:

我使用了以下代码,但它没有更改固定链接:


/*
* Set permlinks on theme activate
*/
function set_custom_permalinks() {
$current_setting = get_option('permalink_structure');

// Save permalinks to a custom setting, force create of rules file
global $wp_rewrite;
update_option("rewrite_rules", FALSE);
$wp_rewrite->set_permalink_structure('/%postname%/%category%/');
$wp_rewrite->flush_rules(true);
}
add_action('after_switch_theme', 'set_custom_permalinks');



Edit:

编辑:


After a bit of searching, I found out that permalinks can be edited through the database in addition to the WordPress settings.

在搜索了一下之后,我发现除了WordPress设置之外,固定链接还可以通过数据库进行编辑。


To change the permanent links through the database, I went to the following path:

要通过数据库更改永久链接,我转到以下路径:


1- I clicked on wp_options in the database

1-我点击了数据库中的wp_Options


2- Under option_name, I looked for the permalink_structure table.

2-在OPTION_NAME下,我查找了PerMalink_Structure表。


3- After finding the permalink_structure table, I clicked on the edit option.

3-找到PERMALINK_STRUCTURE表后,我点击编辑选项。


4- Then in the option_value field, I entered my permanent link as follows:

4-然后在OPTION_VALUE字段中输入我的永久链接,如下所示:


/%postname%/%category%/

After completing the above steps, the permanent link of my site was changed correctly.

完成上述步骤后,我的网站的永久链接被正确更改。


Now I want a code that can do this for me.

现在我想要一个可以为我做这件事的代码。


更多回答
优秀答案推荐

I achieved what I wanted by using the following code and my site's permalinks were sorted correctly:

我通过使用以下代码实现了我想要的结果,并且我的站点的固定链接被正确排序:


function custom_permalinks() {
global $wp_rewrite;

$wp_rewrite->set_permalink_structure('/%postname%/%category%/');

update_option( "rewrite_rules", FALSE );

$wp_rewrite->flush_rules( true );
}

add_action( 'init', 'custom_permalinks' );

By placing the above code in the functions.php file, the site's permanent links will be arranged as you wish, and the user cannot change your permanent link through the WordPress settings under any circumstances.

通过将上面的代码放在unctions.php文件中,站点的永久链接将按照您的意愿进行排列,并且用户在任何情况下都无法通过WordPress设置更改您的永久链接。



Try this code; it will hopefully work:

试一试这段代码;它有望奏效:


function set_custom_permalinks() {
global $wp_rewrite;

// Define your custom permalink structure here
$custom_permalink_structure = '/%postname%/%category%/';

// Save permalinks to a custom setting
update_option('permalink_structure', $custom_permalink_structure);

// Flush and regenerate rewrite rules
$wp_rewrite->set_permalink_structure($custom_permalink_structure);
$wp_rewrite->flush_rules(true);
}
add_action('after_switch_theme', 'set_custom_permalinks');

更多回答

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com