我的 wp 部落格有一個名為“rules_and_laws”的自訂帖子類型,並且我添加了一個函數來指定重寫規則:
function custom_rules_and_laws_permalink_structure() { add_rewrite_rule( '([^/]+)/?$', 'index.php?rules_and_laws=$matches[1]', 'top' ); } add_action( 'init', 'custom_rules_and_laws_permalink_structure' );
該規則適用於自訂貼文類型“rules_and_law”,唯一的問題是現在部落格的所有其他頁面和貼文都傳回 404。
我嘗試了另一種方法,嘗試僅對 cpt 'rules_and_laws' 應用重寫 url,但沒有任何效果:
function custom_rules_and_laws_permalink_structure() { // Check if the current post type is 'rules_and_laws' if (is_singular('rules_and_laws')) { add_rewrite_rule( '([^/]+)/?$', 'index.php?rules_and_laws=$matches[1]', 'top' ); flush_rewrite_rules(); // Flush the rewrite rules to apply the changes } } add_action('init', 'custom_rules_and_laws_permalink_structure');
例如,對於此程式碼,規則根本沒有應用(看起來 is_singular('rules_and_laws') 不起作用)。
這是基本網址的範例:
https://website.com?rules_and_laws=art-4-security-regulation
這就是重寫:
https://website.com/art-4-security-regulation
感謝您的幫忙。
P粉5144588632024-01-17 11:43:14
應解決該問題的程式碼更新版本:
function custom_rules_and_laws_permalink_structure() { // Register the rewrite rule only for the 'rules_and_laws' post type add_rewrite_rule( '^rules_and_laws/([^/]+)/?$', 'index.php?rules_and_laws=$matches[1]', 'top' ); // Flush the rewrite rules to apply the changes flush_rewrite_rules(); } add_action('init', 'custom_rules_and_laws_permalink_structure');