Jump to content

Archived

This topic is now archived and is closed to further replies.

xavaireland

Pagination not working in a Custom loop

Recommended Posts

xavaireland    2
xavaireland

I wrote a custom section to conduct a multi-taxonomy search for a client. The search functionality works perfectly but the pagination in the section doesnt - the loop shows the same results on every page. Any feedback is welcome

 

The code for the section.php is posted here:

<?php
/*
	Section: Tyrebroker Custom Product Search
	Author: Amit Wadhwa
	Author URI: http://xava.ie
	Description: A base section to be used to start new sections.
	Class Name: tbCustomSearch
	Demo:
	Version: 1.2
	Filter: component
*/

/**
 * DMS Meta Info Tips

	// You can set the filter value to one of the following: component, layout, full-width, format, gallery , nav, slider, social, widgetized, misc
	// There are a few additional headers you can utlize. One of which is a full-width filter that forces the section full-width. The other is an active loading filter, which, when applied, doesn't require a page refresh for the section to show up. however, sections with javascript shouldn't use this.
*/



class tbCustomSearch extends PageLinesSection {

	const version = '1.2';  // Declares a version, used in tracking the version of the script. For some reason it's needed with DMS along with declaring true at the end of the script to force into the footer

    // READY TO USE VARIABLES
    // $this->id;          section slug, in this case its nb-section
    // $this->base_url;    the base url of the section
    // $this->base_dir;    the base directory of the section
    // $this->images;      create an /images directory in your section, then use this variable for the path
    // $this->screenshot;  the section thumb
    // $this->splash;      the section splash
    // $this->description  get the description from the header
    // get_the_id();       retrieves the clone id of the section

    // RUNS ALL TIME - This loads all the time, even if the section isn't on the page. Stuff like actions and things can go here, as well as post type setup functions
    function section_persistent(){}

    // LOAD SCRIPTS
    function section_scripts(){
        //wp_enqueue_script('script-name', $this->base_url.'/script.js', array('jquery'), self::version, true );
    }

    // RUNS IN <HEAD>
    function section_head() {

    	// Always use jQuery and never $ to avoid issues with other store products

        /*
    	?><script>
	    	jQuery(document).ready(function(){

	    	});
    	</script><?php
    	*/

        // This is only needed if you'll be using custom fonts
        // echo load_custom_font($this->opt('nb_custom_font'), '.target-class');

    }

    // BEFORE SECTION - this adds a class to the section wrap. you can also put HTML here and it will run outside of the section, and before it
    function before_section_template( $location = '', $clone_id = null ) {

		//$this->wrapper_classes['background'] = 'special-class';

	}

