问题
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>
回答1:
Try this:
I have updated your query and added another query to get post image url :
<div class="block block-blog block-recent-posts">
<?php $resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$query = "SELECT p.id,p.post_title,p.post_name ,p.post_content,p.comment_count,pm.meta_value FROM wp_postmeta AS pm INNER JOIN wp_posts AS p ON pm.post_id=p.ID WHERE pm.meta_key = '_thumbnail_id' AND p.post_type='post' ORDER BY p.post_date DESC LIMIT 10";
$results = $readConnection->fetchAll($query);
?>
<ul>
<?php
$counter = 0;
foreach($results as $row) { ?>
<?php if($row['post_title']!='Auto Draft'):
//Get url from pm.meta_value
/********/
$readConnection1 = $resource->getConnection('core_read');
$query1 ="SELECT * FROM `wp_postmeta` WHERE `post_id` = '".$row['meta_value']."' AND meta_key='_wp_attached_file'";
$results1 = $readConnection->fetchAll($query1);
$url='www.yoursite.com/wp-content/uploads/'.($results1[0]['meta_value']);
echo $url; //YOUR URL just set your site name
//So final url YOUR--> SITENAME/wp-content/uploads/pathfromquery
?>
<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>
回答2:
You shouldn't really be using raw SQL here. As a work around, couldn't you do either of the following:
1) Select more than 2 posts (eg. 5) and loop through them and display the 2 that have featured images
2) Ensure all posts have featured images and then select the first 2 posts
3) Use a collection of posts and then add your custom SQL to that.
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; ?>
来源:https://stackoverflow.com/questions/36793336/return-wordpress-post-featured-images-using-fishpig-in-magento