Page 1 of 1
newbie needs help! search query for last/next post on blog
Posted: Fri Mar 26, 2010 10:50 am
by jredsmyth
Hi,
I'm new to Wordpress. I've got my site (
http://www.uprlip.com) running almost exactly as I would like it to. Basically I'm running two seperate categories as two columns on the main page to make it appear to be two blogs essentially. I want to ad a forward post button, and a back post button to the main page while maintaining the dual-column category look. I've tried a few things and haven't been able to get it to work. The code I'm using to call the categories is located in the 'index.php' file and looks like this:
Code: Select all
<div id="content" class="narrowcolumnJ" role="main">
<div style="float:left;width:380px;padding:10px;">
<?php $my_query = new WP_Query('category_name=J&showposts=1'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endwhile; ?>
</div>
</div>
There's that section of code, then a section exactly like it for the 'M' category. I just need to have a forward and back button above my footer so people can click and get back or forwards. If they're on day, 2 go back to day, 1 ect ect ect.
If anybody has any ideas for me that would be mucho greato. Thank you very much.
Re: newbie needs help! search query for last/next post on blog
Posted: Fri Mar 26, 2010 8:14 pm
by zerkig
Have you tried the wordpress support forums? From memory these sort of buttons automatically appear with a non static theme. There are so many plugins that one might fit your needs.
If none of the above helped, try changing
Code: Select all
WP_Query('category_name=J&showposts=1');
to
Code: Select all
WP_Query('category_name=J&showposts=1&offset=2');
and same for other category
if that gives you day 2 (i.e. what you'd expect to see if you clicked your next button) then you could try creating a couple of standard form buttons in your footer and use request handling to increment/decrement the offset.
Re: newbie needs help! search query for last/next post on blog
Posted: Fri Mar 26, 2010 10:24 pm
by jredsmyth
Thank you so much Zerkig! The "&offset=(x);" work perfectly. If I put in '1' it goes back to the next to current, and '2' goes back 2 from the current and so on and so forth. And, yes I did try the wordpress forums... about four days ago and haven't gotten any responses at all. That seems to be the trend with the wordpress forums, unfortunately.
I'm sorry that I don't quite get how to put this code into button action yet... could you explain that to me at all? So that the main page displays the most current post until the button is clicked to get the "&offset=1" command?
Again thank you so much for the help. Hopefully you can help me with the making a button control this code.
-Jared
Re: newbie needs help! search query for last/next post on blog
Posted: Sat Mar 27, 2010 4:46 am
by zerkig
Here is a working example of an index.php which has a couple of placeholder buttons
Code: Select all
<html><body>
<?php
$myOffset = $_REQUEST["myOffset"];
echo $myOffset;
if (
isset($myOffset)
&& !filter_var($myOffset, FILTER_VALIDATE_INT)
&& 0 > $myOffset
)
{
// for some reason I don't like the input variable so reset to latest
$myOffset =0;
$myOffsetPrev = 0;
$myOffsetNext = 1;
}
{
$myOffsetPrev = $myOffset - 1;
$myOffsetNext = $myOffset + 1;
}
?>
<p>
<?php
echo "OffSet is ". $myOffset;
?>
</p>
<?php
if ( $myOffset > 0)
{
// add a previous link
echo "<A HREF=index.php?myOffset=". $myOffsetPrev . "><<Previous</A>";
}
echo "<A HREF=index.php?myOffset=" . $myOffsetNext . ">next>></A>";
?>
</body></html>
If you can merge this with your file you should be ok.
Re: newbie needs help! search query for last/next post on b
Posted: Sat Mar 27, 2010 10:22 am
by jredsmyth
Again, Kerkig... thank you SO much for your help. Still not affecting the content within columnM and columnJ for some reason though. The buttons work as they should (it seems), but they just aren't changing the content within the columns. Take a look at the code here and see if it's something small I missed. I included all of index.php just so that you would be able to see the whole thing.
Code: Select all
<?php
/**
* @package WordPress
* @subpackage Default_Theme
*/
get_header(); ?>
<?php
$myOffset = $_REQUEST["myOffset"];
echo $myOffset;
if (
isset($myOffset)
&& !filter_var($myOffset, FILTER_VALIDATE_INT)
&& 0 > $myOffset
)
{
// for some reason I don't like the input variable so reset to latest
$myOffset =0;
$myOffsetPrev = 0;
$myOffsetNext = 1;
}
{
$myOffsetPrev = $myOffset - 1;
$myOffsetNext = $myOffset + 1;
}
?>
<p>
<?php
echo "OffSet is ". $myOffset;
?>
</p>
<?php
if ( $myOffset > 0)
{
// add a previous link
echo "<A HREF=index.php?myOffset=". $myOffsetPrev . "><<Previous</A>";
}
echo "<A HREF=index.php?myOffset=" . $myOffsetNext . ">next>></A>";
?>
<div id="content" class="narrowcolumnJ" role="main">
<div style="float:left;width:380px;padding:10px;">
<?php $my_query = new WP_Query('category_name=J&showposts=1$myOffset()'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endwhile; ?>
</div>
</div>
<div id="content" class="narrowcolumnM" role="main">
<div style="float:left;width:380px;padding:10px;">
<?php $my_query = new WP_Query('category_name=M&showposts=1$myOffset()'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
<small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
<div class="entry">
<?php the_content('Read the rest of this entry »'); ?>
</div>
<p class="postmetadata"><?php the_tags('Tags: ', ', ', '<br />'); ?> Posted in <?php the_category(', ') ?> | <?php edit_post_link('Edit', '', ' | '); ?> <?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?></p>
</div>
<?php endwhile; ?>
</div>
</div>
</div>
<?php get_footer(); ?>
Thanks again.
-Jared
Re: newbie needs help! search query for last/next post on b
Posted: Sun Mar 28, 2010 7:14 am
by zerkig
OK
Go back to your original file.
Revised code similar to above but bug fixed
Code: Select all
<?php
$myOffset = filter_var($_REQUEST["myOffset"], FILTER_VALIDATE_INT);
if (
! empty($myOffset)
&& 0 < $myOffset
)
{
$myOffsetPrev = $myOffset - 1;
$myOffsetNext = $myOffset + 1;
} else
{
$myOffset =0;
$myOffsetPrev = 0;
$myOffsetNext = 1;
}
?>
Place the above near the top. It sets 3 variables depending on what it finds in the request.
Replace
Code: Select all
<?php $my_query = new WP_Query('category_name=M&showposts=1'); ?>
with
Code: Select all
<?php $my_query = new WP_Query('category_name=M&showposts=1&offset=' . $myOffset); ?>
Replace
Code: Select all
<?php $my_query = new WP_Query('category_name=J&showposts=1'); ?>
with
Code: Select all
<?php $my_query = new WP_Query('category_name=J&showposts=1&offset=' . $myOffset); ?>
at the bottom somewhere you'll have to play about add
the previous link you'll probably want to rename it as I've got it the wrong way round should maybe be "later"
Code: Select all
<?php
if ( $myOffset > 0)
{
// add a previous link
echo "<A HREF=index.php?myOffset=". $myOffsetPrev . "><<Previous</A>";
}
?>
and a next link
Code: Select all
<?php
echo "<A HREF=index.php?myOffset=" . $myOffsetNext . ">next>></A>";
?>
both assume the page is index.php
You'll need to mess around with it a little.
Re: newbie needs help! search query for last/next post on b
Posted: Sun Mar 28, 2010 8:45 am
by jredsmyth
Hell yeah! That works! Thank you so much! One of the first times I've had success posting a help topic on a forum and actually getting help. I feel like a should send you a formal thank you card or something! Thanks.
-Jared