WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] category.php post_types divided per category (13 posts)

  1. M Szatkowski
    Member
    Posted 1 year ago #

    Hi
    I'm building category.php
    I'd like to divide posts per category

    example:

    cat1
    - post 1
    - post 2
    - post 3

    cat2
    - post 4
    - post 5
    - post 6

    I've done the code:

    <?php
                $cats = get_categories('taxonomy=katalog-produktow');
                    foreach ($cats as $cat) {
                        $cat_id= $cat->term_id;
                        echo "<h2>".$cat->name."</h2>";  
    
                        $args = array(
    						'tax_query' => array(
    						array(
    							'post_type' => 'products',
    							'field' => 'slug',
    							'terms' => ".$cat->name."
    							)
    							)
    						);
    					$query = query_posts( $args );
                        if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    				<div id="gallery" class="one_column">
    
       				 <ul class="portfolio">
    
       				 <li class="clearfix">
               			 <div class="clearfix">
                    	<span class="image-border"><a class="image-wrap" href="<?php the_permalink() ?>" title="<?php _e('Link to', 'theme1512');?> <?php the_title_attribute(); ?>" ><?php the_post_thumbnail( 'portfolio-post-thumbnail-xl' ); ?></a></span>
                   			 <div class="folio-desc"> 
    
    						 <a href="<?php the_permalink();?>"><?php the_title(); ?> | <?php echo $price; ?></a><br />
                            <p><?php echo $short_text; ?></p>
                            <p><a href="<?php the_permalink(); ?>">szczegoly</a></p>
    
                            </div>
                		</div>
            		</li>
            		</div>
    
                        <?php endwhile; endif; ?>
                        <?php wp_reset_query(); ?>
                    <?php }  ?>

    But it doesn't show up.

    Do you know any simpler solution?

    HELP!

  2. deepbevel
    Member
    Posted 1 year ago #

    Do your category names show up?
    I just got a list of categories with no posts..

    I'll work with it, I know another member working on a similar issue.
    Although I have a feeling the solution won't be simpler.

  3. M Szatkowski
    Member
    Posted 1 year ago #

    no - I just can see a blank page ;(

    thank you for your time.

  4. deepbevel
    Member
    Posted 1 year ago #

    this worked for me, ashamed to say I don't know why.

    <?php
    //get lastest post in each of 2 categories
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
      'include' => '3,36',
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 1,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  5. deepbevel
    Member
    Posted 1 year ago #

    But you'll have to add your taxonomy and post type..

  6. M Szatkowski
    Member
    Posted 1 year ago #

    Great, but I have certain category (katalog_produktow) for post_types (products). And I want to list all subdirectories divided by the category.

    Do I need to swap:

    1. $taxonomy = 'category';// e.g. post_tag, category
    to
    $taxonomy = 'katalog-produktow';// e.g. post_tag, category

    2. 'post_type' => 'post',
    to
    'post_type' => 'products',

    ?

  7. deepbevel
    Member
    Posted 1 year ago #

    I beleive so.. I'm not using a cusotm taxonomy so I can't test

  8. M Szatkowski
    Member
    Posted 1 year ago #

    It almost worked for me but displays all post types and categories.
    I need to display only current category subcategories and posts types:

    <?php
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
    
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'products',
          'post_status' => 'publish',
          'posts_per_page' => 10,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>

    Thanks up front!

  9. M Szatkowski
    Member
    Posted 1 year ago #

    I added
    'cat' => $cat_ID, to

    $args=array(...}

    but it still does not work...

    <?php
    $taxonomy = 'category';//  e.g. post_tag, category
    $param_type = 'category__in'; //  e.g. tag__in, category__in
    $term_args=array(
    
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'products',
          'post_status' => 'publish',
          'posts_per_page' => 10,
          'cat' => $cat_ID,
          'caller_get_posts'=> 1
    
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
  10. deepbevel
    Member
    Posted 1 year ago #

    did you include "has_archive" in the params when you registered you custompost types?

    see this

  11. deepbevel
    Member
    Posted 1 year ago #

    you can also get all posts in the category of the current post outside the loop, I use this in a sidebar.

    <ul>
    <?php
    $IDOutsideLoop = $post->ID;
    while( have_posts() ) {
    	the_post();
    	foreach( ( get_the_category() ) as $category )
    		$my_query = new WP_Query('category_name=' . $category->category_nicename . '&orderby=title&order=asc&showposts=100');
    	if( $my_query ) {
    		while ( $my_query->have_posts() ) {
    			$my_query->the_post(); ?>
    			<li<?php print ( is_single() && $IDOutsideLoop == $post->ID ) ? ' class="test"' : ''; ?>>
    				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
    			</li>
    <?php
    		}
    	}
    }
    ?>
    </ul>
  12. deepbevel
    Member
    Posted 1 year ago #

    If you are not using a custom taxonomy for your custom post type, I tested a solution which allows you to use your standard wp category archive:

    in functions.php

    function namespace_add_custom_types( $query ) {
      if( is_category() || is_tag() && empty( $query->query_vars['suppress_filters'] ) ) {
        $query->set( 'post_type', array(
         'post', 'your-custom-post-type-here'
    		));
    	  return $query;
    	}
    }
    add_filter( 'pre_get_posts', 'namespace_add_custom_types' );

    then add:

    'taxonomies' => array( 'post_tag', 'category')

    to the array where you registered your custom post type in functions.php.

    now when you access a category archive it will include entries of your custom post type, as well any other post entires in that category.
    Hope this helps!

  13. deepbevel
    Member
    Posted 1 year ago #

    use this function instead of the one above, otherwise you loose your nav menu on category pages:

    add_filter('pre_get_posts', 'query_post_type');
    function query_post_type($query) {
      if(is_category() || is_tag()) {
        $post_type = get_query_var('post_type');
    	if($post_type)
    	    $post_type = $post_type;
    	else
    	    $post_type = array('nav_menu_item','post','your-custom-post-type-here');
        $query->set('post_type',$post_type);
    	return $query;
        }
    }

    just add your post type in the array.

Topic Closed

This topic has been closed to new replies.

About this Topic