我已成功完成了与magento2的fishpig集成,但我无法显示2个博客文章Magento2主页我创建了我的custom.phtml文件并将其调用到主页但只有单个帖子即将到来
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance() ?>
<?php $posts = $objectManager->create('FishPig\WordPress\Model\ResourceModel\Post\Collection')
->addPostTypeFilter('post')
->setOrderByPostDate()
->addIsViewableFilter()
->setPageSize(5)
->load(2); ?>
<?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; ?>
我想展示两个最新的帖子
答案 0 :(得分:1)
代码看起来很好,但是你不需要通过&#39; 2&#39;加载方法。
->load(2); // You don't need the 2
->load(); // This is fine
如果此代码返回1个帖子,可能是因为您的博客中只有1个帖子?
以下代码将显示您发布的帖子数量。
<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance() ?>
<?php $posts = $objectManager->create('FishPig\WordPress\Model\ResourceModel\Post\Collection')
->addPostTypeFilter('post')
->setOrderByPostDate()
->addIsViewableFilter()
->load(); ?>
<?php echo count(posts) ?> posts