2016-03-02 85 views
0

我正在嘗試創建一個調用定製的Post Type的Wordpress主題。WordPress的|自定義帖子|顯示內容

我創建通過的functions.php崗位類型。這似乎工作,並顯示在管理區域,並允許我生成帖子。下面的代碼exerpt:

​​

我的問題是如何獲取的內容從崗位在我的自定義主題,以顯示?

我目前卡住,遇到錯誤。下面是我在哪裏高達:

我創建了一個名爲單course.php模板現在來看沒有改變任何東西不同勢到我的主模板。

但是當我去查看瀏覽器後我得到的錯誤信息:

對不起,沒有職位符合您的條件。

在single-course.php中,我將下面的代碼放在<div></div>的內部,我希望顯示內容。

如果它有助於視覺什麼,我想總體實現的是一個自定義後類型(在我的情況下,每個崗位的培訓課程),並顯示每一個人定製後的頁面。同樣,它可能是商店和自定義文章是一個產品,其中我的single-course.php應該顯示一個特定的產品信息內的一個div專用於此目的。

// This div shows the post title using a wordpress php function 
<div class="page-title bg-alpha-moderateblue"> 
<h1 style="margin:0px;"><?php echo get_the_title(); ?></h1> 
</div> 

//This div should show the post or in my case the training course overview 
//From my understading I have to use the loop although I only want to SHOW THE CONTENT OF A PARTICULAR CUSTOM (course) POST. 
<div class="page-content bg-white"> 
<?php 

//Define your custom post type name in the arguments 

$args = array('post_type' => 'courses'); 

//Define the loop based on arguments 

$loop = new WP_Query($args); 

//Display the contents 

while ($loop->have_posts()) : $loop->the_post(); 
?> 
<h1 class="entry-title"><?php the_title(); ?></h1> 
<div class="entry-content"> 
<?php the_content(); ?> 
</div> 
<?php endwhile;?> 
</div> 

所有建設性的意見和建議,都大大appreaciated,在此先感謝

+1

我可能會關閉,但你嘗試過進入設置>永久鏈接,並且只需點擊「保存」按鈕?有時會修復重定向問題。 – coopersita

+1

另外,如果模板命名正確,則不需要自定義循環。默認循環應該知道該怎麼做。 – coopersita

回答

0

我不知道,如果你寫的所有的代碼(從您的functions.php文件)自己或如果你鉤住它從其他地方,但我建議使用盡可能少的定製在https://codex.wordpress.org/Post_Types#Custom_Post_Types示例代碼。畢竟,誰也知道Wordpress比Wordpress更好?你可能還想使用Wordpress'TwentySixteen單一模板(https://github.com/WordPress/twentysixteen/blob/master/single.php)中的代碼?我並不是說在生產中使用代碼只是作爲一個起點,以幫助您消除錯誤出現在這兩個代碼塊中的可能性。

這就是說,你嘗試過重命名單course.php單courses.php?畢竟這是您在register_post_type調用中指定的名稱。

希望幫助,祝你好運!

0

問題使用的代碼生成時,我沒能默認「塞」 =>「post_type」改變我的自定義後類型名稱,在我的情況「課程」解決。

0

在一個側面說明,如果你使用的是單courses.php你不應該需要使用

<?php 

//Define your custom post type name in the arguments 

$args = array('post_type' => 'courses'); 

//Define the loop based on arguments 

$loop = new WP_Query($args); 

//Display the contents 

while ($loop->have_posts()) : $loop->the_post(); 
?> 
<h1 class="entry-title"><?php the_title(); ?></h1> 
<div class="entry-content"> 
<?php the_content(); ?> 
</div> 
<?php endwhile;?> 
</div> 

如果主題模板文件名是否正確,你就應該能夠使用標準的WordPress循環和標記顯示您的內容。

即。不需要調用另一個查詢。

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 

//Content 

<?php endwhile; endif; ?> 

除非您正在修改查詢以獲取單個內的相關內容。

在這種情況下,我會建議使用get_posts();用一個簡單的foreach。

$args = array(
    'post_type' = 'courses', 
    'posts_per_page' = -1, 
); 

<?php $related_posts = get_posts($args); 

if ($related_posts) : ?> 

<?php $temp_post = $post; //temporarily store the $post query for use after the related content 
foreach ($related_posts as $post) : setup_postdata($post); ?> 

    <h3><?php the_title(); ?></h3> 

    <?php the_content(); ?> 

<?php endforeach; 
$post = $temp_post; //set the post back to the main query 
endif; ?> 

Wordpress codex for get_posts

相關問題