• I am using the template called Mystique. It uses widget ready sidebars, but I haven’t found one that does exactly what I need. I need for the sidebar to display the 6 last posts in the category “news” only on the index. I consider myself a novice with PHP so I’m having trouble getting this to work.

    In the sidebar template, I’ve put in this code:

    <ul>
     <?php
    if (is_home());
    {
    
     global $post;
     $myposts = get_posts('numberposts=5&offset=1&category=news');
     foreach($myposts as $post) :
       setup_postdata($post);
    }
     ?>
        <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
     <?php endforeach; ?>
     </ul>

    So far, it’s not working. What else do I need to do to get it to work?

Viewing 15 replies - 1 through 15 (of 17 total)
  • Try this instead, fixes a few minor errors..

    <?php
    if( is_home() ) {
    	$myposts = get_posts('numberposts=5&offset=1&category=news');
    	if( $myposts ) {
    	?>
    		<ul>
    		<?php
    		foreach($myposts as $post) :
    			setup_postdata($post);
    		?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php
    		endforeach;
    		?>
    		</ul>
    	<?php
    	}
    }
    ?>

    Thread Starter jynolen

    (@jynolen)

    Thank you, t31os_. Is there some specific place in the sidebar I should place the code? when I put it into the sidebar template, nothing shows up. Would it be easier to just put the code into the index page itself?

    If you want it in the sidebar, then it’ll need to go in the sidebar..

    If you post your sidebar code here i can help you figure out where it can go, if you like…

    If posting code though, and it’s more then 20 lines, please use a pastebin and report the link back here.

    Thread Starter jynolen

    (@jynolen)

    Thank you again, I’m really getting close to pulling my hair out.

    http://wordpress.pastebin.ca/1842509

    Do you want this code to execute alongside widgets?

    Currently you have the code inside a conditional statement that runs only when no widgets are present, it doesn’t need be like that, but is that the intention?

    Thread Starter jynolen

    (@jynolen)

    It was probably accidental. I really don’t need any widgets running, just the code to display news posts.

    Ok, just stick the code from earlier before this line..

    <?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()): ?>

    It should appear .. providing it’s the home (since there’s a condition statement for the home page in your code).

    Thread Starter jynolen

    (@jynolen)

    It doesn’t show up.

    So I changed it to this:

    <?php
    if( is_page('Home') ) {
    	$myposts = get_posts('numberposts=5&offset=1&category=news');
    	if( $myposts ) {
    	?>
    		<ul>
    		<?php
    		foreach($myposts as $post) :
    			setup_postdata($post);
    		?>
    			<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    		<?php
    		endforeach;
    		?>
    		</ul>
    	<?php
    	}
    }
    ?>

    Where ‘Home’ is the title of the first page. It still doesn’t work. Here’s the actual page here.
    And here’s the pastebin of the updated sidebar code again… http://wordpress.pastebin.ca/1842566

    Thread Starter jynolen

    (@jynolen)

    I’ve also tried it as is_front_page() too because the static first page is set as the front. No such luck.

    Thread Starter jynolen

    (@jynolen)

    In fact, nothing shows up at all whenever I try any widgets in the sidebar. Great…

    so this is the issue with the sidebar’s location or something. remove the conditional function <?php if (!function_exists('dynamic_sidebar') || !dynamic_sidebar()): ?> and then insert the code…

    I’ve also tried it as is_front_page() too because the static first page is set as the front. No such luck.

    If you remove the is_home or is_page condition, does it then work?

    In fact, nothing shows up at all whenever I try any widgets in the sidebar. Great…

    This may be unrelated to the new code, remove the new code and see if you’re able to add widgets, if still problematic then further inspection is needed on the sidebar code.

    Thread Starter jynolen

    (@jynolen)

    @ rahulsonar, that was something I tried earlier. It didn’t work.

    @ t31os_, It doesn’t work with or without the condition. I’m trying to retrace what steps I’ve done because at the start of the day, the calendar widget appeared in the sidebar. Now it won’t. I’ve already replaced original copies of the template pages just to see if that makes it work again. So far… no.

    So replacing the sidebar with the original does not fix the widget issue?

    If that’s the case then the problem may not be in the sidebar, but with the file calling the sidebar..

    Thread Starter jynolen

    (@jynolen)

    I found the cause of the missing sidebar. The default page template was missing the get_sidebar call. I’ll test the code now to see if it works.

Viewing 15 replies - 1 through 15 (of 17 total)
  • The topic ‘get posts from one category to show in sidebar’ is closed to new replies.