0
votes

I have a successful Fishpig integration to magento 2.

Now I need to get the blog page to display most recent post on each category.

Ex: If I have 5 categories (without showing uncategorized category), I need to display most recent post from each category.

How can I do this with fishpig?

I tried modifying list.phtml file, from FishPig/WordPress/view/frontend/templates/post. So far I can only get the most recent posts from any category (if I have most recent posts in one category, I get them. not most recent post from each category).

This is the code I've got so far..

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance() ?>
<?php $posts = $objectManager->create('FishPig\WordPress\Model\ResourceModel\Post\Collection')
->addPostTypeFilter('post')
->setOrderByPostDate()
->addIsViewableFilter()
->setPageSize(5)
->load();    
?>

<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<a href="<?php echo $post->getUrl() ?>"><?php echo $this->escapeHtml($post->getPostTitle()) ?></a>
<?php if ($image = $post->getFeaturedImage()): ?>
<a href="<?php echo $post->getUrl() ?>">
<img src="<?php echo $image->getAvailableImage() ?>" src="<?php echo $this->escapeHtml($post->getPostTitle()) ?>" />
</a>
<?php endif; ?>
<p><?php echo $post->getPostExcerpt(40) ?></p></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

How should I modify this to achieve what I need? Please help

1
anyone? please help me?Joey

1 Answers

1
votes

What is it that you want? The module, by default, will show you each category page and list all posts in that category in descending date order. Are you trying to replicate this?

If so, take a look at the Term model. You can load a collection of categories and then call getPostCollection() on it to get posts from that Term/category.

<?php $terms = $objectManager->get('FishPig\WordPress\Model\ResourceModel\Term\CollectionFactory')->create() ?>
<?php $terms->addTaxonomyFilter('category')->load() ?>
<?php   if (count($terms) > 0): ?>
    <ul>
        <?php foreach($terms as $term): ?>
            <?php if ((int)$term->getId() === 1): ?><?php /* This is the uncategorized category, so skip */ continue; ?><?php endif; ?>
            <?php $posts = $term->getPostCollection()->setPageSize(5)->setOrderByPostDate()->addIsViewableFilter()->load() ?>
            <?php if (count($posts) > 0): ?>
                <li>
                    <h2><a href="<?php echo $term->getUrl() ?>"><?php echo $term->getName() ?></a></h2>
                    <ul>
                        <?php foreach($posts as $post): ?>
                            <li>
                                <?php /* Standard post code here */
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>