2017-10-03 14 views
0

カスタムカテゴリIDでカスタム投稿を取得しようとしています。カテゴリに付いた投稿データ。ただし、投稿カテゴリを1つ選択すると、自動的に他のカテゴリで選択されます。私は別のカテゴリに異なる投稿を割り当てることができません。ここでカスタムカテゴリIDでカスタム投稿を取得する方法

$args = array( 
     'post_type'  => 'portfolio', 
     'posts_per_page' => 5 
    ); 

    $loop = new WP_Query($args,'category_name=categories'); 

    while ($loop->have_posts()) : $loop->the_post(); 
     echo '<div class="col-1-3">'; 
     ?><?php $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), "size");?> 
     <div class="wrap-col"> 
         <div class="item-container"> 
          <a class="fancybox" href="<?php echo $thumbnail[0]; ?>" data-fancybox-group= gallery> 

          <div class="overlay text-center"> 
          <div class="overlay-inner"> 
           <h4>BAKER CANISTER PUMP</h4> 
          </div> 
          </div><img src="<?php echo $thumbnail[0];?>"></a> </div> 
         </div> 
     <?php echo '</div>'; 
    endwhile; wp_reset_postdata();?> 

は私のカスタムtaxonomies.Iが良い、すべてのことをやっていますが、私はwrong.Iは、すべての方法をどのように次のですされませんが、何の成功はI @

function create_portfolio_taxonomies() { 
    $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 Name'), 
     'menu_name'   => __('Categories'), 
    ); 

    $args = array(
     'hierarchical'  => true, // Set this to 'false' for non-hierarchical taxonomy (like tags) 
     'labels'   => $labels, 
     'show_ui'   => true, 
     'show_admin_column' => true, 
     'query_var'   => true, 
     'rewrite'   => array('slug' => 'categories'), 
    ); 

    register_taxonomy('portfolio_categories', array('portfolio'), $args); 
} 
add_action('init', 'create_portfolio_taxonomies', 0); 

答えて

0
<?php 

//loop the names of the slugs for the portfolio_categories 
$terms = get_terms(array ('taxonomy' => 'portfolio_categories', 'hide_empty' => false, 'parent' => 0, 'orderby' => 'date', 'order' => 'DESC')); 
foreach ($terms as $term) { 
    $slug = $term->slug; 

    $posts_args = array(
    'post_type'   => porfolio, 
    'posts_per_page' => -1, 
    'tax_query'   => array(
     'relation' => 'AND', 
     array (
      'taxonomy' => 'portfolio_categories', 
      'field' => 'slug', 
      'terms' => $slug, 
    ) 
    ), 
); 
    $posts_query = new WP_Query($posts_args); 
    if($posts_query->have_posts()): 
    echo '<div id="' . $slug . '"> 
     <h1 class="text-center">' . $term->name . '</h1> 
      while($posts_query->have_posts()): $posts_query->the_post(); 
       echo '<div>' . get_the_content() . '</div>'; 
      endwhile; 
    echo '</div>'; 
    endif; 
    } 
?> 
+0

omukiguyを助けないしなさいですあなたのコードを使っていますが、変更を知っています。私の更新された質問を確認してくださいあなたはあなたの答えを更新することができます –

+0

私はどこかでそれを使用しています。私はそれがどうなるか教えてください。 – omukiguy

+0

omukiguy @はいそれは働いた –

関連する問題