DotDragnet
February 04, 2012, 11:17:30 PM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Wordpress loop fun  (Read 812 times)
net-curtains
Guest
« on: April 30, 2010, 11:12:29 AM »

I've probably omitted something glaringly obvious, but if anyone can point out my mistake I'd appreciate it. I've created a custom template page to display posts from a specified category (information), which works fine but the 'next' and 'previous' links aren't working properly, and just show the same posts.

Code:
<?php get_header(); ?>

<div id="content" class="column">
<?php query_posts('category_name=information'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

<div class="entry">
 <h2><a href="<?php the_permalink() ?>" title="Permalink"><?php the_title(); ?></a></h2>
<p><?php the_excerpt(); ?></p>

<p class="info">
<?php if ($post->post_excerpt != '') { ?><a href="<?php the_permalink() ?>" class="more"><?php _e('Continue'); ?></a><?php ?>
<?php comments_popup_link(__('Leave a Comment'), __('1 Comment'), __('% Comments'), 'commentlink'''); ?>
<em class="date"><?php the_time(__('F j, Y')) ?><!-- at <?php the_time('h:ia')  ?>--></em>
<!--<em class="author"><?php the_author(); ?></em>-->
<?php edit_post_link('Edit','<span class="editlink">','</span>'); ?>
</p>

</div>

<?php endwhile; ?>

<p>
<span class="next"><?php previous_posts_link(__('Next page')) ?></span>
<span class="previous"><?php next_posts_link(__('Previous page')) ?></span>
</p>

<?php else : ?>

<h2><?php _e('Page not found'); ?></h2>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>

<?php endif; ?>

</div>

<?php get_sidebar(); ?>
<?php get_footer(); ?>

If anyone can point out my errors I'd be grateful.

TIA
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2122



View Profile WWW Awards
« Reply #1 on: April 30, 2010, 12:13:47 PM »

This page should help you - http://wordpress.org/support/topic/373060

However, any reason why you're not just creating a dedicated category.php template for this category and linking to that?
Logged

net-curtains
Guest
« Reply #2 on: April 30, 2010, 12:26:37 PM »

This page should help you - http://wordpress.org/support/topic/373060


Thanks for the link - doesn't seem to work unfortunately.
However, any reason why you're not just creating a dedicated category.php template for this category and linking to that?

I'm using pages, and using a custom template for each page which has the above code in it.

I think I might have been through all this before and ended up giving up and using a get_posts tag instead. I seem to have a blind spot with this.

Could I use a category template for the page instead of a page template?
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2122



View Profile WWW Awards
« Reply #3 on: April 30, 2010, 01:00:30 PM »

The trouble with using the query_post is that you need to pass the correct page in to get the right results. The next/previous links don't work otherwise. Here's something else to try - http://weblogtoolscollection.com/archives/2008/04/13/define-your-own-wordpress-loop-using-wp_query/

I've never really used it because I know it's harder to get working.

You can create individual category template pages for each category, however your link would still be domain/categorybase/categoryname (unless you drop the category base which is doable, let me know if you want a link on how to do it). You could always use the page links to plugin to get the category pages into the standard WP list pages menu by basically manipluating the menu.
Logged

net-curtains
Guest
« Reply #4 on: April 30, 2010, 01:27:03 PM »

The trouble with using the query_post is that you need to pass the correct page in to get the right results. The next/previous links don't work otherwise.

Thanks Sarah, I still can't get my head around this though.

I can get the category posts to display, it's just this annoying next/previous link that doesn't work. At the moment I'm using pages in the main nav, and have a page called 'Information'. I also have a category called 'information', and want to display posts from this category as a list in the Information page, 5 at a time with next and previous links for when there's more than 5 posts in the category. I've set up a custom page template (page-information.php) which contains the code I pasted previously.

Is there an example of some code that I can use for the page-information template that would work, or do I have to abandon the page template altogether and use instead a category template? If so, what code would I use in the template and how would I get it to appear in the menu.

Sorry if I'm asking something you've already told me how to do but I can't get my brain to understand the words so I need some real basics.

I'm not impressed with the Wordpress docs, they don't make this stuff easy to find, or maybe I'm just not understanding it.
Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2122



View Profile WWW Awards
« Reply #5 on: April 30, 2010, 03:17:30 PM »

Getting the category posts to display - easy. Getting the Next/Previous links to work - not so easy once you manipulate the query. That's the problem.

Personally I would do this.

Create a template file called category-X.php (or category-slug.php works I think now). Set the page up how you want it. If you need the text from the information page, use get_post to get the text from that specific page.

Install the page links to plugin. This then adds a box at the bottom of each page. Open up your information Page and go to this box, put in the link to the category page. On your menu you'll then have a link 'Information' but it'll link directly to the category page instead. I see no reason why you can't store any opening content for this category in the Information Page's content box, as mentioned, just use get_post() to get the page text.

Then your standard WP loop will pull out the posts from the category and your next/previous links will work.

If you want to stick with the page template method then the first link I gave should work, so possibly something else is amiss. I can try it in a page for you using your code above but it probably won't be until tomorrow or Sunday.

And yes, the codex isn't the easiest to understand smile
Logged

net-curtains
Guest
« Reply #6 on: April 30, 2010, 05:18:05 PM »

Personally I would do this.

Create a template file called category-X.php (or category-slug.php works I think now). Set the page up how you want it. If you need the text from the information page, use get_post to get the text from that specific page.

Install the page links to plugin. This then adds a box at the bottom of each page. Open up your information Page and go to this box, put in the link to the category page. On your menu you'll then have a link 'Information' but it'll link directly to the category page instead. I see no reason why you can't store any opening content for this category in the Information Page's content box, as mentioned, just use get_post() to get the page text.

Then your standard WP loop will pull out the posts from the category and your next/previous links will work.


Thanks Sarah, that's got the little bugger. All working fine now, I owe you a cake.


Logged
sarahA
DDN Contribs
Hero Member
*****
Posts: 2122



View Profile WWW Awards
« Reply #7 on: April 30, 2010, 07:25:40 PM »

No problem smile

(for some reason Mr A. said don't forget him... I think you should personally wink)
Logged

net-curtains
Guest
« Reply #8 on: April 30, 2010, 07:31:45 PM »

No problem smile

(for some reason Mr A. said don't forget him... I think you should personally wink)

I'm on a diet - I don't see why I should suffer alone. I've got plenty of Ryvitas in the cupboard.
« Last Edit: April 30, 2010, 07:48:59 PM by net-curtains » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!