2013-12-16 30 views
0

即時通訊新的WordPress的。WordPress的自定義內容的URL匹配沒有帖子或頁面

我想使用兩個表格並顯示一些內容而不創建頁面或帖子。

匹配的網址將是這樣的:

/[category]/[image] - /nature/green-tree

我的方法是檢查從主題的index.php URL和分裂的URL,只是建立一個小型的路由系統,爲畫廊。但我認爲這不是最聰明的想法。 我不想使用插件庫插件,因爲我已經在使用插件了,這是我需要做的一項改變。 這樣做的最好方法是什麼?

+0

我個人會使用一個插件庫插件,這樣WordPress可以管理頁面和URL以及圖片元數據。 – DrCord

回答

0

我想你需要自定義重寫規則。這是你如何自己做的。

如果您使用的是主題,請打開functions.php文件並輸入以下代碼,否則,如果您使用的是插件,請將此代碼放置在插件的某個位置,但請確保它立即加載。

function registerCustomUrl($rewrite) 
{ 
    $rewrites  = $rewrite->rules; 
    $newRewrites = array(); 

    $newRewrites['([^/]+)/([^/]+)/?$'] = 'index.php?gallery_category=$matches[1]&gallery_image=$matches[2]'; 

    foreach($rewrites as $rk => $rv) 
    { 
     $newRewrites[$rk] = $rv; 
    } 

    $rewrite->rules = $newRewrites; 
} 
add_action('generate_rewrite_rules', 'registerCustomUrl'); 

function registerCustomQueryVars($vars) { 
    $vars[] = 'gallery_category'; 
    $vars[] = 'gallery_image'; 
    return $vars; 
} 
add_filter('query_vars', 'registerCustomQueryVars'); 

function myCustomTemplateRedirect() 
{ 
    global $wp_query; 

    if(
     isset($wp_query->query_vars['gallery_category']) && 
     !empty($wp_query->query_vars['gallery_category']) && 
     isset($wp_query->query_vars['gallery_image']) && 
     !empty($wp_query->query_vars['gallery_image']) 
    ) 
    { 
    // Within your custom template file you are able to 
     // use any theme function like the get_header(), get_footer(), etc 
     require_once('path/to/your/custom/template/file.php'); 
     exit(0); 
    } 
} 
add_action("template_redirect", 'myCustomTemplateRedirect'); 
相關問題