在Magento中使用Fishpig返回wordpress发布特色图片 [英] Return wordpress post featured images using Fishpig in Magento
问题描述
我正在尝试从Wordpress中获得两篇包含特色图片的最新帖子.需要获取帖子标题,内容(字符有限)和精选图片.到目前为止,我所掌握的只是特色图片.
Im trying to get two of the latest posts from Wordpress that include featured images. Need to get the post title, content (character limited) and the featured image. I have this so far, all that is missing is the featured image.
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT `id`, `post_title`,`post_name` ,`post_content`, `comment_count` FROM `wp_posts` WHERE `post_type`='post' ORDER BY `comment_count` DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'): ?>
<li class="item">
<a href="<?php echo $this->getUrl('news/').$row['post_name'];?>"> <?php echo $row['post_title'];?></a>
<p class="blog-content"> <?php $content = $row['post_content']; echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";} ?></a></p>
</li>
<?php endif; ?>
<?php
if($counter == 2)
{
break;
}
$counter++;
}
?>
</ul>
推荐答案
您实际上不应该在这里使用原始SQL.解决方法是,您不能执行以下任一操作:
You shouldn't really be using raw SQL here. As a work around, couldn't you do either of the following:
1)选择2个以上的帖子(例如5个)并循环浏览,并显示2个带有特色图片的
1) Select more than 2 posts (eg. 5) and loop through them and display the 2 that have featured images
2)确保所有帖子都有特色图片,然后选择前2个帖子
2) Ensure all posts have featured images and then select the first 2 posts
3)使用帖子集合,然后向其中添加自定义SQL.
3) Use a collection of posts and then add your custom SQL to that.
此代码将获得2个帖子,并将_thumbnail_id添加到集合中.您可以向其中添加一些代码以检查此字段是否存在(使用$ posts-> load(true)调试SQL查询,并使用$ posts-> getSelect()-> where()添加自定义过滤器)
This code will get 2 posts and add the _thumbnail_id to the collection. You can add some code to this to check this field exists (use $posts->load(true) to debug the SQL query and $posts->getSelect()->where() to add the custom filter)
<?php $posts = Mage::getResourceModel('wordpress/post_collection') ?>
<?php $posts->addPostTypeFilter('post') ?>
<?php $posts->addIsViewableFilter() ?>
<?php // Limit the collection to 2 posts. Change this number or remove this line completely to include all posts
<?php $posts->setPageSize(2) ?>
<?php // Adds the _thumbnail_id meta field to the collection ?>
<?php // This can be used for checking in the SQL whether a post has a featured image ?>
<?php $posts->addMetaFieldToSelect('_thumbnail_id') ?>
<?php $posts->load() ?>
<?php if (count($posts) > 0): ?>
<ul>
<?php foreach($posts as $post): ?>
<li>
<h2><a href="<?php echo $post->getPermalink() ?>"><?php echo $post->getPostTitle() ?></a></h2>
<?php if ($image = $post->getFeaturedImage()): ?>
<a href="<?php echo $post->getPermalink() ?>" class="img"><img src="<?php echo $image->getAvailableImage() ?>" alt="" /></a>
<?php endif; ?>
<div class="post-content"><?php echo $post->getPostContent() ?></div>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
这篇关于在Magento中使用Fishpig返回wordpress发布特色图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!