[resolved] Pull one category on a page where page name = category name (3 posts)

  1. Elad Bushari
    Posted 7 years ago #


    I'm trying to create a sidebar section on a custom page that will show only posts from a category named the same as the page's name.

    Page's name = mandarin, side bar shows posts all posts from mandarin category (may be a sub category)

    This is what I have now:
    <?php $recent = new WP_Query("cat=3&showposts=5"); while($recent->have_posts()) : $recent->the_post();?>
    <?php if( get_post_meta($post->ID, "thumbnail", true) ): ?>
    " rel="bookmark">
    <?php else: ?>
    " rel="bookmark">
    <?php endif; ?>
    <b>" rel="bookmark"><?php the_title(); ?></b>
    <?php the_content_limit(200, ""); ?>
    <?php endwhile; ?>

    It shows category #3 only, but I'd like it to be more automated once I create a new page (I use a separate page template for this purpose)

    What should I write instead of the category number?


  2. MichaelH
    Posted 7 years ago #

    This will give you something to play with

    if (is_page()) {
      $cat=get_cat_ID($post->post_title); //use page title to get a category ID
      $posts = get_posts ("cat=$cat&showposts=5");
      if ($posts) {
        foreach ($posts as $post):
          setup_postdata($post); ?>
          <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <?php endforeach;
  3. Elad Bushari
    Posted 7 years ago #

    Thanks!!! Works great!!!

Topic Closed

This topic has been closed to new replies.

About this Topic