How to link to category and list post under category
-
Can someone help me with the code below, I’m a designer and not too familiar with php.
<ul> <?php // get all the categories from the database $cats = get_categories(); // loop through the categries foreach ($cats as $cat) { // setup the category ID $cat_id = $cat->term_id; // Make a header for the cateogry echo "<li>".$cat->name."</li>"; // create a custom wordpress query query_posts("cat=$cat_id&post_per_page=10"); // start the wordpress loop! if (have_posts()) : while (have_posts()) : the_post(); ?> <?php // create our link now that the post is setup ?> <li><a href="<?php the_permalink();?>"><?php the_title(); ?></a></li> <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?> <?php } // done the foreach statement ?> </ul>
I actually want it to print…
– Link to category
– Instead of post title, just increments of 1,2,3.
– post nested within category.How can I get it to print like the below….
<ul> <li><a href="">category name</a> <ul> <li><a href="/">1</a></li> <li><a href="/">2</a></li> <li><a href="/">3</a></li> </ul> </li> <li><a href="">category name 2</a> <ul> <li><a href="/">1</a></li> <li><a href="/">2</a></li> <li><a href="/">3</a></li> </ul> </li> </ul>
-
<ul> <?php // get all the categories from the database $cats = get_categories(); // loop through the categries foreach ($cats as $cat) { // setup the category ID $cat_id = $cat->term_id; // Make a header for the cateogry echo "<li><a href=\"".get_category_link($cat_id)."\">".$cat->name."</a></li>"; // create a custom wordpress query $catposts = new WP_Query( array( 'category__in' => array($cat_id), 'posts_per_page' => 10) ); // start the wordpress loop! if ( $catposts->have_posts() ) : ?> <ul> <?php while( $catposts->have_posts() ) : $catposts->the_post(); ?> <?php // create our link now that the post is setup ?> <li><a href="<?php the_permalink();?>"><?php echo $catposts->current_post+1; ?></a></li> <?php endwhile; ?> </ul> <?php endif; wp_reset_postdata(); // done our wordpress loop. Will start again for each category ?> <?php } // done the foreach statement ?> </ul>
http://codex.wordpress.org/Function_Reference/get_category_link
http://codex.wordpress.org/Class_Reference/WP_Query
http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
http://codex.wordpress.org/Function_Reference/wp_reset_postdataThank you so much!
Ops… is it possible to ‘hack’ the category url to not include the word ‘category’ in the url? As I’ve created pages according to category names.
try a plugin – http://wordpress.org/extend/plugins/wp-no-category-base/
That won’t work for it, it breaks my current pages which has the same name as the category, I just thought of another way to make this work. But I’m not sure how to write it in php.
echo "<li><a href=\"".get_category_link($cat_id)."\">".$cat->name."</a>";
Instead of printing the category name, how can I print the blog url follow by the blog name in the link field?
- The topic ‘How to link to category and list post under category’ is closed to new replies.