treehouse : what would you like to learn today?
Web Design Web Development iOS Development

can any wordpress legend help me with a simple if statement please?

  • Hi guys!

    ok this is probably simple for someone that understands the loop.

    on one page i am showing 5 posts, but when you click "older entries" it doesn't display the next 5 posts. it displays the same posts again... so i am doing something wrong.

    this is the page that i am talking about... http://star-signings.com/category/deals/

    disclaimer: i am using category.php to control both the deals and the news section (i don't know if i should have seperate pages for each?) - if someone could explain the pros and cons of having loops for different pages in the category.php then that would be absolutley amazing...

    anyway this is the loop i am using at the moment and i think i need something added to it, so that it works properly when you click "older entries"

    <?php if (is_category('deals')) { 
    query_posts('category_name=deals&posts_per_page=5');
    };
    ?>


    obviously this is saying, if it's the deals page, show 5 posts, but how do i get it to work properly?

    below is the whole code for category.php just incase it helps! =

    THANK YOU SO MUCH FOR HELPING ME! I WOULD LOVE YOU IF YOU COULD HELP ME WITH THIS!!!

    <?php get_header(); ?>


    </div>
    <div class="clr"></div>

    <div class="clr"></div>
    <div class="header_text">

    <h2>Star Management Signings</h2>
    </div>

    </div>
    </div>

    <div class="body_resize">
    <div class="body">
    <div class="left">

    <?php if (is_category('news')) { ?>
    <h2>Latest News</h2>
    <?php } elseif (is_category('deals')) { ?>
    <h2>Our Deals</h2>
    <p>Here is a selection of deals that Star Management have been involved in, either representing the club or on behalf of the player:</p>
    <?php } elseif (is_category('featured')) { ?>
    <p>My beautiful posts baby!</p>
    <?php } else { ?>players
    <p>This is some generic text to describe all other category pages,
    I could be left blank</p>
    <?php } ?>
    <?php if (have_posts()) : ?>

    <?php $post = $posts[0]; // Hack. Set $post so that the_date() works. ?>

    <?php /* If this is a category archive */ if (is_category()) { ?>
    <!-- <h2> Star Management Signings <?php single_cat_title(); ?></h2>
    blocked out cos i have cat dependant headers at the top -->
    <?php /* If this is a tag archive */ } elseif( is_tag() ) { ?>
    <h2>Posts Tagged &#8216;<?php single_tag_title(); ?>&#8217;</h2>

    <?php /* If this is a daily archive */ } elseif (is_day()) { ?>
    <h2>Archive for <?php the_time('F jS, Y'); ?></h2>

    <?php /* If this is a monthly archive */ } elseif (is_month()) { ?>
    <h2>Archive for <?php the_time('F, Y'); ?></h2>

    <?php /* If this is a yearly archive */ } elseif (is_year()) { ?>
    <h2 class="pagetitle">Archive for <?php the_time('Y'); ?></h2>

    <?php /* If this is an author archive */ } elseif (is_author()) { ?>
    <h2 class="pagetitle">Author Archive</h2>

    <?php /* If this is a paged archive */ } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?>
    <h2 class="pagetitle">Blog Archives</h2>

    <?php } ?>

    <!-- this fucking works!! -->
    <!-- if it's "deals" category then query the posts with a category of "deals"
    and post 20 per page -->

    <?php if (is_category('deals')) {
    query_posts('category_name=deals&posts_per_page=5');
    };
    ?>
    <?php if (is_category('news')) {
    query_posts('category_name=news&posts_per_page=10');
    };
    ?>
    <!-- this fucking works!! -->
    <!-- i rule -->

    <?php while (have_posts()) : the_post(); ?>



    <div <?php post_class() ?>>

    <!--
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    -->

    <div class="entry">

    <?php if (is_category('news')) { ?>
    <h2><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    <?php } elseif (is_category('deals')) { ?>
    <h2 id="post-<?php the_ID(); ?>"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h2>
    <?php } elseif (is_category('featured')) { ?>
    <p>My beautiful posts baby!</p>
    <?php } else { ?>players
    <p>This is some generic text to describe all other category pages,
    I could be left blank</p>
    <?php } ?>

    <div class="clr"></div>
    </div>


    </div>

    <?php endwhile; ?>
    <div class="clr"></div>
    <?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>

    <?php else : ?>

    <h2>Nothing found</h2>

    <?php endif; ?>


    <div class="clr"></div>


    <div class="clr"></div>
    </div>

    <div class="right"> <h2 class="subscribe">Subscribe</h2> <div class="clr"></div>
    <a href="http://silvermanwebdesign.co.uk/star/category/news/feed"&gt; <img src="<?php bloginfo('template_url'); ?>/images/rss_5.gif" alt="rss" class="floatleft"/></a>
    <div class="clr"></div>


    <?php get_sidebar();?>
    </div>
    <div class="clr"></div>

    </div>
    <div class="clr"></div>
    </div>


    <?php get_footer(); ?>

  • I had this problem on my site. Now - I'm very new to all this, so I don't guarantee anything :-)
    From my understanding, query_posts does not work with pagination (older entries link, etc.) You have to include the paged argument. Here's where I found out about it:
    http://codex.wordpress.org/Function_Reference/query_posts
    Try this code for your query:


    <?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
    'category_name' => 'deals',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    query_posts($args);
    ?>









  • Oops, I forgot the conditional statement for your 'deals' category:


    <?php if (is_category('deals')) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
    'category_name' => 'deals',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    query_posts($args);
    }
    ?>


    I think this might work :-)
  • Hi Hayley!

    wow thanks soo much for your help. I used it and it works! i am having issues using it in the news section for some reason, but it works in the deals part!

    thanks very much for everything!

    i have used it like this... the first loop works a treat, but the second loop doesn't! i don't know why, but maybe someone else could help?!

    <?php if (is_category('deals')) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
    'category_name' => 'deals',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    query_posts($args);
    }
    ?>
    <?php if (is_category('news')) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
    'category_name' => 'news',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    query_posts($args);
    }
    ?>
  • Hmm...I've never done 2 like that. Wait, maybe it needs to be an if/elseif statement. That would make more sense. Taking a stab at it here:


    <?php if (is_category('deals')) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
    'category_name' => 'deals',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    query_posts($args);
    }
    elseif (is_category('news')) {
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $args=array(
    'category_name' => 'news',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    query_posts($args);
    }
    ?>

  • <?php

    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

    if (is_category('deals')) {

    $args=array(
    'category_name' => 'deals',
    'post_per_page' => 5,
    'paged'=>$paged,
    );

    }
    elseif (is_category('news')) {
    $args=array(
    'category_name' => 'news',
    'post_per_page' => 5,
    'paged'=>$paged,
    );
    }

    query_posts($args);

    ?>


    It's the same as @hayley's code, just with less "stuff".
  • thanks @betzster - that's much better. I really need to learn PHP :-)
  • @hayley so do I ;).
  • Hi.

    There is something very important about the loop and using the query_posts function that you are missing. It is the reason why your first attempt at having two loops did not work. After each loop, you must reset the query. It is important to do, even when you are only running query_posts once on a page.

    How do you do it? Just add a line like this at the end of the loop after the endwhile at a minimum and if you have navigation at the bottom of the page that takes the user to more pages of stuff in the same loop, then be sure to put this line _after_ that as well.

    <?php wp_reset_query(); ?>


    Hope this helps. I just figured this out the other day and it has enabled me to do all kinds of fancy things now.

    Cheers!
  • <?php if (is_category('lightbox')) { 

    };
    ?>

    I learned this much from this post.

    I have created posts for different lightboxs.

    For lightbox post i have catagory called 'lightbox' When i click on link.

    [lightbox href="http://localhost/?p=229" iframe="true" width="700" height="460"]Open [/lightbox]

    It opens whole post with header and sidebar and everything.

    I was wondering if we can hide header, sidebar, footer everything except that post data. That would be great...

    Please let me know if its possible and how can i achive this.

    Thanks a ton.

    * I am using twenty eleven theme.