2015-09-05 36 views
1

我需要獲取cuurent頁面類型。在wordpress中獲取當前頁面類型

Whene我使用這個func的任何其他功能的作品!

function get_current_page_name(){ 
    if (is_home() || is_front_page()) 
     return 'is_home'; 
    else 
     return 'is_page'; 
} 

但whene我在主頁上使用這樣的

function my_function(){ 
    echo get_current_page_name(); /// returns 'is_page' 
} 

此功能與阿賈克斯

回答

0

我檢查調用,它似乎爲我工作。

你能指定你使用的是什麼版本的WordPress嗎?有沒有安裝插件?你究竟把這個代碼放在哪裏?

在我的測試案例中,我將其添加到我的functions.php文件中。

function get_current_page_name(){ 
    if (is_home() || is_front_page()) 
     return 'is_home'; 
    else 
     return 'is_page'; 
} 

function my_function(){ 
    echo get_current_page_name(); 
} 

而這對我的index.php文件。

<?php 
    my_function(); 
?> 

This Works。

如果我會選擇主頁,在WordPress設置一些靜態的頁面,並把相同的,但在page.php文件時,它仍然有效,並返回is_home

+0

它是AJAX代碼 - 這是在functions.php的 –

+0

您需要提供您正在使用的更具體的代碼。此處顯示的代碼運行良好。問題在你的代碼中的其他地方。請用你的具體用例來擴展你的問題。 –