0
votes

I'm attempting to display:

  1. Linked Category Titles
  2. Category Descriptions (trimmed to custom length)
  3. Category "Readmore Styled Link
  4. A lists of Post Titles WITHIN that category, linking to the posts

I've accomplished tasks 1-3 with a Foreach loop, but I can't seem to display posts titles WITHIN each category through methods I've used in the past. The main thing is that I can't figure out how to run get_posts(); or a function like it with a variable in the $args array.

I feel this really needs to be done with a foreach loop, as I'm working with 20+ Categories. I've tried mixing / matching with a 3rd party shortcode plug-in, but due to "order of wp operations" that idea failed as well :( Any help would be EXTREMELY appreciated as I've been spinning my wheels for the past 3-4 hours.

Source Code → http://pastebin.com/Mm9u27dF

Code Output:

<p class="topic-link-heading"><a href="http://localhost:81/wordpress/?cat=3" id="topic-link">Understanding Democratic Governance and Market Economy</a></p><p class="topic-list">There is an ongoing debate in academic circles and among practitioners on the linkages between democratic governance and market economies. It has intensified in light of transitions taking place after the fall of the Berlin Wall. Amidst expectations that all &hellip; <a href="http://localhost:81/wordpress/?cat=3" > Topic Overview &rarr;</a></p>3

Please Note

The "3" is only being displayed to show that the category ID variable is outputting correctly

1

1 Answers

1
votes

From what I understand, you want to display the posts for a specific category and you're having trouble doing it.

You're looping through the categories and I think you just need to use the query_posts function to query the relevant posts with the specific category (I've taken the code from the official documentation):

<?php
 $post_args = array(
'posts_per_page'   => 5,
'offset'           => 0,
'category'         => $category->term_id, //in your case.
'orderby'          => 'post_date',
'order'            => 'DESC',
'include'          => '',
'exclude'          => '',
'meta_key'         => '',
'meta_value'       => '',
'post_type'        => 'post',
'post_mime_type'   => '',
'post_parent'      => '',
'post_status'      => 'publish',
'suppress_filters' => true );

// The Query
query_posts( $post_args );

// The Loop
while ( have_posts() ) : the_post();
    echo '<li>';
    the_title();
    echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
?>

To check if the posts are getting queried or not do something like this:

 $relevant_posts = query_posts( $post_args );
 print_r($relevant_posts); //Should print an associated array with the posts.

So you could populate the args variable with the specific category that you're fetching within the for loop and then just query the posts. Once you have the posts, you can refer to the links from easily. This documentation also might come in handy while looping through the posts.