0

I have a custom collection of around 900 wordpress blog posts

I have used custom pagination for this custom collection.

The problem is, when i click on the pagination button, the pager URL seems to load fine with /page2/, /page3/ etc, but same set of collection which is loaded in 1st page is loading in all pages.

UPDATE:

app\design\frontend\rau\default\template\wordpress\homepage.phtml

Initially i have collection defined like below:

<?php 
$collection = Mage::getResourceModel('wordpress/post_collection')
    ->setOrderByPostDate()
    ->addIsViewableFilter()
    ->addPostTypeFilter('post');
    $artical_type = Mage::app()->getRequest()->getParam('article_type');
    $message ='';
    if(!empty($artical_type)) {
        $posttypevalue = Mage::helper('wordpress')->getPostMetaData();
        if(!empty($posttypevalue)) {
            $collection = $collection->addFieldToFilter('ID',array('in' => $posttypevalue));
        } else {
            $message = 'No relevant article of this type found';
        }
    }
    $collection = $collection->setPageSize(12)->setCurPage(2);  

?>

<?php if (count($collection) > 0): $i=0; ?>
<div class="blog-articles aa">
    <?php foreach($collection as $value): 
    ...
    ...
   <?php endforeach; ?>
<?php endif; ?>

<?php if($this->getCollection()->getSize()): ?>
    <div class="blog-pagination">

        <?php if($this->getLastPageNum()>1): ?>
            <?php if (!$this->isFirstPage()): ?>

                    <a class="pagination-older previous<?php if(!$this->getAnchorTextForPrevious()): ?> i-previous<?php endif;?>" href="<?php echo $this->getPreviousPageUrl() ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Newer Posts')) ?>">
                    <i class="fa fa-chevron-left" aria-hidden="true"></i>
                        <?php if(!$this->getAnchorTextForPrevious()): ?>
                        <?php echo $this->__('Newer Posts') ?>

                        <?php else: ?>
                            <?php echo $this->getAnchorTextForPrevious() ?>
                        <?php endif;?>
                    </a>
            <?php endif;?>
            <?php if (!$this->isLastPage()): ?>
                    <a class="pagination-newer next<?php if(!$this->getAnchorTextForNext()): ?> i-next<?php endif; ?>" href="<?php echo $this->getNextPageUrl() ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Older Posts')) ?>">
                        <?php if(!$this->getAnchorTextForNext()): ?>
                        <?php echo $this->__('Older Posts') ?>
                        <i class="fa fa-chevron-right" aria-hidden="true"></i>
                        <?php else: ?>
                            <?php echo $this->getAnchorTextForNext() ?>
                        <?php endif;?>
                    </a>
            <?php endif;?>
        <?php endif; ?>     

    </div>
<?php endif ?>

In the Homepage block i have collection

UPDATED

app\code\community\Fishpig\Wordpress\Block\Homepage.php

class Fishpig_Wordpress_Block_Homepage extends Fishpig_Wordpress_Block_Post_List_Wrapper_Abstract
{
public function __construct()
{
    /*parent::__construct();

    $this->setPageVarName('page');

    $baseLimit = $this->helper('wordpress')->getWpOption('posts_per_page', 10);

    $this->setDefaultLimit($baseLimit);
    $this->setLimit($baseLimit);

    $this->setAvailableLimit(array(
        $baseLimit => $baseLimit,
    ));

    $this->setFrameLength(5);


    $collection = Mage::getResourceModel('wordpress/post_collection');
    $this->setCollection($collection);*/

    /*With this code below if i set CurPage(2), i get the 2nd page's collection
     But if i navigate to other page, still 2nd page collections shows*/
    parent::__construct();
    $collection = Mage::getResourceModel('wordpress/post_collection');
    $this->setCollection($collection);
}

protected function _prepareLayout()
{
    parent::_prepareLayout();

    $pager = $this->getLayout()->createBlock('wordpress/post_list_pager', 'wordpress_post_list.pager');
    $pager->setAvailableLimit(array(5=>5,10=>10,20=>20,'all'=>'all'));  //This limit doesnt seem to be set, its always using setPageSize(12) from phtml 
    $pager->setCollection($this->getCollection());
    $this->setChild('pager', $pager);
    $this->getCollection()->load();
    return $this;
}

public function getPagerHtml()
{
    return $this->getChildHtml('pager');
}

 public function getLastPageNum()
{
    return $this->getCollection()->getLastPageNumber();
}

public function isLastPage()
{
    return $this->getCollection()->getCurPage() >= $this->getLastPageNum();
}

public function getNextPageUrl()
{
    return $this->getPageUrl($this->getCollection()->getCurPage(+1));
}

/**
 * Retrieve url for previous page
 *
 * @return string
 */
public function getPreviousPageUrl()
{
    return $this->getPageUrl($this->getCurrentPage() - 1);
}

public function getPageUrl($page)
{
    return $this->getPagerUrl(array($this->getPageVarName()=>$page));
}


/**
 * Return the URL for a certain page of the collection
 *
 * @return string
 */ 
public function getPagerUrl($params=array())
{
    $pageVarName = $this->getPageVarName();

    $slug = isset($params[$pageVarName]) 
        ? $pageVarName . '/' . $params[$pageVarName] . '/'
        : '';

    $slug = ltrim($slug, '/');

    $baseUrl = $this->getUrl('*/*/*', array(
        '_current' => true,
        '_escape' => true,
        '_use_rewrite' => true,
        '_nosid' => true,
        '_query' => array('___refresh' => null),
    ));

    $queryString = '';

    if (strpos($baseUrl, '?') !== false) {
        $queryString = substr($baseUrl, strpos($baseUrl, '?'));
        $baseUrl = substr($baseUrl, 0, strpos($baseUrl, '?'));
    }

    return rtrim($baseUrl, '/') . '/' . $slug . $queryString;
}
  • 1
    and where exactly is the pagination applied to your blog posts? I see that you retrieve the post collection in your template and just set a limit but not a page number – Marius Jun 14 '18 at 11:59
  • Thanks for pointing that out ...Could you please help me out with the code .. I searched in core files, I'm not getting the right code and how to set the page number and where to place – Sushivam Jun 14 '18 at 16:18
  • Have updated the code @Marius, if i set CurPage(2), i get only 2nd page collection, if i go to page 3 from the pagination, it still loads 2nd pages results... – Sushivam Jun 14 '18 at 17:24
  • Could you help me with this @Marius – Sushivam Jun 15 '18 at 3:53
  • sorry, I don;t know how to proceed. – Marius Jun 15 '18 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.