• Resolved websta

    (@websta)


    I don’t know php, yet need to display a list of 10 most recent posts in sidebar.php from another category, and the list needs to vary depending on what category the post is in.

    After hours and hours of research, reading, and trial & error, the “bla bla…” text displays properly, but I am at a loss how to pull titles/links of recent posts from category XYZ into a list. Any suggestions?

    <?php
    if  (in_category(14)) {
            echo "<li id='recent'><ul class='recent'><li>";
            echo "bla bla blabbity bla bla";
            echo "<?php wp_get_archives('type=postbypost&limit=10'); ?>";
            echo "</li></ul></li>"; 
    
    } elseif (in_category(13)) {
            echo "<li id='recent'><ul class='recent'><li>";
            echo "<?php wp_get_archives('type=postbypost&limit=10'); ?>";
            echo "</li></ul></li>";
    
    } else {
            // catch-all for everything else (archives, searches, 404s, etc)
            echo "Pedro offers you his protection.
    ";
    }
    ?>
Viewing 6 replies - 1 through 6 (of 6 total)
  • Hello, I don’t mean to spam. I’m just waiting for answers too..
    🙂

    Here’s something I’ve used:

    echo('<ul>');
     query_posts('cat=4&showposts=-1');
     if (have_posts()) : while (have_posts()) : the_post();
     echo('<li><a href="');
     the_permalink();
     echo('">');
     the_title();
     echo('</a></li>');
     endwhile; endif;
     echo('</ul>');
    Thread Starter websta

    (@websta)

    Thank you drmanry! It works! It works! 88 miles per hour!

    For afandi2, and anyone else who needs it, here is what I have to display a list of posts from another category, which varies according to what category the user is in (e.g., it changes the list of posts in the sidebar depending on what category the current post/page is in).

    This may be useful if, like me, you have child sub-categories but you need a list of posts from them to appear on posts/pages located in the parent directory. Paste the code below into a template page, just change the 14 and 22 below to your relevant categories:

    <?php
    if  (in_category(14)) {
            echo "<li id='recent'><ul class='recent'><li>";
            echo('<ul>');
            query_posts('cat=22');
            if (have_posts()) : while (have_posts()) : the_post();
            echo('<a href="');
            the_permalink();
            echo('">');
            the_title();
            echo('</a>');
            endwhile; endif;
            echo('</ul>');
            echo "</li></ul></li>";
    
    // start of a place that can be repeated for each category,
    // just replace 35 and 28 below
    // i.e., if we are in category id 35, then show
    // a linked list of posts from category 28
    
    } elseif (in_category(35)) {
            echo "<li id='recent'><ul class='recent'><li>";
            echo('<ul>');
            query_posts('cat=28');
            if (have_posts()) : while (have_posts()) : the_post();
            echo('<a href="');
            the_permalink();
            echo('">');
            the_title();
            echo('</a>');
            endwhile; endif;
            echo('</ul>');
            echo "</li></ul></li>";
    
    // end of the repeating part
    
    // below is the end - which is helpful to see if problems arise
    // you can change the text between quotation marks to anything
    // you want - including php or html
    // if you want to show nothing, you can change it to: echo "";
    // or delete the line entirely
    
    } else {
            // catch-all for everything else (searches, 404s, etc)
            echo "<p>Pedro offers you his protection.</p>";
    }
    ?>

    Hey but I used this code below & it made my page repeat forever and ever lol how do I make it stop. BTW i know nothing about PHP lol

    <?php
    echo(‘

      ‘);
      query_posts(‘cat=18&showposts=10’);
      if (have_posts()) : while (have_posts()) : the_post();
      echo(‘

    • <a href=”‘);
      the_permalink();
      echo(‘”>’);
      the_title();
      echo(‘
    • ‘);
      endwhile; endif;
      echo(‘

    ‘);
    ?>

    Can anybody please help?

    i think re install the theme. 🙂

    well if you look closely at the codes, they will somehow tell you only its abit messy.

    You need to reset the query, so a full working code would be:

    <?php
    	query_posts('cat=5');
    	if (have_posts()) : while (have_posts()) : the_post();
    	echo('<li><a href="');
    	the_permalink();
    	echo('">');
    	the_title();
    	echo('</a></li>');
    	endwhile; endif;
    
    	//Reset Query
    	wp_reset_query();
    ?>

    And that should do it. You can see this article in the Codex for more info

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Part way there… what am I doing wrong??’ is closed to new replies.