1

I have a Block class under the path, which im trying to access a function getPosts()

UPDATED:

app\code\community\Fishpig\Wordpress\Block\Post\List.php

public function getPosts()
{
    return $this->_getPostCollection();
}

protected function _getPostCollection()
{
$artical_type = Mage::app()->getRequest()->getParam('article_type');
    if (is_null($this->_postCollection) && $this->getWrapperBlock()) {
        $this->_postCollection = $this->getWrapperBlock()->getPostCollection();
        
        if ($this->_postCollection) {
            if ($this->getPostType()) {
                $this->_postCollection->addPostTypeFilter($this->getPostType());
                if(!empty($artical_type)) {
                    $posttypevalue = Mage::helper('wordpress')->getPostMetaData();
                    if(!empty($posttypevalue)) {
                        $this->_postCollection = $this->_postCollection->addFieldToFilter('ID',array('in' => $posttypevalue));
                    }
                }
            }
            
            if ($this->getPagerBlock()) {
                $this->getPagerBlock()->setCollection($this->_postCollection);
            }
        }
    }
    
    return $this->_postCollection;
}

/**
 * Sets the parent block of this block
 * This block can be used to auto generate the post list
 *
 * @param Fishpig_Wordpress_Block_Post_List_Wrapper_Abstract $wrapper
 * @return $this
 */
public function setWrapperBlock(Fishpig_Wordpress_Block_Post_List_Wrapper_Abstract $wrapper)
{
    return $this->setData('wrapper_block', $wrapper);
}

/**
 * Get the pager block
 * If the block isn't set in the layout XML, it will be created and will use the default template
 *
 * @return Fishpig_Wordpress_Post_List_Pager
 */
public function getPagerBlock()
{
    if (!$this->hasPagerBlock()) {
        $this->setPagerBlock(false);
        
        if (!$this->getChild('pager')) {
            $this->setChild('pager', $this->getLayout()
                ->createBlock('wordpress/post_list_pager')
                    ->setNameInLayout('wordpress_post_list')
                    ->setAlias('pager')
            );
        }

        if ($pager = $this->getChild('pager')) {
            $this->setPagerBlock(
                $pager->setPostListBlock($this)
            );
        }
    }
    
    return $this->_getData('pager_block');
}

In its respective phtml file, it has:

app\design\frontend\rau\default\template\wordpress\post\list.phtml

<?php $posts = $this->getPosts() ?>

If i do count($posts); in this list.phtml i get the count value.

But the same Block im trying to access in another phtml file like:

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

UPDATE:

$searchcountobj = new Fishpig_Wordpress_Block_Post_List();   print_r($searchcountobj);
$searchcount = $searchcountobj->getPosts();  echo 'COUNT:'.count($searchcount); 

Output of get_class_methods($searchcountobj)

