2017-02-15 38 views
1

我在掙扎着一件事。我有這樣的WordPress的功能:如何使wordpress功能只能在單頁和非頁面上工作

function wpq_insert_attachment_data($data, $postarr){ 
if (!is_single()) { 
    $posttitle = get_the_title($postarr['post_parent']); 
    $data['post_title'] = $posttitle; 
    $data['post_name'] = $posttitle;  
    return $data;   
}} 

add_filter('wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2); 

它的工作原理,但高超它涵蓋所有單/自定義後類型/頁等有沒有辦法排除該功能的網頁?我試着用is_single()整理出來,但沒有成功。

+0

在這裏看到:http://stackoverflow.com/questions/22070223/how-can-i-use-is-page-inside-a-plugin 比添加掛鉤重定向功能 – Bart

+0

@Bart - 它不起作用。它仍然執行所有類型的元素:posts/pages ..看看更新後的代碼。 –

+0

你可以粘貼你的新代碼嗎? – Bart

回答

0

負荷僅在特定頁面中的功能,添加頁面ID is_page(id_here)

function wpq_insert_attachment_data($data, $postarr){ 
    if (is_page(page_id)){ 
     $posttitle = get_the_title($postarr['post_parent']); 
     $data['post_title'] = $posttitle; 
     $data['post_name'] = $posttitle;  
     return $data; 
    } 

    add_filter('wp_insert_attachment_data', 'wpq_insert_attachment_data', 10, 2); 

你也可以添加的,而不是ID喜歡你的頁面毛坯:

if (is_page('slug')) 
+0

但頁面實際上是我想排除的元素(所以我想執行除了頁面以外的任何地方的功能) –

+0

然後嘗試使用'!is_page()' –

+0

該功能仍適用於所有類型的元素)。 –

0

看起來像你只需要調整您的條件聲明:

if (!is_single()) {

應該改爲:

if (!is_page()) {

+0

無法正常工作。我發現在其他論壇上的線程,在我的目的functions.php is_page等無法正常工作,我應該使用get_post_type($ postarr ['post_parent'])。但它仍然沒有告訴我:( –

相關問題