2014-11-21 71 views
1

我已經在函數內部創建了一個自定義文章類型,並通過ACF添加了一些內容。 我首先嚐試創建一個archive-forfragningar.php,但它不起作用。 然後我的index.php裏面我加入PHP如果是,但我不能指定我的自定義後類型與is_post_type_archive。當我去我的檔案頁,我得到,而不是代碼如果你想呈現的自定義特定歸檔頁面,裏面是其他的index.php

my custome post type如何爲我的自定義類型製作模板?

function create_forfragningar_cpt() { 
    register_post_type('forfragningar', 
    array(
     'public' => true, 
     'has_archive' => true, 
     'menu_position' => 5, // places menu item directly below Posts 
     'menu_icon' => '', 
     'labels' => array(
     'name' => __('Förfrågningar'), 
     'singular_name' => __('Förfrågning'), 
     'add_new' => __('Lägg till'), 
     'add_new_item' => __('Lägg till nytt Förfrågning'), 
     'edit' => __('Redigera'), 
     'edit_item' => __('Redigera Förfrågning'), 
     'new_item' => __('Nytt Förfrågning'), 
     'view' => __('Visa Förfrågning'), 
     'view_item' => __('Visa Förfrågning'), 
     'search_items' => __('Sök igenom förfrågningar'), 
     'not_found' => __('Inga förfrågningar hittade'), 
     'not_found_in_trash' => __('Inga förfrågningar hittade i Papperskorgen'), 
     'parent' => __('Föräldrer av Förfrågning'), 
     ), 
    ) 
); 
} 
add_action('init', 'create_forfragningar_cpt'); 


index.php

<?php if(is_front_page()){ ?> 

//code 

<?php } if (is_post_type_archive('forfragningar')) { ?> 

<h1>Test</h1> 
//code 

<?php } else { ?> 

//code 

<?php } ?> 
+0

你的意思是用'然後我的index.php裏面我加入PHP的,如果是,但我可以不要使用is_post_type_archive作爲我的自定義帖子類型。「您的問題是什麼?你能提出更具體的問題嗎? – thanassis 2014-11-21 12:05:54

+0

對不起,我的不好解釋。 所以我想在我的自定義帖子類型頁面上輸出自定義帖子類型的內容。 所以這就是爲什麼我添加<?php} if(is_post_type_archive('forfragningar')){?> in index.php 但是當我看着我的自定義帖子類型頁面(www.mydomain.com/forfragningar),我可以除了<?php} else {?>中的代碼之外什麼也看不到。 – 2014-11-21 12:17:31

回答

1

帖子類型您需要了解Template Hierarchy作品。我也用這個image

你的情況,你必須創建archive-forfragningar.phpsingle-forfragningar.php並插入這些文件中的代碼。看看這個也是。
你不需要if (is_post_type_archive('forfragningar'))您的模板內,因爲WordPress是已經到這個自定義類型。您可以使用is_post_type_archive('forfragningar'),非forfragningar自定義後類型的模板文件,以檢查是否你在你的自定義文章類型的查詢,並作出具體工作到你的情況。
例如,要呈現<h1> tiltle </h1>到你的頭在你的forfragningar頁。你會做什麼是:

插在你的header.php

if (is_post_type_archive('forfragningar')) { ?> 

<h1>Test</h1> 
//code 

<?php } else { ?> 

//code 

<?php } ?> 

,然後在您的自定義模板類型渲染這個get_header(); 什麼這就是創造你的頭爲你的自定義類型和你的頭你當wordpress運行您的forfragningar類型時,標題將運行特定的代碼。

+0

你好夥伴。 我確實嘗試過在開始時,但它沒有奏效。必須將永久鏈接更改爲/%category%/%postname%並移動一個具有$ wp_rewrite-> flush_rules()的函數;低於帖子類型的功能。因爲每次我嘗試去歸檔或單頁時,我都會得到404。但是現在看起來似乎一切正常。 感謝您的幫助 – 2014-11-27 06:29:12

3

你可以做到這一點很容易在這條路上, 只是複製single.php和重命名爲single-custom-post-type-name.phpsingle-cars.php,與歸檔或分類, taxonomy-taxonomy-name.phparchive-taxonomy-name.php

+1

Hello mate。 我確實嘗試過在開始時,但它沒有奏效。必須將永久鏈接更改爲/%category%/%postname%並移動一個具有$ wp_rewrite-> flush_rules()的函數;低於帖子類型的功能。因爲每次我嘗試去歸檔或單頁時,我都會得到404。但是現在看起來似乎一切正常。 Hvala za pomoc。 :) – 2014-11-27 06:25:24

1

同樣的事情,嘗試下面的代碼。使PHP文件中像single-{post_type}.php

現在終於你的文件名是single-forfragningar.php

Make文件single-forfragningar.php到根目錄

+0

你好夥伴。 我確實嘗試過在開始時,但它沒有奏效。必須將永久鏈接更改爲/%category%/%postname%並移動一個具有$ wp_rewrite-> flush_rules()的函數;低於帖子類型的功能。因爲每次我嘗試去歸檔或單頁時,我都會得到404。但是現在看起來似乎一切正常。 感謝您的幫助。 :) – 2014-11-27 06:26:44

相關問題