ThemeShaper Forums » Thematic

[closed]

News Category Sidebar Query

(19 posts)
  • Started 3 years ago by stever
  • Latest reply from stever
  • This topic is resolved

Tags:

  1. stever
    Member

    Hi, been trying out a custom query at the top of my sidebar but have the white screen of death when saving my functions.php. Firstly is this the best way to show links to the last five posts in my news category? If it is, where am I going wrong?

    Code is;

    /* ----- News Category Sidebar Query ----- */
    function news_catloop(){
    query_posts("numberposts=5&category_name=news");
    if ($news_query->have_posts()) : while ($news_query->have_posts()) : $news_query->the_post();
    <ul class="xoxo">
    <h3 class="widgettitle">Latest News</h3>

  2. "><?php the_title(); ?>
  3. <?php endwhile; else: ?>
    <p>No Available News Posts</p>
    <?php endif;
    wp_reset_query();
    }
    add_action('thematic_abovemainasides', 'news_catloop');

    Thanks
    Steve

Posted 3 years ago #
  • stever
    Member

    And the code is:

    /* ----- News Category Sidebar Query ----- */
    function news_catloop(){
    query_posts("numberposts=5&category_name=news");
    if ($news_query->have_posts()) : while ($news_query->have_posts()) : $news_query->the_post();
    <ul class="xoxo">
    <h3 class="widgettitle">Latest News</h3>

  • "><?php the_title(); ?>
  • <?php endwhile; else: ?>
    <p>No Available News Posts</p>
    <?php endif;
    wp_reset_query();
    }
    add_action('thematic_abovemainasides', 'news_catloop');

    Posted 3 years ago #
  • stever
    Member

    Can't seem to post the code properly, the missing lines are the li and ul lines, I don't think these are the issue.

    Posted 3 years ago #
  • helgatheviking
    Moderator

    you post code between tick marks.

    also i would recommend using get_posts instead of a new query for something in the sidebar. that way you don't monkey with the main query in any way.

    also whenever you get a white screen of death you should set the WP_debug mode to TRUE in the config.php file of your wordpress install. like this:

    http://www.wprecipes.com/how-to-easily-enabledisable-debug-mode-in-wordpress

    that should tell which line is killing the PHP. it is probably a quote or bracket that isn't closed, or a missing semi-colon.

    and for what you are trying to do, why not just use someone else's code. there is a widget for exactly this.

    http://wordpress.org/extend/plugins/category-posts/

    Posted 3 years ago #
  • stever
    Member

    Hi H,

    Big learning curve for me, trying my hardest to not use external plugins.

    /* ----- News Category Sidebar Query ----- */
    function latest_news(){
    <ul class="xoxo">
    	<?php
    		global $post;
    		$myposts = get_posts('numberposts=5&category_name=news');
    		foreach($myposts as $post) :
    		setup_postdata($post);
    	?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
    </ul>
    }
    add_action('thematic_abovemainasides', 'latest_news');

    Using get_posts, I think it is more to do with the way I add the function, am I doing that part right? FTP client just flattend my wp-config into one long line, so just got over the shock my test site back up again. Using back ticks this time, thanks for the nod.

    Posted 3 years ago #
  • helgatheviking
    Moderator

    well does that work? it looks correct to me. also don't hate on plugins... :) no sense in reinventing the wheel. (spoken by someone who spent 3 days trying to code what NextGen gallery almost already does and does better)

    Posted 3 years ago #
  • stever
    Member

    Parse error: syntax error, unexpected '<' in /home/users blah blah line 4 - Put it at the top so it's when it hits the <UL

    The main section of the code works fine in the default Wordpress 2010 theme;

    <ul class="xoxo">
    	<?php
    		global $post;
    		$myposts = get_posts('numberposts=5&category_name=news');
    		foreach($myposts as $post) :
    		setup_postdata($post);
    	?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
    </ul>

    So I still think it may be the way I'm adding the function, or does the above need some extra coding because it's inside a function, can you see anything? Eyes are bleeding, off for a think. Don't know any PHP yet, any help would be appreciated.

    Thanks - Steve

    Posted 3 years ago #
  • krowchuk
    Member

    Steve,

    You are not closing your php code (assuming it was left open in the function above this one...) before your html in this code you wrote... and then not reopening it after your html before the add_action function.

    /* ----- News Category Sidebar Query ----- */
    function latest_news(){
    <ul class="xoxo">
    	<?php
    		global $post;
    		$myposts = get_posts('numberposts=5&category_name=news');
    		foreach($myposts as $post) :
    		setup_postdata($post);
    	?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
    </ul>
    }
    add_action('thematic_abovemainasides', 'latest_news');

    Try this...

    /* ----- News Category Sidebar Query ----- */
    function latest_news(){
    
    ?>
    
    <ul class="xoxo">
    	<?php
    		global $post;
    		$myposts = get_posts('numberposts=5&category_name=news');
    		foreach($myposts as $post) :
    		setup_postdata($post);
    	?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
    </ul>
    
    <?php
    
    }
    add_action('thematic_abovemainasides', 'latest_news');
    Posted 3 years ago #
  • helgatheviking
    Moderator

    yeah you weren't closing your php tags and then re-opening them

    /* ----- News Category Sidebar Query ----- */
    function latest_news(){ ?>
    <ul class="xoxo">
    	<?php
    		global $post;
    		$myposts = get_posts('numberposts=5&category_name=news');
    		foreach($myposts as $post) :
    		setup_postdata($post);
    	?>
    		<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
    	<?php endforeach; ?>
    </ul> <?php
    }
    add_action('thematic_abovemainasides', 'latest_news');
    Posted 3 years ago #
  • krowchuk
    Member

    Or, you could do it Helga's way too! ;-)

    Posted 3 years ago #
  • stever
    Member

    Hi Guys,

    Thank you both. Works a treat, just need some styling now to match the other sidebar items. Might just slow down a little after this and look at a basic php course, do you know any good references for newbs.

    Thanks again
    Steve

    Posted 3 years ago #
  • stever
    Member

    Hi again, gone a little further on this one.

    /* ----- News Category Sidebar Query ----- */
    function latest_news(){
    ?>
    <div class="aside main-aside">
    	<ul class="xoxo">
    	<h3 class="widgettitle">Latest News</h3>
    		<?php
            	global $post;
            	$myposts = get_posts('numberposts=5&category_name=news');
            	foreach($myposts as $post) :
            	setup_postdata($post);
        		?>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        	<?php endforeach; ?>
    	</ul>
    </div>
    <?php
    }
    add_action('thematic_abovemainasides', 'latest_news');

    Great for the standard Thematic 2col layouts; Not that I use the 3col layout but in the 3col layout the sidebar drops below the content, presumably because it's above the the Primary or Secondary divs. Would need to add it to the 'widget_area_primary_aside' Helga, this is where you say 'why not use a widget' as above. Good learning curve, but might have to use the widget after all.

    Posted 3 years ago #
  • krowchuk
    Member

    Or, you could simply wrap your aside with a div that you open in thematic_abovemainasides and close it in thematic_betweenmainasides...

    Think outside the box here (or the div ha!)

    /* ----- News Category Sidebar Query ----- */
    function latest_news(){
    ?>
    <div id="aside-wrapper" class="aside main-aside">
    
    	<ul class="xoxo">
    	<h3 class="widgettitle">Latest News</h3>
    		<?php
            	global $post;
            	$myposts = get_posts('numberposts=5&category_name=news');
            	foreach($myposts as $post) :
            	setup_postdata($post);
        		?>
    	<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
        	<?php endforeach; ?>
    	</ul>
    
    <?php
    }
    add_action('thematic_abovemainasides', 'latest_news');
    
    function close_news() {
    ?>
    
        </div><!-- end aside-wrapper opened in thematic_abovemainasides -->
    
    <?php
    }
    add_action('thematic_betweenmainasides','close_news');
    Posted 3 years ago #
  • helgatheviking
    Moderator

    krowchuk- why would you do that? then you'd also be wrapping everything else in the primary aside up in this div too. better i think to just wrap the content stever is trying to generate. and ummm... yes this is exactly why i like plugins that already do exactly what i am trying to do. :)

    Posted 3 years ago #
  • stever
    Member

    Krowchuck - Like the concept but unfortunately it didn't work.
    Helga - Looked at the plugin you suggested, as much as I would love to do it without a plugin, just can't fault how easy it is to do with that particular widget + the fact that it's easily re-usable in different widget areas.

    Posted 3 years ago #
  • krowchuk
    Member

    viking - cuz steve insists on reinventing the wheel! ;-)

    ... and I haven't figured out how to filter yet!

    Seems to me that one could set up a filter to remove these:

    <div class="aside main-aside">
    	<ul class="xoxo">

    and then add them into the new above_mainasides function?

    I just don't know how to do that but I am sure you could figure it out?

    steve - did you style the wrapper div? Set a new css rule for your #aside-wrapper and have a look at your #main rules, you will need to copy the rules from #main into #aside-wrapper (not the classes aside and main-aside since they affect other asides!) and then it should work. Specifically the floats and widths. You might have to fiddle with your #main, remove some rules there. Without knowing what your changes have been to #main so far, or which 3 column layout you are using I can't really be more specific on what to change. Do you have Firebug?

    Either way, your problem is now css positioning / layout so you are mostly there.

    FWIW, I would grab two plugins and add them to your arsenal, Executable PHP widget and Widget Logic - these will solve most of your problems the easy way...

    Posted 3 years ago #
  • stever
    Member

    Krowchuk - Thanks for all your thought on this. I actually didn't beleive the effort this would cause bearing in mind it's a doddle in a normal theme. Creating a ton of functions to cram the thing into the sidebar, then re-jigging the standard Thematic layouts in my child theme seems 'too much' when a widget is available to do what I need. Again thank you for your help, I hadn't considered that I was re-inventing the wheel, just trying to do something I thought was fairly simple, that I had working in a non-framework theme well enough.

    Helga - I'm a convert, thanks for your advice.

    Posted 3 years ago #
  • krowchuk
    Member

    Hey Steve - no problem!

    I understand what you are saying, I went through this very exercise myself of placing custom php INSIDE of an ASIDE before deciding the best approach was to simply use the Executable PHP Widget plugin that gives you a widget to enter your custom php into. Combined with Widget Logic this eliminates almost all the pain!

    The exercise though, did teach me how the hooks can be very powerful and how to use them.

    I would agree, managing the seemingly inflexibility of the asides seems to be the trickiest part of Thematic.

    I still think there must be a way to use a filter in combination with the hook to insert the custom PHP INSIDE the ASIDE and accomplish this without the use of plugins.

    Posted 3 years ago #
  • stever
    Member

    krowchuk - Executable PHP widget and Widget Logic. Will take a look at these two plugins and thanks for your help.

    Posted 3 years ago #

  • RSS feed for this topic

    Topic Closed

    This topic has been closed to new replies.