如何通过自定义类别ID获取自定义帖子

时间:2017-10-03 11:33:12

标签: php wordpress

我正在尝试按自定义类别ID获取自定义帖子.Post数据随附类别。但是当我选择一个类别进行发布时,它会自动选择其他类别。问题是我无法将不同的帖子分配给不同的类别。

$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();?>

这是我的自定义分类法。我做的一切都很好,但我没有错。我遵循所有方法但没有成功。请帮助

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 );

1 个答案:

答案 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;
  }
?>