    // SECTION MARKUP - This is the function that outputs all the HTML onto the page. Put all your viewable content here
   	function section_template() {
   		//global $product, $post;
   		global $my_products;
		$width = $_GET['width'];
		$aratio = $_GET['aratio'];
		$dia = $_GET['dia'];
		$paged=$_GET['paged']; if (!$paged){$paged = 1;}
		?>
		<div class="bordered" style="margin-top:20px;">
			<div class="row">
				<span class="span12">
					<span style="font-size: 20px; font-weight: bold;display:inline; margin-bottom: 0px; margin-top: 0px;">Search our stock for the cheapest fitted tyres in Dublin</span>
				</span>
			</div>
			<div class="row" style="margin-bottom: 0px;">
				<div class="span12">	
					<form id="tyresearch" name="search" method="GET" action="http://www.tyrebroker.ie/your-search" style="margin: 0px"> 
						<fieldset style="width:25%;display:inline">
							<select name="width" style="width: 100%;">
								<option value="0">Tyre Width</option><option value="135"<?php if ($width == 135) echo " selected='selected'"; ?>>135</option><option value="145"<?php if ($width == 145) echo " selected='selected'"; ?>>145</option><option value="155"<?php if ($width == 155) echo " selected='selected'"; ?>>155</option><option value="165"<?php if ($width == 165) echo " selected='selected'"; ?>>165</option><option value="175"<?php if ($width == 175) echo " selected='selected'"; ?>>175</option><option value="185"<?php if ($width == 185) echo " selected='selected'"; ?>>185</option><option value="195"<?php if ($width == 195) echo " selected='selected'"; ?>>195</option><option value="205"<?php if ($width == 205) echo " selected='selected'"; ?>>205</option><option value="215"<?php if ($width == 215) echo " selected='selected'"; ?>>215</option><option value="225"<?php if ($width == 225) echo " selected='selected'"; ?>>225</option><option value="235"<?php if ($width == 235) echo " selected='selected'"; ?>>235</option><option value="245"<?php if ($width == 245) echo " selected='selected'"; ?>>245</option><option value="255"<?php if ($width == 255) echo " selected='selected'"; ?>>255</option><option value="265"<?php if ($width == 265) echo " selected='selected'"; ?>>265</option><option value="275"<?php if ($width == 275) echo " selected='selected'"; ?>>275</option><option value="285"<?php if ($width == 285) echo " selected='selected'"; ?>>285</option><option value="295"<?php if ($width == 295) echo " selected='selected'"; ?>>295</option><option value="305"<?php if ($width == 305) echo " selected='selected'"; ?>>305</option><option value="315"<?php if ($width == 315) echo " selected='selected'"; ?>>315</option><option value="325"<?php if ($width == 325) echo " selected='selected'"; ?>>325</option>
							</select>
						</fieldset >
						<fieldset style="width:25%;display:inline">
							<select name="aratio" style="width:100%">
								<option value="0">Tyre Profile</option><option value="25"<?php if ($aratio == 25) echo " selected='selected'"; ?>>25</option><option value="30"<?php if ($aratio == 30) echo " selected='selected'"; ?>>30</option><option value="35"<?php if ($aratio == 35) echo " selected='selected'"; ?>>35</option><option value="40"<?php if ($aratio == 40) echo " selected='selected'"; ?>>40</option><option value="45"<?php if ($aratio == 45) echo " selected='selected'"; ?>>45</option><option value="50"<?php if ($aratio == 50) echo " selected='selected'"; ?>>50</option><option value="55"<?php if ($aratio == 55) echo " selected='selected'"; ?>>55</option><option value="60"<?php if ($aratio == 60) echo " selected='selected'"; ?>>60</option><option value="65"<?php if ($aratio == 65) echo " selected='selected'"; ?>>65</option><option value="70"<?php if ($aratio == 70) echo " selected='selected'"; ?>>70</option><option value="75"<?php if ($aratio == 75) echo " selected='selected'"; ?>>75</option><option value="80"<?php if ($aratio == 80) echo " selected='selected'"; ?>>80</option><option value="85"<?php if ($aratio == 85) echo " selected='selected'"; ?>>85</option>
							</select>
						</fieldset >
						<fieldset style="width:25%;display:inline">
							<select name="dia" style="width:100%">
								<option value="0">Tyre Size</option><option value="8"<?php if ($dia == 8) echo " selected='selected'"; ?>>8</option><option value="10"<?php if ($dia == 10) echo " selected='selected'"; ?>>10</option><option value="12"<?php if ($dia == 12) echo " selected='selected'"; ?>>12</option><option value="13"<?php if ($dia == 13) echo " selected='selected'"; ?>>13</option><option value="14"<?php if ($dia == 14) echo " selected='selected'"; ?>>14</option><option value="15"<?php if ($dia == 15) echo " selected='selected'"; ?>>15</option><option value="16"<?php if ($dia == 16) echo " selected='selected'"; ?>>16</option><option value="17"<?php if ($dia == 17) echo " selected='selected'"; ?>>17</option><option value="18"<?php if ($dia == 18) echo " selected='selected'"; ?>>18</option><option value="19"<?php if ($dia == 19) echo " selected='selected'"; ?>>19</option><option value="20"<?php if ($dia == 20) echo " selected='selected'"; ?>>20</option><option value="21"<?php if ($dia == 21) echo " selected='selected'"; ?>>21</option><option value="22"<?php if ($dia == 22) echo " selected='selected'"; ?>>22</option>
							</select>
						</fieldset >
						<fieldset style="width:15%;display:inline">
							<input type="hidden" name="paged" value="1">
							<input type="submit" value="Find Tyres"  style="width:100%; vertical-align:bottom"/>
						</fieldset >
					</form>
				</div>	
			</div>
			<div class="clear"></div>
		</div>
		<?php
		if ( ($_GET['width'] == 0) && ($_GET['aratio'] == 0) && ($_GET['dia'] == 0) ) { 
			echo 'Please select a tyre size to search from or take a look at our most popular tyres.';
		} else { if ($width && !$aratio && !$dia){
			$aratiop = "--";
			$diap = "--";
			$my_products = new WP_Query(
				array(
					'paged' => $paged,
					'post_type' => 'product',
					'tax_query' => array(
						'relation' => 'AND',
						array(
							'taxonomy' => 'pa_width',
							'field' => 'slug',
							'terms' => $width)
					),
					'orderby' => 'meta_value_num',
					'meta_key' => '_regular_price',
					'order' => 'ASC'
				)
			);
		} else if ($width && $aratio && !$dia){
			$aratiop = $aratio;
			$diap = "--";
			$my_products = new WP_Query(
				array(
					'paged' => $paged,
					'post_type' => 'product',
					'tax_query' => array(
						'relation' => 'AND',
						array(
							'taxonomy' => 'pa_width',
							'field' => 'slug',
							'terms' => $width),
						array(
							'taxonomy' => 'pa_aratio',
							'field' => 'slug',
							'terms' => $aratio)
					),
					'orderby' => 'meta_value_num',
					'meta_key' => '_regular_price',
					'order' => 'ASC'
				)
			);
		}else if ($width && $aratio && $dia){
			$aratiop = $aratio;
			$diap = $dia;
			$my_products = new WP_Query(
				array(
					'orderby' => 'meta_value_num',
					'meta_key' => '_regular_price',
					'order' => 'ASC',
					'paged' => $paged,
					'post_type' => 'product',
					'tax_query' => array(
						'relation' => 'AND',
						array(
							'taxonomy' => 'pa_width',
							'field' => 'slug',
							'terms' => $width),
						array(
							'taxonomy' => 'pa_aratio',
							'field' => 'slug',
							'terms' => $aratio),
						array(
							'taxonomy' => 'pa_dia',
							'field' => 'slug',
							'terms' => $dia)
					)
				)
			);
		}
			if (!($my_products->have_posts())){ ?>
				<div class="notfound">
					Your search for <?php echo $width;?>/<?php echo $aratiop; ?>R<?php echo $diap; ?> yielded no results<br>
					Please contact us for prices.
				</div>
			<?php
			}else{ 
				$i=0;
				$numposts = $my_products->have_posts() ? sizeof($my_products->posts) : 0; 
				$num_pages = $my_products->max_num_pages;
				// * sizeof($my_products->posts); ?>
				<div class="restitle" style="float:left"> Search Results for tyre sizes <?php echo $width; ?>/<?php echo $aratiop; ?>R<?php echo $diap; ?> </div><div style="float:right; display:inline">Page <?php echo $i+1; ?> of <?php echo $num_pages;  ?></div>. </div>
				<div class="clear"></div>
				<p>
				<div id = "search_heading" class="row" style="border:1px solid rgba(102,155, 204, 0.5);padding:10px  0px;">
					<div class="span2" style="margin: 0px; padding: 5px"><strong>Brand</strong></div>
					<div class="span5" style="margin: 0px; padding: 5px"><strong>Product Details</strong></div>
					<div class="span1" style="margin: 0px; padding: 5px"><strong>LR/SR<sup><a href="#notes">*</a></sup></strong></div>
					<div class="span1" style="margin: 0px; padding: 5px"><strong>EU<sup><a href="#notes">**</a></sup></strong></div>
					<div class="span1" style="margin: 0px; padding: 5px"><strong>Price</strong></div>
					<div class="span2" style="margin: 0px; padding: 5px"><strong> </strong></div>
				</div>
						
				<?php while ($my_products->have_posts()) : $my_products->the_post(); global $product;					
					// get the terms related to post
					$brands = get_the_terms( $post->ID, 'pa_brand' );
					foreach( $brands as $tyreBrand ) {$tyrebrand = $tyreBrand->name;} 
					$widths = get_the_terms( $post->ID, 'pa_width' );
					foreach( $widths as $tyreWidth ) {$tyrewidth = $tyreWidth->name;} 
					$aratios = get_the_terms( $post->ID, 'pa_aratio' );
					foreach( $aratios as $tyreAratio ) {$tyrearatio = $tyreAratio->name;} 
					$dias = get_the_terms( $post->ID, 'pa_dia' );
					foreach( $dias as $tyreDia ) {$tyredia = $tyreDia->name;}
					$euratings = get_the_terms( $post->ID, 'pa_eurating' );
					if ($euratings){
						foreach( $euratings as $tyreEU ){$tyreeu = $tyreEU->name;}
					}else{
						$tyreeu = 'Not Supplied';
					}
					$lspeds = get_the_terms( $post->ID, 'pa_lsped' );
					foreach( $lspeds as $tyreLS ) {$tyrels = $tyreLS->name;} 
					?>
								<div id = "post-<?php the_ID(); ?>" <?php post_class(); ?> >				
						<div class="prow-<?php echo $i; ?> row <?php if($i%2 == 0){echo "row-grey";} ?>" style="margin-bottom:0px;border-bottom:1px solid rgba(102,155, 204, 0.5);border-right:1px solid rgba(102,155, 204, 0.5);border-left:1px solid rgba(102,155, 204, 0.5);">
							<div class="span2" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo $tyrebrand; ?></span></a></div>
							<div class="span5" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo the_title(); ?></span></a></div>
							<div class="span1" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo $tyrels; ?></span></a></div>
							<div class="span1" style="margin: 0px; padding: 5px"><a href="<?php echo the_permalink(); ?>" title="<?php the_title(); ?>"><span class="plinks"><?php echo $tyreeu; ?></span></a></div>
							<div class="span1" style="margin: 0px; padding: 5px">
<span class="price"><?php echo $product->get_price_html(); ?></span></div>
							<div class="span2" style="margin: 0px; padding: 5px">
								<div class="product-avlb">
									<?php if($product->is_in_stock() ) { 
										$content = "[add_to_cart id=\"".$post->ID."\"]";?>
										<?php echo do_shortcode( $content ); ?>
										<form class="cart" method="post" enctype='multipart/form-data'>
	 										<?php do_action( 'woocommerce_before_add_to_cart_button' ); ?>
	 										<?php
									 		if ( ! $product->is_sold_individually() )
									 			woocommerce_quantity_input( array(
									 				'min_value' => apply_filters( 'woocommerce_quantity_input_min', 1, $product ),
									 				'max_value' => apply_filters( 'woocommerce_quantity_input_max', $product->backorders_allowed() ? '' : $product->get_stock_quantity(), $product )	) );
	 										?>
	 										<input type="hidden" name="add-to-cart" value="<?php echo esc_attr( $product->id ); ?>" />
	 										<button type="submit" class="single_add_to_cart_button button-alt"><?php echo $product->single_add_to_cart_text(); ?></button>
	 										<?php do_action( 'woocommerce_after_add_to_cart_button' ); ?>
										</form>
	
									<?php }
									else { ?>
										<div style="align:right"><a id="not-in-stock-link" href="http://www.tyrebroker.ie/contact-us" class="button">Contact Us</a></div>
									<?php }?>
								</div>
							</div>					
						</div>
					</div>
					<div class="clear"></div>	        
					<?php $i++;
				endwhile;
			}
			wp_reset_query();
			if($my_products->max_num_pages>1){ ?>
				<nav class="woocommerce-pagination pager">
					<?php
						echo paginate_links( apply_filters( 'woocommerce_pagination_args', array(
							'base' 			=> str_replace( 999999999, '%#%', get_pagenum_link( 999999999 ) ),
							'format' 		=> '',
							'current' 		=> max( 1, $paged),
							'total' 		=> $my_products->max_num_pages,
							'prev_text' 	=> '&larr;',
							'next_text' 	=> '&rarr;',
							'type'			=> 'list',
							'end_size'		=> 3,
							'mid_size'		=> 3
						) ) );
					?>
				</nav>
			<?php } ?>
			<h3 class="headline">Can't find what you need, <a href="http://www.tyrebroker.ie/contact-us">contact us</a> for a quote.</h3>
			<div class="footnote">All tyrebroker.ie quotes are valid till the end of business on the date of the quote or until stocks last.</div><p></p>		
		<?php } 
        // call settings like so
        // $var = $this->opt($this->id.'_some_key');


        // print iterated options - as of 1.1

   	}

