2016-11-20 7 views
0

다양한 맞춤 필드가있는 네 개의 맞춤 게시물 유형 (동영상, 오디오, 프로그램, 블로그)을 만들었으며 게시 유형에 따라 특정 맞춤 입력란을 표시하는 맞춤 보관 파일 페이지를 만들었습니다. 아카이브 페이지는 특정 사용자 정의 게시물 유형의 아카이브 만 볼 때 잘 작동합니다. 내가 가지고있는 문제는 카테고리에 대한 아카이브 페이지입니다.Wordpress : 카테고리 보관 페이지에 맞춤 포스트 유형 UI (CPT UI)로 만든 맞춤 게시물 유형을 포함하려면 어떻게해야합니까?

각 맞춤 게시물 유형은 글로벌 '게시'카테고리에 액세스 할 수 있으므로 모든 카테고리가 각 게시물 유형에 표시됩니다. 게시물 유형에 관계없이 카테고리를 쿼리하고 관련 게시물을 표시하고 싶습니다. 즉 '비즈니스'카테고리는 두 개의 동영상, 오디오 게시물 및 블로그를 가져올 수 있습니다. 문제는 지금 내 카테고리 페이지가 비어 있다는 것입니다. 이 아카이브 컨텐츠 '내용 archive.php'에 대한 템플릿 잡고

<?php 
if (have_posts()) : ?> 


<div class="container"> 

    <div class="row"> 

     <?php 
     /* Start the Loop */ 
     while (have_posts()) : the_post(); 

      get_template_part('template-parts/content-archive', get_post_format()); 

     endwhile; 

     the_posts_navigation(); ?> 

    </div><!-- .row --> 

</div><!-- .container --> 

<?php endif; ?> 

: 여기

은 '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

해결책 주셔서 감사합니다.하지만 불행히도 문제가 해결되지 않았습니다. 그러나 솔루션을 찾아서 게시 할 수있었습니다. –