Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

List Title of posts in subcategories WordPress

  • SOLVED

Example:

Sports is a parent category. Soccer is a sub cat of Sports, as is Baseball. I have multiple posts in each (sub) category, and I need the title of those posts to appear as links, grouped by their sub category - with a heading of that sub category name.

This page should display all subcategories which are under the Sports parent category. This is not fixed- it needs to be flexible, so it can't be hard coded. Even a query for the subcats can't be hard coded because if there are no posts for Soccer, then soccer shouldn't show up. If someone starts a frisbee team tomorrow, it needs to show up automatically.

Something like the following:


<h2>Sports</h2>

<h4>Baseball</h4>
<ul>
<li><a href="#">Baseball Team A</a></li>
<li><a href="#">Baseball Team B</a></li>
<li><a href="#">Baseball Team C</a></li>
<li><a href="#">Baseball Team D</a></li>
</ul>

<h4>Frisbee</h4>
<ul>
<li><a href="#">Frisbee Team A</a></li>
<li><a href="#">Frisbee Team B</a></li>
<li><a href="#">Frisbee Team C</a></li>
<li><a href="#">Frisbee Team D</a></li>
</ul>

<h4>Soccer</h4>
<ul>
<li><a href="#">Soccer Team A</a></li>
<li><a href="#">Soccer Team B</a></li>
<li><a href="#">Soccer Team C</a></li>
<li><a href="#">Soccer Team D</a></li>
</ul>


My current code, which I've peiced together based on varius help forms and articles is this:

<?php
global $options;
foreach ($options as $value) {
if (get_option( $value['id'] ) === FALSE) { $$value['id'] = $value['std']; }
else { $$value['id'] = get_option( $value['id'] ); }
}
?>
<?php

if ( is_page('Sports') ) { ?>

<?php query_posts('showposts=0&cat=6'); ?>

<?php global $post; $count = 1;
while ( have_posts() ) : the_post() ?>
<?php $category = get_the_category();
// Get the ID of a given category
$category_id = get_cat_ID( $category[0]->cat_name );

// Get the URL of this category
$category_link = get_category_link( $category_id ); ?>

<h4> <a href="<?php echo $category_link; ?>" title="<?php echo $category[0]->cat_name; ?>"><?php echo $category[0]->cat_name; ?>
<span>view only <?php echo $category[0]->cat_name; ?> &rarr;</span>
</a>

</h4>
<ul>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?>
<?php // This is the implementation of custom write panels.
$data = get_post_meta( $post->ID, 'module', true ); ?>
<span class="listing-entry organization"><?php echo $data[ 'organization' ]; ?></span>
<span class="read-more">(read more)</span>
</a></li>
</ul>
<?php endwhile; ?>

<?php } ?>


And what comes out is each listed title is within it's own <ul>, so rather than all the soccer posts listed together, they are separate, as seen here: http://yatescommunitycenter.org/dev/sports .

Answers (2)

2009-12-28

Utkarsh Kukreti answers:

<?php

if(is_page('Sports'))
{
$cat = 'Sports';
$catID = get_cat_ID($cat);
echo '<h2>' . $cat . '<h2>';
$subcats = get_categories('child_of=' . $catID);
foreach($subcats as $subcat)
{
echo '<h4><a href="' . get_category_link($subcat->cat_ID) . '">' . $subcat->cat_name . ' <span>view only ' . $subcat->cat_name . ' &rarr;</a></h4>';
echo '<ul>';
$subcat_posts = get_posts('cat=' . $subcat->cat_ID);
foreach($subcat_posts as $subcat_post)
{
echo '<li>';
$postID = $subcat_post->ID;
echo '<a href="' . get_permalink($postID) . '">';
echo get_the_title($postID);
echo '<span class="listing-entry organization">';
$postmeta = get_post_meta($postID, 'module', true);
echo $postmeta['organization'];
echo '</span><span class="read-more">(read more)</span>';
echo '</a>';
echo '</li>';
}
echo '</ul>';
}
}

?>


Just change the line $cat = 'Sports'; to change the category.
If there are any errors, please contact me.

2009-12-28

Ron Rennick answers:

Starting at

if ( is_page('Sports') ) {
$cat = 6; //sports category id
$children = get_term_children( $cat );
if( is_array( $children ) && !empty( $children ) ) {
foreach( $children as $child ) {
query_posts( 'cat=' . $child );
if( have_posts() ) { ?>
<h4><a href="<?php echo get_category_link( $child ); ?>"><?php echo get_cat_name( $child ); ?></a></h4>
<ul>
<?php while( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?>

<?php // This is the implementation of custom write panels.

$data = get_post_meta( $post->ID, 'module', true ); ?>

<span class="listing-entry organization"><?php echo $data[ 'organization' ]; ?></span>

<span class="read-more">(read more)</span>

</a></li>
<?php endwhile; ?>
</ul>
<?php
}
}
}
}
?>


The post limit on this will be whatever is set in your reading settings for the number of posts to show. If you want to show just 4 post titles per category then add

showposts=4

to the query_posts.