Array ( [0] => getPosts [1] => _getPostCollection [2] => setWrapperBlock [3] => getPagerBlock [4] => getPagerHtml [5] => getPostRenderer [6] => getPostRendererTemplate [7] => _prepareLayout [8] => _beforeToHtml [9] => getPost [10] => getPage [11] => getPostId [12] => canComment [13] => canDisplayPreviousNextLinks [14] => getPasswordProtectHtml [15] => displayExcerptInFeed [16] => preparePost [17] => _getBlockForPostPrepare [18] => getAfterPostContentHtml [19] => getBeforePostContentHtml [20] => _getChildTextList [21] => getMetaBlock [22] => escapeHtml [23] => _construct [24] => getTemplate [25] => setTemplate [26] => getTemplateFile [27] => getArea [28] => assign [29] => setScriptPath [30] => getDirectOutput [31] => getShowTemplateHints [32] => fetchView [33] => renderView [34] => _toHtml [35] => getBaseUrl [36] => getJsUrl [37] => getObjectData [38] => getCacheKeyInfo [39] => _getAllowSymlinks [40] => __construct [41] => _getFactory [42] => _getApp [43] => getRequest [44] => getParentBlock [45] => setParentBlock [46] => getAction [47] => setLayout [48] => getLayout [49] => getIsAnonymous [50] => setIsAnonymous [51] => getAnonSuffix [52] => setAnonSuffix [53] => getBlockAlias [54] => setBlockAlias [55] => setNameInLayout [56] => getSortedChildren [57] => setAttribute [58] => setChild [59] => unsetChild [60] => unsetCallChild [61] => unsetChildren [62] => getChild [63] => getChildHtml [64] => getChildChildHtml [65] => getSortedChildBlocks [66] => _getChildHtml [67] => _beforeChildToHtml [68] => getBlockHtml [69] => insert [70] => sortChildren [71] => append [72] => addToChildGroup [73] => addToParentGroup [74] => getChildGroup [75] => getChildData [76] => setFrameTags [77] => toHtml [78] => _afterToHtml [79] => _getUrlModelClass [80] => _getUrlModel [81] => getUrl [82] => getUrlBase64 [83] => getUrlEncoded [84] => getSkinUrl [85] => getMessagesBlock [86] => setMessagesBlock [87] => getHelper [88] => helper [89] => formatDate [90] => formatTime [91] => getModuleName [92] => __ [93] => htmlEscape [94] => stripTags [95] => urlEscape [96] => escapeUrl [97] => quoteEscape [98] => jsQuoteEscape [99] => getNameInLayout [100] => countChildren [101] => _beforeCacheUrl [102] => _afterCacheUrl [103] => getCacheKey [104] => getCacheTags [105] => addCacheTag [106] => addModelTags [107] => getCacheLifetime [108] => _loadCache [109] => _saveCache [110] => _getTagsCacheKey [111] => _getSidPlaceholder [112] => getItemsTags [113] => _isSecure [114] => _addFullNames [115] => _initOldFieldsMap [116] => _prepareSyncFieldsMap [117] => isDeleted [118] => hasDataChanges [119] => setIdFieldName [120] => getIdFieldName [121] => getId [122] => setId [123] => addData [124] => setData [125] => unsetData [126] => unsetOldData [127] => getData [128] => _getData [129] => setDataUsingMethod [130] => getDataUsingMethod [131] => getDataSetDefault [132] => hasData [133] => __toArray [134] => toArray [135] => _prepareArray [136] => __toXml [137] => toXml [138] => __toJson [139] => toJson [140] => toString [141] => __call [142] => __get [143] => __set [144] => isEmpty [145] => _underscore [146] => _camelize [147] => serialize [148] => getOrigData [149] => setOrigData [150] => dataHasChangedFor [151] => setDataChanges [152] => debug [153] => offsetSet [154] => offsetExists [155] => offsetUnset [156] => offsetGet [157] => isDirty [158] => flagDirty )

COUNT:0

How am i supposed to access the block function in magento?

Am i accessing in the right way?

11
  • add your config.xml file code here Commented Jun 18, 2018 at 10:26
  • Have updated the question with the modules config xml Commented Jun 18, 2018 at 10:30
  • Can it be done anyway using my module's layout xml? where the name of the reference block is wordpress_post_list and i tried by getChildHtml()... i still get empty Commented Jun 18, 2018 at 10:55
  • paste the content of your wordpress.xml
    – Vishnunath
    Commented Jun 18, 2018 at 11:59
  • Updated the wordpress.xml Commented Jun 18, 2018 at 12:02

4 Answers 4

0

use below code in your phtml file

$this->getLayout()->getBlockSingleton('wordpress/post_list')->getPosts()
6
  • Yes i had tried that too in my custom.phtml, count says 0 Commented Jun 18, 2018 at 11:11
  • Any other alternate that yu can think of suggesting for this? @Prashant Commented Jun 18, 2018 at 11:52
  • you can directly call post collection in template this one you should try Commented Jun 18, 2018 at 12:31
  • u mean to say $searchcountobj->_getPostCollection()? I tried even that..i get 0 as count Commented Jun 18, 2018 at 13:14
  • Mage::getModel using this Commented Jun 18, 2018 at 13:36
0

You need to create object in common.phtml :

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

 $obj = new Fishpig_Wordpress_Block_Post_List();

 $searchcount = $obj->getPosts();

 echo count($searchcount);

