0

在我正在構建的當前網站中,我構建了以下功能。在Wordpress中顯示基於選定的自定義分類標準的自定義帖子

當前情況,以更好地理解問題。

有一個頁面叫博客。此頁面顯示列表中的所有博客(帖子)。有一個旁邊所有類別的職位。用戶可以選擇一個類別。一旦用戶點擊它,用戶將轉到category.php並查看具有該特定類別的所有帖子。

我想創建相同的場景,但使用自定義帖子類型。我有一個模板部分; 'offer-list-template.php'

offer-list-template.php(這裏我得到所有的提供並顯示它們);

<?php 
     // set up or arguments for our custom query 
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; 
     $query_args = array(
     'post_type' => 'Offers', 
     'posts_per_page' => 10, 
     'paged' => $paged 
    ); 
     // create a new instance of WP_Query 
     $the_query = new WP_Query($query_args); 
    ?> 

<?php if ($the_query->have_posts()) : while ($the_query->have_posts()) : $the_query->the_post(); // run the loop ?> 
    <?php 

    //$objectData is used in post-listing-item.php 

    $objectData->title = get_the_title($post); 
    $objectData->content = get_the_content($post); 
    $objectData->permalink = get_the_permalink($post); 
    $objectData->thumbnail = get_the_post_thumbnail($post); 
    $objectData->posttype = get_post_type($post); 

    include(locate_template('template-parts/post-listing-item.php')); 
    ?> 
<?php endwhile; ?> 

在同一個文件中有旁邊顯示的類別。 offer_category是分類slu。。

<?php $terms = get_terms('offer_category'); 
    foreach ($terms as $term) { 

     // The $term is an object, so we don't need to specify the $taxonomy. 
     $term_link = get_term_link($term); 

     // If there was an error, continue to the next term. 
     if (is_wp_error($term_link)) { 
      continue; 
     } 

     // We successfully got a link. Print it out. 
     echo '<li><a href="' . esc_url($term_link) . '">' . $term->name . '</a><span>('. $term->count . ')</span></li>'; 
    } 
    ?> 
    </ul 

結果是:

enter image description here

如果用戶點擊它進入種類:分類要約-category.php(分類學slug.php)

這裏我需要顯示具有所選類別的帖子(post_type-> offers)。

登記自定義後類型:

//Register Custom post type for Offers. 
function create_posttype_offer() { 
    $args = array(
    'labels' => array(
     'name' => __('Offers', ''), 
     'singular_name' => __('Offer'), 
     'all_items' => __('All Offers'), 
     'add_new_item' => __('Add New Offer'), 
     'edit_item' => __('Edit Offer'), 
     'view_item' => __('View Offer') 
    ), 
    'public' => true, 
    'has_archive' => true, 
    'rewrite' => array('slug' => 'Offers'), 
    'show_ui' => true, 
    'show_in_menu' => true, 
    'show_in_nav_menus' => true, 
    'capability_type' => 'page', 
    'supports' => array('title', 'editor', 'thumbnail'), 
    'exclude_from_search' => true, 
    'menu_position' => 70, 
    'has_archive' => true, 
    'menu_icon' => 'dashicons-star-filled' 
    ); 
    register_post_type('Offers', $args); 
} 
add_action('init', 'create_posttype_offer'); 

// Register Custom Categoeries for Custom Post Type Offers 
function taxonomies_offer() { 
$labels = array(
    'name'    => _x('Categories', 'taxonomy general name'), 
    'singular_name'  => _x('Category', 'taxonomy singular name'), 
    'search_items'  => __('Search Categories'), 
    'all_items'   => __('All Categories'), 
    'parent_item'  => __('Parent Category'), 
    'parent_item_colon' => __('Parent Category:'), 
    'edit_item'   => __('Edit Category'), 
    'update_item'  => __('Update Category'), 
    'add_new_item'  => __('Add New Category'), 
    'new_item_name'  => __('New Category'), 
    'menu_name'   => __('Categories'), 
    ); 

$args = array(
    'labels' => $labels, 
    'hierarchical' => true, 
    ); 

register_taxonomy('offer_category', 'offers', $args); 
} 
add_action('init', 'taxonomies_offer', 0); 

當我使用默認柱類型和我打電話category.php其具有以下代碼是將顯示與所選擇的類別的文章。但對於自定義帖子類型,我無法找到管理它的方法。

<?php if (have_posts()) : while (have_posts()) : the_post(); // run the loop ?> 
    <?php 

    //$objectData is used in post-listing-item.php 

     $objectData->title = get_the_title($post); 
     $objectData->content = get_the_content($post); 
     $objectData->permalink = get_the_permalink($post); 
     $objectData->thumbnail = get_the_post_thumbnail($post); 
     $objectData->posttype = get_post_type($post); 

     include(locate_template('template-parts/post-listing-item.php')); 
    ?> 
    <?php endwhile; ?> 

這是上市項目後(圖)

<article class="post-item"> 
    <figure> 
     <?php echo $objectData->thumbnail ?> 
    </figure> 

    <div class="content"> 
     <a href="<?php echo $objectData->permalink ?>"> 
      <h2><?php echo $objectData->title ?></h2> 
     </a> 

     <p><?php echo $objectData->content ?></p> 

     <div class="read-more-button"> 
     <a href="<?php echo $objectData->permalink ?>">read more 
     <span> 
      <svg class="next-arrow"><use xlink:href="#next-arrow" /></svg> 
     </span> 
     </a> 
     </div> 
    </div> 
</article> 

回答

0

我找到了!

我從url中獲得了分類和slug,並在查詢中使用它。

<?php 

    $term_slug = get_query_var('term'); 
    $taxonomyName = get_query_var('taxonomy'); 

     $the_query = new WP_Query(array(
     'post_type' => 'offers', 
     'tax_query' => array(
      array (
       'taxonomy' => $taxonomyName, 
       'field' => 'slug', 
       'terms' => $term_slug, 
      ) 
      ), 
     )); 
     ?> 

     <ul> 
      <?php while($the_query->have_posts()) : $the_query->the_post(); ?> 
      <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> 
      <?php endwhile; wp_reset_query(); ?> 
     </ul> 
+0

我剛剛檢索了標題,但我可以從對象中獲得所需的一切。 – Marc

0

通過了解,你必須擁有具備全部自定義分類後由下面的代碼加載查詢:

$terms = get_terms( 
     array(
      'taxonomy' => 'offer_category', 
      'hide_empty' => false, 
      ) 
     ); 

foreach ($terms as $term){ 
    $args = array(
      'post_type' => 'Offers', 
      'tax_query' => array(
           array(
            'taxonomy' => 'offer_category', 
            'field' => 'slug', 
            'terms' => $term->slug, 
           ), 
         ), 
      ); 
     $query = new WP_Query($args); 
     if($query->have_posts()): while($query->have_posts()): $query->the_post(); 

      the_title(); 
      the_content(); 
     endwhile; 
     wp_reset_postdata(); 
     endif; 
} 

希望這對你的作品

+0

我只是覺得,它是接近我有什麼,我不從你的代碼理解的唯一的事情就是你得到的分類兩次?首先在這個術語中,而不是它的tax_query。仍然感謝你的努力! – Marc

相關問題