WordPress.org

Ready to get started?Download WordPress

Forums

List categories and posts per category (22 posts)

  1. obidos
    Member
    Posted 2 years ago #

    Hi all,

    I am trying to list all posts within each category.

    Category name
    - All posts for the above category

    Another category name
    - All post for the above category

    Here is the code I have and it lists the categories but not the posts!

    <ul>
    	<?php
    
        $args = array
        (
            'post_type'		=> 'products',
            'orderby'		  => 'menu_order',
            'order'			=> 'ASC',
            'posts_per_page'	=> -1,
            'category'	     => 'productcat'
        );
    
        $fileposts = get_posts($args);
    
        if (count($fileposts) > 0)
        {
            foreach ($fileposts as $post)
            {
                setup_postdata($post);
    
                $pdfs = get_children( array(
                    'post_type' => 'attachment',
                    'post_parent' => get_the_ID(),
                    'post_mime_type' => 'application/pdf'
                ));
                foreach( (array) $pdfs as $pdf ) {
    
                $attachmenturl=wp_get_attachment_url($pdf->ID);
                $file_title = $pdf->post_title;
        ?>
        <li><a href="<?php echo $attachmenturl; ?>" target="_blank"><?php echo $file_title; ?></a></li>
        <?php
                }
            }
        }
        ?>
    </ul>
  2. deepbevel
    Member
    Posted 2 years ago #

    This works, gets category and posts in category. I'll try it with your args next.

    <?php
    query_posts ('cat=1 &posts_per_page=20'); ?>
     <?php the_category();?>
    
     <?php if ( have_posts() ) : ?>
     <?php while (have_posts()) : the_post(); ?>
    
    <a href="<?php the_permalink() ?>">
    <?php the_title(); ?> </a></p>
    <?php endwhile;?>
    <?php endif;?>
  3. deepbevel
    Member
    Posted 2 years ago #

    may be too difficult to test as I don't have post types.

  4. obidos
    Member
    Posted 2 years ago #

    Do you need me to give you the functions file that sets up the custom post type and custom taxonomies?

    Thank you for taking the time to help

  5. obidos
    Member
    Posted 2 years ago #

    Here is the function.php part that creates a custom post type called products and a custom taxonomy called productscat:-

    // CUSTOM POST TYPES //
    add_action('init', 'feature_init');
    function feature_init()
    {
    	//Default arguments
    	$args = array
    	(
    		'public' 				=> true,
    		'publicly_queryable'	=> true,
    		'show_ui' 				=> true,
    		'query_var' 			=> true,
    		'rewrite' 				=> true,
    		'capability_type' 		=> 'post',
    		'has_archive' 			=> true,
    		'hierarchical' 			=> false,
    		'menu_position' 		=> NULL,
    	);
    
    	/* ----------------------------------------------------
    	PRODUCTS
    	---------------------------------------------------- */
    
    	$labels = array
    	(
    		'name' 					=> 'Products',
    		'singular_name' 		=> 'Products',
    		'add_new' 				=> 'Add Products',
    		'add_new_item' 			=> 'Add New Products',
    		'edit_item' 			=> 'Edit Product',
    		'new_item' 				=> 'New Product',
    		'view_item' 			=> 'View Products',
    		'search_items' 			=> 'Search Products',
    		'not_found' 			=> 'No Products found',
    		'not_found_in_trash'	=> 'No Products found in Trash',
    		//'parent_item_colon' 	=> '',
    		'menu_name' 			=> 'Products'
    
    	);
    
    	//'taxonomies' => array('category', 'post_tag') // this is IMPORTANT
    
    	$args['labels']			= $labels;
    	$args['supports']			= array('title','editor','thumbnail');
    	$args['rewrite']			= array('slug' => 'products');
    	$args['show_in_menu']		= true;
    	$args['taxonomies']			= array('products', 'post_tag');
    	$args['menu_icon']			= get_bloginfo('template_directory').'/custom/img/jobs.png';
    
    	register_post_type('products', $args);
    
    }
    
    // Add custom taxonomy for Products
    function product_init() {
    	// create a new taxonomy
    	register_taxonomy(
    		'productscat',
    		'products',
    		array(
    			'label' 		=> __( 'Products Category' ),
    			'sort' 			=> true,
    			'args' 			=> array( 'orderby' => 'products_order' ),
    			'query_var'		=> true,
    			//'rewrite'		=> true,
    			'rewrite'		=> array( 'slug' => 'productscat' ),
    			'hierarchical' 	=> true
    		)
    	);
    }
    add_action( 'init', 'product_init' );
    
    function add_featurecustom_meta()
    {
    	add_meta_box('product-detail', 'Product', 'product_meta', 'products', 'normal', 'default');
    }
  6. deepbevel
    Member
    Posted 2 years ago #

    I'm not used this code .. it may be beyond me, although that usually doesn't stop me :)

  7. deepbevel
    Member
    Posted 2 years ago #


  8. deepbevel
    Member
    Posted 2 years ago #

    pastebin
    I first had to figure out what the heck a custom post type is, how to use. What a suprise to find out!

    Just change the category slug and output as desired. You can repeat it to get more categories.
    The code behind the closing ul is all just for showing the category link, like you get with the_category. Took a while to get that!

    here's a demo, it's in the right sidebar at the bottom, used a php widget.

  9. deepbevel
    Member
    Posted 2 years ago #

    Haha, I had to update permalinks to get the category and post links working, thought for sure it was something deep, whew!

  10. obidos
    Member
    Posted 2 years ago #

    Hey,

    Thank you so much for looking at this. I'm glad you have also learned something re the custom post types :)

    This code kinda works for me. It lists the categories at the top and then lists all products. But each product is not under its category title :( so close.

    It looks like this:-

    Cat 1
    Cat 2
    Cat 3
    Cat 4

    All posts here...

    I need

    Cat 1
    Cat 1 posts here

    Cat 2
    Cat3 posts here

    Cat 3
    Car 3 poster here etc..

    Your code should work as its calling post within the category loop but for some reason wordpress is not doing it boo.

  11. deepbevel
    Member
    Posted 2 years ago #

    this code gets 1 category link only;

    <?php
    //list terms in a given taxonomy (useful as a widget for twentyten)
    $taxonomy = 'productscat';
    $tax_terms = get_terms($taxonomy);
    ?>
    <ul>
    <?php
    foreach ($tax_terms as $tax_term) {
    echo '<li>' . '<a href="' . esc_attr(get_term_link($tax_term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $tax_term->name ) . '" ' . '>' . $tax_term->name.'</a></li>';
    }
    ?>
    </ul>

    This code lists posts in 1 category only:

    <?php
    $args = array(
     'post_type' => 'products',
    'category'	     => product-test-cat-1,
    'posts_per_page' => -1,
    );
    $loop = new WP_Query( $args );
    
    while ( $loop->have_posts() ) : $loop->the_post();
    ?>
    <a href="<?php the_permalink(); ?>"><?php the_title();?></a>
    
    <?php	echo '<div class="entry-content">';
    the_post_thumbnail('thumbnail');
    the_excerpt();
    	echo '</div>';
    endwhile;
    ?>

    when I put them together like in pastebin I get:

    Category 1
    post1
    post2
    post3

    as is shown in my demo.
    `
    If you use the same code again for a second category and posts, it no longer works?

  12. obidos
    Member
    Posted 2 years ago #

    Hello,

    Thank you again for help.

    If I use code it shows this:

    Cat 1
    all posts

    Cat 2
    all posts

    The cat names are correct but it just lists all the post under each category name. So not post for that category just all posts.

    Maybe there is a way to specify the category i need posts for?

  13. deepbevel
    Member
    Posted 2 years ago #

    yes, did you specify here?

    'category'	     => product-test-cat-1,

    just add the name of your own category.

  14. obidos
    Member
    Posted 2 years ago #

    Hello,

    Here is the code I have come to that showed the results:-

    <?php
    	//list terms in a given taxonomy (useful as a widget for twentyten)
    	$taxonomy = 'productscat';
    	$tax_terms = get_terms($taxonomy);
    ?>
    <ul>
    <?php
    	foreach ($tax_terms as $tax_term) {
    
    		//print_r($tax_terms);
    		$catID = $tax_term->term_id;
    		$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    		echo $term;
    
    		//echo '<li><strong>'.$tax_term->name.'</strong></li>';
    
    		$args = array(
    			'post_type' 		=> 'products',
    			'category'	     => product-test-cat-1,
    			'posts_per_page'   => -1
    		);
    
    		print_r($catID);
    
    		$posts=get_posts($args);
    		if ($posts) {
    			echo '<p><strong>' .$tax_term->name.'</strong></p>';
    			echo '<p><strong>' .$tax_term->term_id.'</strong></p>';
    			foreach($posts as $post)
    			{
    				setup_postdata($post); ?>
    				<p><a href="<?php the_permalink() ?>" rel="bookmark" ><?php the_title(); ?><?php echo $catID; ?></a></p>
    			<?php
    			} // foreach($posts
    		}
    	}
    ?>
    </ul>

    I have commented bits out as I try to make it work as I need.

    If you see I put

    <?php echo $catID; ?>

    Next to the post title. This is to show what cat id the posts show they are in. It shows under each cat title posts are in that category.
    The the next category it shows the id for that category.

    It's just not separating the posts and calling the correct posts. It's calling all posts under each category title.

    Am I doing something wrong?

    :)

  15. deepbevel
    Member
    Posted 2 years ago #

    I'm sorry, it's different from what I did, too complex for me. What I did gives the results you say you want. Are you trying to add more arguments? different output?

    Also, I noticed in your code you still have

    'category'	     => product-test-cat-1,

    is "product-test-cat-1" the name of your products category? if not change it to what ever your cat is named.

  16. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    in the code from this reply http://wordpress.org/support/topic/list-categories-and-posts-per-category?replies=15#post-2972561
    try to edit this line:

    'category'	     => product-test-cat-1,

    to:

    'category_name'	     => $tax_term->slug,

    or to:

    'cat'	     => $tax_term->term_id,

    http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

  17. obidos
    Member
    Posted 2 years ago #

    Hi,

    It just list the cat ID numbers 98116107.

    Nothing else with either of those options.

    Its all odd its like it should work but doesn't

    It's driving me mad lol.

  18. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    my mistake - I did not fully realize that you are working with a custom taxonomy;

    http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

    try:

    'productscat'	     => $tax_term->slug,
  19. obidos
    Member
    Posted 2 years ago #

    That's it! Fixed! Thank you so much!

    I shall read the link too and keep learning.

    You're the best :)

  20. deepbevel
    Member
    Posted 2 years ago #

    this is too weird for me. How does the one I used work if I'm not using what Sweeper said? must be a different kind of query?

  21. deepbevel
    Member
    Posted 2 years ago #

    I messed with it a bit more, now I see the difference.

    => $tax_term->slug,

    gets all posts ordered by category. mine only pulls from 1 cat, and includes a link to the category page.
    So I see now your method is easier and more efficiant. It's also clear that it includes many more parameters, one of which probably displays the category name. Very interesting.

  22. obidos
    Member
    Posted 2 years ago #

    Glad you have learned too deepbevel!

    I have learned a lot about custom taxonomies etc recently. Going to look in to the code more and get my head around it :).

    Thank you to The Sweeper and Deepbevel for all your help.

Topic Closed

This topic has been closed to new replies.

About this Topic