views:

618

answers:

1

I've created a custom page and it is set as my homepage, within this custom page I am pulling out the latest post from a specific category, I've also created a form of pagination which when clicked upon will take the user to single.php. My intention for the single.php is to have two custom loops.

Custom loop one I want single.php to distinguish that it has came from the homepage and loop through all of the posts tagged with the same category as the one on the homepage.

Some of these posts will have to be tagged with more than one category, so the loop will have to know to ignore the other categories and just pay attention to the category in question. Does that make sense?

Custom loop two If the user hasn't arrived from the homepage, single.php will just act as it normally does i.e, if the user comes from index.php (the blog) they will be taken to this second loop (blog post)

However I don't seem to be able to make the distinction between the two loops, I might be over complicating matters, as I've got a loop which wraps everything together and then I have a loop for my custom pagination.

Here is the code below to show you what I'm talking about

custompage.php (set to home) - This works just fine but I'll post it just incase anyone is able to tidy it up

<?php query_posts('cat=1'); ?>

<?php
$myPosts = new WP_Query();
$myPosts->query('showposts=1');

if (have_posts()) :
while ($myPosts->have_posts()) : $myPosts->the_post();
?>

<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
<div id="post-<?php the_ID(); ?>" class="info">
      <h2><?php the_title(); ?></h2>
        <ul class="nav">
           <?php query_posts('posts_per_page=1&offset=1'); the_post(); ?>
           <li class="prev"><a href="<?php the_permalink() ?>">Previous</a></li>
            <?php wp_reset_query(); ?>
            <li class="next"></li>
        </ul>
</div>
<!-- end .info -->

<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

single.php - Currently broken

<?php if( in_category('1') ) { ?>
   <!-- start -->
   <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="info">
    <script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
            <h2><?php the_title(); ?></h2>
            <ul class="nav">
                <li class="prev"><?php previous_post_link('%link', '&nbsp;', 'true', '1') ?></li>
                <li class="next"><?php next_post_link('%link', '&nbsp;', 'true', '1'); ?></li>
                <!--li class="prev"><?php //previous_post_link('%link', '%title;', 'true', '1') ?></li>
                <li class="next"><?php //next_post_link('%link', '%title;', 'true', '1'); ?></li-->
            </ul>
    </div>
    <!-- end .info -->
    <?php endwhile; else: ?>
    <?php endif; ?>
   <!-- end -->

<?php }else{ ?>

   <div id="content" class="widecolumn" role="main">
   <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div <?php post_class() ?> id="post-<?php the_ID(); ?>">
         <h2><?php the_title(); ?></h2>
         <div class="entry">
            <?php the_content('<p class="serif">Read the rest of this entry &raquo;</p>'); ?>
         </div>
      </div>
   <?php comments_template(); ?>
   <?php endwhile; else: ?>
      <p>Sorry, no posts matched your criteria.</p>
    <?php endif; ?>
   </div>

<?php } ?>

The problem I seem to be running into is when a post has been tagged with two categories, wordpress doesn't seem to be able to make the distinction between the two categories and instead of carrying on to the next category it breaks and defaults to the second loop.

A: 

how about this?

HOMEPAGE
<?php
$myPosts = new WP_Query();
$myPosts->query('showposts=1&cat=1');

if (have_posts()) :
while ($myPosts->have_posts()) : $myPosts->the_post();
?>

<script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
<div id="post-<?php the_ID(); ?>" class="info">
      <h2><?php the_title(); ?></h2>
        <ul class="nav">
           <?php query_posts('posts_per_page=1&offset=1'); the_post(); ?>
           <li class="prev"><a href="<?php the_permalink() ?>">Previous</a></li>
           <?php wp_reset_query(); ?> <-- not sure if this will reset the overall query
           <li class="next"></li>
        </ul>
</div>    
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>

the queryposts('cat=1') at the top of your query wont run with that category, the category would have to be set inside your custom query

$myPosts->query('showposts=1&cat=1');

the 2nd page (SINGLE.PHP) with 2 loops.. if the user is coming from the homepage to a single page you want to attach the current category to the query, without this wordpress (while on the singles page will default to loop through all posts)

so for the singles page would the below be any good?

<?php if( in_category('1') ) { ?>
 <!-- your selected category -->
   <!-- start -->
   <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="info">
    <script type="text/javascript">$.backstretch("<?php $key="image"; echo get_post_meta($post->ID, $key, true);?>");</script>
            <h2><?php the_title(); ?></h2>
            <ul>
             <php global $post;
              $my_query = get_posts('numberposts=1&cat=1&offset=-1');
              foreach($my_query as $post) :
          setup_postdata($post);
          $link = get_post_meta($post->ID, 'site-url', true); ?>
     <li>
                <a href="<php echo $link; ?>"><php the_title(); ?></a>
             </li>
            <php endforeach; ?>
            <php global $post;
              $my_other_query = get_posts('numberposts=1&cat=1&offset=1');
              foreach($my_other_query as $post) :
          setup_postdata($post);
          $link = get_post_meta($post->ID, 'site-url', true); ?>
     <li>
                <a href="<php echo $link; ?>"><php the_title(); ?></a>
             </li>
            <php endforeach; ?>
           </ul>

    </div>
    <!-- end .info -->
    <?php endwhile; else: ?>
    <?php endif; ?>
   <!-- end -->

<?php }else{ 
    include('standard-wp-singles-page-stuff.php');
} ?>

then once your singles page loads it will check for the current category in that post, if its in category 1, then it will load your custom loop, then it will loop through 2 posts, in the same category? giving you 2 links to other posts. also using an offset on that query should give you a link forward and a link backwards?

hope that helps..

fixing code layout.. didnt work too well :P

Marty
<a href="<php echo $link; ?>"><php the_title(); ?></a> just seems to be echoing out the current page titles, with no url in the href.I'm not sure if the loop itself is incorrect or if it's just this ul.
petrescu
sorry m8.... maybe some other Wordpress guru's can look into this one.. :)
Marty