    // RUNS IN <FOOTER> - This is just like using wp_footer so this stuffs will in the footer of your site
	function section_foot(){}


    // WELCOME MESSAGE - HTML content for the welcome/intro option field
	function welcome(){

		ob_start();

		?><div style="font-size:12px;line-height:14px;color:#444;"><p><?php _e('You can have some custom text here.','nb-section');?></p></div><?php

		return ob_get_clean();
	}

    // SECTION OPTIONS - draws out the section options. This symbol * denotes optional fields.

} // that's it, that's the end of it. never put code past this area as it's then out of the class

Share this post


Link to post
Share on other sites
James B    436
James B

Hi there, the moderator team aren't developers so we're not the best people to advise on the above unfortunately as it's outside of our immediate support spectrum.

 

Are you a member of the Developer Community/300? If you are ask in their skype room and they might be able to advise further for you. http://www.pagelines.com/the-300/


Kindly search the forum and read the documentation before posting. It will help you resolve many issues.

For CSS help be sure to check out W3Schools first and be sure to download FireBug for FireFox for troubleshooting.

James B

Share this post


Link to post
Share on other sites

  • Similar Content

    • jojoking
      By jojoking+
      I have to admit to being baffled.
      I'm trying to embed a video (from my WP media library) within a WP post using the videobox section shortcode.
      The shortcode section popup only allows two items to be configured - the section type, in this case videobox - and a unique id. There's no URL or other parameters such as autoplay or loop which are available within the Pagelines editor.
      Whatever I put in the unique ID makes no difference to what is shown which seems to be a default video with stars or white noise on a black background.
      I assume I need to define this ID somewhere else, or maybe WP has done it for me?
      HELP!
       
      PS This is the page source generated when a shortcode is added within the WP post editor
      <!-- VideoBox | Section --> <section id="videobox_1675853156" class="pl-sn pl-col-sm-12 pl-col-sm-offset-0 pl-sn-videobox" data-object="PL_VideoBox" data-clone="1675853156" data-level="0"><div class="pl-sn-wrap"><div class="pl-sn-pad fix" data-bind="pledit: true" > <div class="video-highlight"><h3 class="video-title" data-bind="pltext: vb_title"></h3></div> <div class="pl-video-box" data-bind="plcallback: { vb_autoplay: vb_autoplay(), vb_loop: vb_loop(), vb_controls: vb_controls(), vb_video: vb_video()}" data-callback="videobox"> <video id="280414138" autoplay loop > <source src="https://www.handinhandmanchester.org.uk/wp-content/plugins/pl-section-videobox/default.mp4?r=280414138" type="video/mp4" /> No browser html5 video. </video> <script> jQuery('#280414138')[0].load(); </script> </div> </div></div></section> </div>  
      If I add a videobox section using the Pagelines page editor the page source generated includes my video:
      <!-- VideoBox | Section --> <section id="videobox_udvu4uz" class="pl-sn pl-col-sm-12 pl-col-sm-offset-0 pl-sn-videobox" data-object="PL_VideoBox" data-clone="udvu4uz" data-level="0"><div class="pl-sn-wrap"><div class="pl-sn-pad fix" data-bind="pledit: true" > <div class="video-highlight"><h3 class="video-title" data-bind="pltext: vb_title"></h3></div> <div class="pl-video-box" data-bind="plcallback: { vb_autoplay: vb_autoplay(), vb_loop: vb_loop(), vb_controls: vb_controls(), vb_video: vb_video()}" data-callback="videobox"> <video id="429558737" autoplay loop > <source src="https://www.handinhandmanchester.org.uk/wp-content/uploads/2017/04/HIHWelcome.mp4?r=429558737" type="video/mp4" /> No browser html5 video. </video> <script> jQuery('#429558737')[0].load(); </script> </div> </div></div></section>  
    • flourishdesignstudio
      By flourishdesignstudio
      Hi,
      I am having an issue with the Lax sections on Platform 5. I can embed a widget or text box into a Lax section which is really neat, but the image in the background (the parallax image) seems to get stretched and doesn't scale in an attractive way.  For example, the page http://ed6.61e.myftpupload.com/#clients has a parallax section with a woman's face  and I thought is would scale as the browser window gets smaller but the zoom-level never changes. Also, the amount of parallax shift seems more dramatic than what we want. I thought that if I uploaded a shorter image it would scan/scroll less since the height from the image's top to bottom would be less. However, that doesn't seem to have any effect and I am at a loss of how to properly control the way the image is displayed within that box. Any thoughts, tips, or suggestions to get me pointed in the right direction?
      Thanks so much for the help!
    • PierreFF
      By PierreFF+
      Hello Everyone, 
      I think this one is for the pagelines team. 
      I have some trouble with the Adventure section plugin https://www.pagelines.com/product/pl-section-adventure/ that doesn't seem to load in any of the three websites I run using pagelines. 
      I do the setup via the extend platform as usual. It tells me that the plugin is installed and activated but unfortunately it doesn't appear on the page editor.
      I have just tried it consecutively with three websites I own with the same result.
      My intention was to use it with this website: http://launchpadweekend.foundersofthefuture.co/ (free version)
      And I also tried with this website http://foundersofthefuture.co/ (pro license) and this one http://ffactor.me/ (free version). And I have the same problem with each. 
      I would love to see how I could solve this issue. 
      Thanks a lot. 
      Pierre
       
    • chamois_blanc
      By chamois_blanc+
      What is the right/best way to make a one-page website with PL5? The framework seems to hardcode section IDs without any possibility of changing them. Scrolling to a page section requires the link to use the section ID, ie <a href="#<section_ID>">...</a>. I don't see myself hardcoding weird section IDs in there as this will show up in the address bar.
      Please advise, thanks!
    • chamois_blanc
      By chamois_blanc+
      Hello,
      I am a bit confused about the usage of percentages for section padding/margin settings in the PL5 UI. Percentages don't scale well for mobile devices (especially in height), so those settings are pretty much useless. Case in point, the defaults are expressed in rem units, not percentages:
      .pl-sn-pad { padding: 1rem; ... } Why force the user to use percentages in the PL5 section UI when it would have been as easy to let the user specify the unit?
×