ThemeShaper Forums » Thematic

[closed]

Index Loop - Most recent post of a single category

(15 posts)
  • Started 3 years ago by vicky515
  • Latest reply from helgatheviking
  • This topic is resolved
  1. vicky515
    Member

    It seems like everytime I start to understand how Thematic works, I run into a situation that has me confused again. So although what I want to achieve is simple, it may require some explaining so that I understand it :)

    As a background, typically what I've been doing is finding the function that I need to change, copying and pasting it into my child theme's functions.php file, changing it's name to childtheme_override_whatever, then modifying it as I need. I have a few filters but I feel like I aught to be using them more. Which suggests that I still don't understand them.

    Right now, though, I'm staring at my newly pasted childtheme_override_index_loop function and I'm feeling pretty overwhelmed. I find it difficult to understand what exactly is going on in this function to determine what I need to change. And the few helpful loop explanations that I've found aren't specific to Thematic and don't look even remotely close to Thematic's loop.

    What I want to do is only show the most recent article in 1 category.

    And here's what I'm starting with...

    // My Custom Index Loop
    function childtheme_override_index_loop() {
    	global $options, $blog_id;
    
    	foreach ($options as $value) {
    		if (get_option( $value['id'] ) === FALSE) {
    			$$value['id'] = $value['std'];
    		} else {
    			if (THEMATIC_MB)
    			{
    				$$value['id'] = get_option($blog_id,  $value['id'] );
    			}
    			else
    			{
    				$$value['id'] = get_option( $value['id'] );
    			}
    		}
    	}
    
    	/* Count the number of posts so we can insert a widgetized area */ $count = 1;
    	while ( have_posts() ) : the_post();
    
    			thematic_abovepost(); ?>
    
    			<div id="post-<?php the_ID();
    				echo '" ';
    				if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
    					post_class();
    					echo '>';
    				} else {
    					echo 'class="';
    					thematic_post_class();
    					echo '">';
    				}
    				thematic_postheader(); ?>
    				<div class="entry-content">
    <?php thematic_content(); ?>
    
    				<?php wp_link_pages('before=<div class="page-link">' .__('Pages:', 'thematic') . '&after=</div>') ?>
    				</div><!-- .entry-content -->
    				<?php thematic_postfooter(); ?>
    			</div><!-- #post -->
    
    		<?php 
    
    			thematic_belowpost();
    
    			comments_template();
    
    			if ($count==$thm_insert_position) {
    					get_sidebar('index-insert');
    			}
    			$count = $count + 1;
    	endwhile;
    }
    } // end index_loop

    Much thanks in advance.

    Posted 3 years ago #
  2. vicky515
    Member

    So I added

    query_posts("category_name=blog&posts_per_page=1");

    to the top of the function, seems to work. Except now it's showing the post as an excerpt (which it wasn't doing before), and when you click "Older posts" at the bottom, it keeps showing only that 1 post.

    Any ideas?

    Posted 3 years ago #
  3. vicky515
    Member

    I think this might be a bigger problem... If somebody could please help me.
    I decided to look at other loops so I reduced the posts per page cont in Wordpress' Reading Settings to 1. And now every blog page after the first article crashes with 404. Except the index loop, which keeps showing the same article.

    Posted 3 years ago #
  4. i answered a similar question on modifying the index loop here:
    http://forums.themeshaper.com/topic/exclude-a-category-from-index-loop#post-20941

    you should also take a look at the blog template in thematic. the problem w/ your above effort is that you don't have the paged variable. here is the query from the blog template

    $wp_query = new WP_Query();
    			$wp_query->query( array( 'posts_per_page' => get_option( 'posts_per_page' ), 'paged' => $paged ) );
    			$more = 0;

    basically that paged variable is how you get different posts on the "previous posts" section.

    so you could either remember to include that, or do what i did in that other thread and use wp_parse_args to merge your arguments w/ the existing query args.

    Posted 3 years ago #
  5. vicky515
    Member

    Thanks very much for responsing, Helga.

    1. When you say to look at the blog template in thematic, what are you referring to? What function? As I said, I have copied & pasted the Index Loop from thematic exactly. So I'm confused as to what I'm missing. And would like very much to know where thise paged variable originally resides.

    2. I replaced my query_posts line with the statement that you provided, and now the Index loop functions correctly. However, all the other category loops are not - they are still returning the 404. Where would you recommend I put that statement for it to apply to all loops?
    Also, do you have any suggestion as to what caused the problem in the first place? I haven't touched any of the parent theme files. It just seems odd that I'd have already-broken loops in my theme...

    3. Also, I copied and pasted the function you wrote in the other post and changed
    $exclude = array('category__not_in'=> array('blog'));
    But it doesn't work. It still shows new posts from all categories.

    Posted 3 years ago #
  6. 1. i mean look at the actual template. the file: template-page-blog.php

    2. i'd start w/ undoing whatever you've done. why are you overriding the index loop if you aren't making changes to it? (or are you.. i didn't look too closely) maybe even starting from scratch if necessary to get rid of the 404s.

    3. you can think of my $exclude variable as just where you put any new parameters that aren't in the default query. the category__not_in parameter only accepts category ID numbers, so array('blog') will never work.

    see the codex for usable parameters
    http://codex.wordpress.org/Function_Reference/WP_Query#Parameters

    4. to mod the query across the board you can't hook into thematic_above_indexloop. as the name implies that will only fire above the index loop. so you need a hook that will be on every page/post. i'd probably try thematic_abovecontent hook.

    ----
    what exactly are you trying to do? if you just want to show 1 post per page you can do that in your Dashboard's "settings" w/o monkeying with the loop at all.

    Posted 3 years ago #
  7. vicky515
    Member

    Hi, so I found the blog thing you were talking about and added it to my categories.php file and it still causes the error. I decided to try changing my theme back to the Thematic default one and I'm getting the 404 errors when clicking "Older Posts".

    So it's broken in the parent theme as well.

    Thoughts?

    Posted 3 years ago #
  8. vicky515
    Member

    Oops, didn't refresh the page to see your response :)

    Regarding what I'm trying to do...

    1. I want the index loop only to show the blog posts. That's why I was modifying the index loop. I wanted to restrict it to show blog posts only. But I'm having difficulty determining how to limit it to just one category without causing other issues. If you look at my second post, I describe what the issues was.

    2. As for the other loops, I'm not doing anything. I haven't touched them at all. But they're all returning 404 errors when you click to see previous posts. And I don't know why. And as I said above, even the parent thematic theme has this issue.

    I have even tried deleting the Thematic theme and reinstalling it, just in case I accidentally modified some of the files.

    Posted 3 years ago #
  9. resave your permalinks, b/c sometimes that solves random 404 errors.

    why are you adding stuff to your categories.php if you want to adjust how the blog loop works?

    undo whatever you've done. start from scratch. it seems painful but it is much easier than trying to debug where you went wrong. (especially for me)

    function mod_index_query() {
    global $wp_query;
    
    		$defaults = $wp_query->query_vars;
    		$custom = array('category_name'=> 'blog');
    		$args = wp_parse_args( $custom, $defaults );
    		query_posts($args);
    
    }
    add_action('thematic_above_indexloop','mod_index_query');

    since you only want to change the blog index (you're telling me you only want to show posts in the blog category) then the above should work fine. thematic_above_indexloop is suited for this task.

    Posted 3 years ago #
  10. vicky515
    Member

    There are 2 points to what I am trying to do, as mentioned above.

    1. Make index only show blog posts

    2. FIX the other loops, which throws 404 everytime "Older posts" is clicked.

    The reason I'm in the categories.php is because of reason #2.

    Resaving the permalinks did not resolve the issue.

    I'm not really sure what more "undoing" I can do from deleting Thematic, reinstalling, and using the newsly installed Thematic theme. Which has the same issue. Do you want me to delete all of my posts too? I can't do that :\

    Your function works, but they are showing as excerpts which I mentioned in my second post also.

    Posted 3 years ago #
  11. starting from scratch involves a blank functions.php in your child theme (and not overriding any other templates either... like category.php etc). you are making a child theme right?

    1. my code above solves this

    2. thematic shows category pages (and all archives in fact) with correct pagination just fine out of the box, so the problem is with your code somewhere.... i have no idea where, which is why i suggested starting from scratch.

    3. the switching to excerpts is happening after using only my code? shouldn't have anything to do with it, so i am confused by that one. this is how you adjust whether to show excerpts or full posts

    function childtheme_content($content) {
    	if (is_category()) {   //use your conditional logic of choice
    		$content= 'full';}  //or set to excerpt
    	return $content;
    }
    add_filter('thematic_content', 'childtheme_content');

    thematic shows full posts on is_home() (which is your blog index) by default

    Posted 3 years ago #
  12. vicky515
    Member

    I'm sorry, I don't really know how to make this more clear...

    I am using a child theme, but please forget that for now.
    Even if I activate Thematic as my theme, I am still getting these "not found" errors when clicking "Older posts" in the category views. This means none of my code is being used.

    And just in case I unknowingly modified Thematic's code by accident, I even deleted my child theme and Thematic, then reinstalled Thematic through the Wordpress admin theme installer. This would make it a fresh, untouched, installation.
    And still, I get the "not found" errors when clicking "Older posts" from category view.

    In fact, to further explain this issue, I have just tried activating the twenty-ten theme that Wordpress comes with. Completely separate from Thematic.
    Still get "not found" when clicking "Older posts" from category view.

    So now I know that it is not even Thematic causing the issue. But I'm afraid this makes it even harder to fix.

    I have also tried deactivating plugins, and short of deleting my posts I'm not really sure what more to do to "start from scratch" :(

    ======

    As for Thematic showing full posts on is_home(), it always does. When I do not restrict it, it always shows full posts. But for some reason once I get it down to just show blog posts, it does. Both with your function and the statement I tried in my second post. I have no idea why, because I don't have anything in my functions.php file that would cause this.

    Your function fixed it though, thank you so much.
    To limit it to only show 1 post, I copied & pasted the function adding "2" to the name, and changed the array to 'posts_per_page'=> '1'
    Is this ok? Or is there a "better way" to combine arguments?

    =========

    I'm starting to wonder if some Wordpress updates that I've done may be causing these random issues. Whenever I would logon to work on it a bit more and I'd see a new update was available, I'd just go ahead and do it. I don't have crazy code or changes made and the updates never generated any errors, so I never thought twice about it until now.

    Thoughts?

    Posted 3 years ago #
  13. ok- so we're on the same page that you're using a clean child theme. sorry i had to ask, b/c some folks come on here and AREN'T. also good to hear you already switched themes, and disabled plugins. that means it isn't thematic. i really thought the permalinks were it, b/c that happened to me yesterday. i changed to a new type of permalink, saved it and then the weird 404 errors (i wasn't getting the 404 page either) went away. i would try that again. pick one of the defaults. you could also delete your .htaccess (it is in the root folder of WP) and then save permalinks again. after that i'm pretty much out of ideas.

    if full changes to excerpt on modding the query... that could be bug, i'd have to look more into it.

    you don't need multiple functions to add multiple arguments to the query. it accepts arrays

    function mod_index_query() {
    global $wp_query;
    
    		$defaults = $wp_query->query_vars;
    		$custom = array('category_name'=> 'blog',
                                    'posts_per_page'=> '1');
    		$args = wp_parse_args( $custom, $defaults );
    		query_posts($args);
    
    }
    add_action('thematic_above_indexloop','mod_index_query');
    Posted 3 years ago #
  14. vicky515
    Member

    I figured out the issue... Or at least I figured out what was causing it.

    I had my permalinks set to %category%/%post-name% (i know it's not recommended) but the problem was that I had a period (.) as my category base so that I wouldn't see "/category/" in the URL. Worked perfectly fine for everything else (sigh). Didn't even think twice about it, really :( As soon as I removed that category base it worked perfectly. Aside from that ugly "/category/" in the URL now.

    So, I changed all the categories into pages except 'blog'.
    But now I need help with that :) I will open a new topic.

    Thank you so much for all your assistance Helga - you are awesome! I've bookmarked this thread as I'm sure I will be referring to it often <3

    Posted 3 years ago #
  15. see what you get for trying to be tricky? ;) i thought it sounded like a permalinks problem. you can change the "base" from categories to something less ugly like "topics" or "thoughts" or "musings". dont forget to mark the thread as resolved.

    Posted 3 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.