Jump to content

Page rendering limits other queries in sidebar


MilenKo
 Share

Recommended Posts

Hello guys. I am super happy to be passing to the final cleanup of my first custom build theme and to start applying the SEO optimization modules/code. I noticed something strange this morning and can't find out where exactly is my issue. Hopefully it is a quick fix or silly miss on my side.

So here is the issue: I am generating a list of all my recipes in the db using the following query:

<?php 	if($page->numChildren) {

//Build a selector and limit page results to 10
$recipe_results = $page->children("limit=4");

//Assign results of the array to $listing
foreach($recipe_results as $index => $listing) {

//Limit the text content to 160 chars using the function in function.php
$text = $sanitizer->textarea(truncate("$listing->recipe_intro_text", 160));
?>

Then follows the boring part of html insert of the variables, clases etc. The listing works fine. So as far as there would be more than the planned results per page, I implemented the page rendering using the following code:

<!-- Pagination -->
<?php	
echo $recipe_results->renderPager(array(

//Show font awesome right hand icon for next page
'nextItemLabel' => __("<i class='fa fa-hand-o-right'></i>"),

//Show font awesome left hand icon for previous page
'previousItemLabel' => __("<i class='fa fa-hand-o-left'></i>"),	

//Define the active class of current page
'currentItemClass' => "current",

//Define the block markup code
'listMarkup' => "<ul class='page-nav'>{out}</ul>",

//Define the item markup code and applying currentItemClass
'itemMarkup' => "<li class='{class}'>{out}</li>",

//Generating the link appearance of all buttons
'linkMarkup' => "<a href='{url}'>{out}</a>",  

));  
?>
<!-- Pagination -->

And again, everything worked perfectly after I allowed page numbers. 

So then I implemented a few similar queries in the sidebar of my website, listing content from other page parents/children. Here is one example:

<?php 
	$favorite_books = $pages->find("template=books-inner, sort=-random, limit=3");
	foreach($favorite_books as $b) { 
	if (count($b->book_images)) {
	$thumb = $b->book_images->first->size(70,60)->url; 
	$desc = $b->book_images->first->description;
	}
	else {
	$thumb = $settings->small_thumb->url;
	$desc = "NoThumb";
	}
?>

<li>
	<div class="thumb">
		<a href="<?php echo $b->url?>">
			<img src="<?php echo $thumb?>" alt="<?php echo $desc?>"/>
		</a>
	</div>
	<div class="detail">
		<a href="<?php echo $b->url?>"><?php echo $b->title?></a>
	</div>
</li>

<?php } ?>

 

It was all working fine, until I started browsing page 2, 3, etc. It appears for some reason, that the pageRendering is applied not only to $recipe_results, but also to all requests from the widgets. So on page 1, I get the proper number of recipes and proper number of results in the widgets (books in this example). If I browse to page two, I still see the proper recipes number and everything else, but the widgets show only 1 result (as far as I have imported 4 test posts only). On page 3, it shows notihing in the widgets, but recipes are OK again.

As far as to render pages I am using $recipe_results and the pageRender is called for that variable, I am not sure why it is also applied to $favorite_books and other widgets. Any suggestions or ideas what is going on? I tried to change every variable on the page thinking that I might be having a call to the same one, but it is obvious in the example that they do not match...

Link to comment
Share on other sites

@kongondo Thanks for the link. I checked it up and it is interesting approach, however it seems like the post author was trying to render two sources on the same page and paginate the results where in my case I am just trying to show the results of two queries but pagination is needed only for the one.Will try to test the solution provided there, but it is not yet clear for me how that approach would fit in my scenario.

Edit: I found a simple way to fix the issue, and it was right under my nose in the pagination markup explanationTo make the queries work where I do not want the PageRendering to happen, I just added start=0 into the find query:

$favorite_books = $pages->find("template=books-inner, sort=-random, start=0, limit=3");

Thanks again for pointing me to the initial link which gave me an idea how to phrase my search and bring me to the right spot. As far as I wanted to pull N-random requests, it works perfect. One more step closer to the end ;)

  • Like 2
Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...