2016-11-20 33 views
0

我已經創建了四種自定義帖子類型 - 視頻,音頻,程序,博客 - 具有各種自定義字段,並且我設法創建了一個自定義存檔頁面,根據帖子類型顯示特定的自定義字段。當僅查看某個自定義帖子類型的存檔時,存檔頁面運行良好。我遇到的問題是歸檔頁面的類別。Wordpress:如何在類別歸檔頁面中包含使用自定義帖子類型UI(CPT UI)製作的自定義帖子類型?

每個自定義帖子類型都可以訪問全球'帖子'類別,因此我的所有類別都顯示在每個帖子類型中。我希望能夠查詢某個類別並顯示相關的帖子,而不管帖子類型如何。即「商業」類別可能拉起兩個視頻,一個音頻帖子和一個博客。問題是我的分類頁面現在是空的。

這裏是 'archive.php' 我的電流回路:

​​

這抓住歸檔內容 '內容archive.php' 模板:

<?php 
/** 
* Get featured posts from Archives 
*/ 

if(is_post_type_archive('videos')) { 

    $video_image = get_field('video_still_image'); 
    print_archive_post($video_image); 

} elseif(is_post_type_archive('programs')) { 

    $program_featured_image = get_field('program_featured_image'); 
    print_archive_post($program_featured_image); 

} elseif(is_post_type_archive('audio')) { 

    $audio_featured_image = get_field('audio_featured_image'); 
    print_archive_post($audio_featured_image); 

} elseif(is_post_type_archive('blogs')) { 

    $blog_featured_image = get_field('blog_featured_image'); 
    print_archive_post($blog_featured_image); 

} 

?> 

而且,這裏是我的從「的functions.php」功能打造爲文章內容:

// Function to print archive type posts 
function print_archive_post($image) { 

    echo '<div class="col-md-4 featured-thumb-container">'; 
    echo '<a href="' . get_the_permalink() . '"><span class="featured-thumb" style="background: url(' . $image . ')"></span></a>'; 
    echo '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>'; 

    // The category list 
    $post_cats= array(); 
    $categories = get_the_category(); 

    echo '<p class="category-list">'; 

     foreach($categories as $cat) : 
     echo '<a class="category-link" href="' . get_home_url() . '/category/' . $cat->slug . '">' . $cat->cat_name . '</a><span>,</span> '; 
     endforeach; 

    echo '</p>'; 

    echo '</div>'; 

} 

我知道我缺少一個條件檢查美食血腥,但我一直無法找到我的研究解決方案。任何幫助將不勝感激。 :)

+0

爲什麼不爲每個帖子類型創建自定義分類標準? – ucheng

+0

@ucheng感謝您的回覆!我創建不同分類法的問題是冗餘。在這種情況下,將會有四個「商業」類別 - 每個類別都有一個。有沒有辦法拉出與一個類別相關的所有自定義帖子類型? –

+0

我添加了一個答案,不知道我是否理解正確。如果沒有,請讓我知道,我會更新答案。 – ucheng

回答

0

因此,經過一番研究,我找到了解決方案。我需要將我的自定義帖子類型添加到Wordpress的內置類別和標籤中。下面是我添加到我的functions.php文件中的代碼:

// Add custom post types to default WP categories and tags 
function add_custom_types_to_tax($query) { 
    if(is_category() || is_tag() && empty($query->query_vars['suppress_filters'])) { 

     // Get all your post types 
     $post_types = get_post_types(); 

     $query->set('post_type', $post_types); 
     return $query; 
    } 
} 
add_filter('pre_get_posts', 'add_custom_types_to_tax'); 

我在這裏找到的代碼:https://premium.wpmudev.org/blog/add-custom-post-types-to-tags-and-categories-in-wordpress/

此外,我創造了只分類存檔一個category.php文件。我使用了與我的archive.php文件相同的代碼,但是我換了一個新的模板文件,用於分類內容:'content-category-archive.php'。下面是該模板的代碼:

<?php 
/** 
* Get featured posts from Category Archives 
*/ 

if(get_post_type() == 'videos') { 

    $video_image = get_field('video_still_image'); 
    print_archive_post($video_image); 

} elseif(get_post_type() == 'programs') { 

    $program_featured_image = get_field('program_featured_image'); 
    print_archive_post($program_featured_image); 

} elseif(get_post_type() == 'audio') { 

    $audio_featured_image = get_field('audio_featured_image'); 
    print_archive_post($audio_featured_image); 

} elseif(get_post_type()== 'blogs') { 

    $blog_featured_image = get_field('blog_featured_image'); 
    print_archive_post($blog_featured_image); 

} 

?> 

這和我的其他「內容archive.php」模板之間的區別是有條件的每個帖子類型。而不是檢查is_post_type_archive('my custom post type')我正在檢查get_post_type() == 'my custom post type'當前類別中每個帖子的帖子類型。

我希望這可以幫助其他人可能有同樣的問題。 :)

1

不知道我理解你的問題是否正確。如果要查詢與一個類別相關的所有自定義帖子,可以在存檔頁面中使用WP_Query

//if you're on a category archive, it will return the category object 
    $category_object = get_queried_object(); 
    $args = array(
     'post_type' => 'your-cpt', 
     'cat' => $category_object->term_id 
); 

    $wp_query = new WP_Query($args); 

    if ($wp_query->have_posts()) { 

    while ($wp_query->have_posts()) { 
     $wp_query->the_post(); 
     //echo your posts 
    } 

    } else { 
     // aww, no posts... do other stuff 
    } 
    wp_reset_postdata(); 
+0

感謝您的解決方案,但不幸的是它沒有奏效。但是,我能夠找到解決方案併發布。 –

相關問題