• I’m trying to show the post links by each category in my sidebar on my blog.

    <h2>Category ID 2</h2>

      <?php what is the php code to display the category posts by cat id 2 ?>

    <h2>Category ID 34</h2>

      <?php what is the php code to display the category posts by cat id 34 ?>

    Any help is appreciate before I pull my hair out!@@

Viewing 9 replies - 1 through 9 (of 9 total)
  • <?php wp_list_categories(‘orderby=name&include=2’); ?>

    This code example comes from this help page:
    Category Help

    Does this help with your problem?

    Thread Starter chrisb125

    (@chrisb125)

    It displays the categories, but not the post “in” the category. So the category id is 34, how do I display the posts from id 34 into the sidebar?

    There is probably an easier way, or a plugin, but here is how I accomplished showing a link to each category post, limited by category:

    <?php
    $myposts = get_posts(‘numberposts=10&category=’);
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <?php if ( ! in_category(6) ) { ?>
    <li class=’page_item’>” rel=”bookmark” title=”Permanent Link to <?php the_title(); ?>”><?php the_title(); ?>
    <?php } ?>
    <?php endforeach; ?>

    Just change the in_category(6) to include whatever category you wish to have post links displayed from.

    Thread Starter chrisb125

    (@chrisb125)

    Thank you…

    When adding a link what php tag do I put in here to link to the post?

    ??

    <ul>
    			<li class="page_item"><h2>Recent News</h2></li>
    		<?php
    			$myposts = get_posts('numberposts=10&category=');
    			foreach($myposts as $post) :
    			setup_postdata($post);
    			?>
    			<?php if ( ! in_category(6) ) { ?>
    				<li class='page_item'><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></li>
    			<?php } ?>
    		<?php endforeach; ?>
    	</ul>

    For some reason, the earlier message truncated the a href tag. The code above should display a link to the post using the Title as the link text.

    Thread Starter chrisb125

    (@chrisb125)

    Ah… thank you so much!

    Actually I had to add the category # to display the correct category posts:

    $myposts = get_posts(‘numberposts=10&category=28’);

    Thanks for the help ??

    Glad I could help. You are quite welcome!

    This may be six of one, half a dozen of the other, but here’s how I did it. You can see it (Tech Tips), here: https://www.znzx.com

    You’ll need to grab your category ID (I inserted the value X), and you can tell it how many posts you want it to display (value Y). Change up the heading from techtips to whatever your category is.

    <li id="techtips"><h3>Tech Tips</h3>
    <ul>
     <?php
     global $post;
     $myposts = get_posts('numberposts=Y&offset=1&category=X');
     foreach($myposts as $post) :
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
    </ul>

    @jonimueller: Thanks for your post.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Code to display posts by category?’ is closed to new replies.