Try this.

9
  • Have updated my question using object of List class @Indranil, i still get 0 as count...! Commented Jun 18, 2018 at 10:26
  • Can you please mention the class name ? which you have seen after echo get_class($this); in list.phtml Commented Jun 18, 2018 at 10:33
  • On echo($this) in list.phtml i get Fishpig_Wordpress_Block_Post_List as class name and also updated the output for $searchcountobj, i dont think i am getting the getPosts() in ouput..whats wrong in the code? Commented Jun 18, 2018 at 10:40
  • Anyway u can correct me @IndranilMaity? Commented Jun 18, 2018 at 10:58
  • $searchcountobj = new Fishpig_Wordpress_Block_Post_List(); After this code. Try this print_r(get_class_methods($searchcountobj)); and paste your response here. Please. Commented Jun 18, 2018 at 11:02
0

This is what your _postCollection() method.

protected function _getPostCollection()
{
$artical_type = Mage::app()->getRequest()->getParam('article_type');
    if (is_null($this->_postCollection) && $this->getWrapperBlock()) {
        $this->_postCollection = $this->getWrapperBlock()->getPostCollection();

        if ($this->_postCollection) {
            if ($this->getPostType()) {
                $this->_postCollection->addPostTypeFilter($this->getPostType());
                if(!empty($artical_type)) {
                    $posttypevalue = Mage::helper('wordpress')->getPostMetaData();
                    if(!empty($posttypevalue)) {
                        $this->_postCollection = $this->_postCollection->addFieldToFilter('ID',array('in' => $posttypevalue));
                    }
                }
            }

            if ($this->getPagerBlock()) {
                $this->getPagerBlock()->setCollection($this->_postCollection);
            }
        }
    }

    return $this->_postCollection;
}

As you can see here, this collections depends on two things.

  1. Wrapper block
  2. The parameter article_type.

Here the villain in your case would be most probably the wrapper block. So let's focus on that first

if (is_null($this->_postCollection) && $this->getWrapperBlock()) {
  //loading collection from the wrapper block
}

From the above condition check, it is obvious that it looks for a wrapper block in order to load the collection. When you call this block directly, this wrapper block instance may be missing or if it does really exist, then something makes the $this->getWrapperBlock()->getPostCollection() null again.

To debug this, now you need to find what is that wrapper block class and find it's collection method and debug there.

This is just an idea of how to find the real cause. I think this will help you to find the real culprit in your case. Good luck

4
  • Thanks for the detailed explanation @Rajeev, i tried removing the getWrapperBlock() condition, seems the wrapper block is present even in the core List Block class, but not sure, even though object of that class is created, count becomes 0 Commented Jun 19, 2018 at 6:35
  • Have updated the question with setwrapperblock code Commented Jun 19, 2018 at 7:13
  • @user7115281 Above I have given you an idea of how to find the real cause. Debug part you should do from your side. The purpose of the site is not give a "copy-paste and then bingo it works!!" answer, rather give you an idea on what happens in the background so that with the given info you can resolve the issue. Otherwise no one going to benefit from this forum. Commented Jun 19, 2018 at 9:04
  • Yes i understand @Rajeev, i appreciate your suggestion. im debugging from my end..thanks Commented Jun 19, 2018 at 9:22
0

Try the below code:

<?php echo $this->getLayout()->createBlock('wordpress/post_list')
    ->setTemplate('wordpress/common.phtml')->toHtml(); ?>

Now you can call the functions of your block class in common.phtml

7
  • where should i add this code? in common or list phtml? Could you please provide a detail answer Commented Jun 18, 2018 at 15:21
  • Where did you call common.phtml? Commented Jun 18, 2018 at 15:32
  • as already mentioned, i just copied the common html from base folder to my theme folder and doing changes there Commented Jun 18, 2018 at 15:34
  • But there is not such file on base by default. Can you check the layout file if any common.phtml is called or not? Commented Jun 18, 2018 at 15:38
  • On which page you are showing common.phtml? Commented Jun 18, 2018 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.