簡體   English   中英

Fishpig獲取標題,發布內容,發布縮略圖和固定鏈接

[英]Fishpig get title, post content, post thumbnail and permalink

我想讓我的代碼使用Fishpig擴展名從Magento的wordpress中返回最后3個帖子。 這是我到目前為止的代碼,但似乎不止一次返回帖子。 我還需要它僅返回帖子,因為它現在還返回頁面。

<?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  ORDER BY p.post_date";
   $results = $readConnection->fetchAll($query);


   ?>
<?php 
   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='/news/wp-content/uploads/'.($results1[0]['meta_value']);
   ?>
<div class="blog-post-image">
   <img src="<?php echo $url; ?>">
</div>
<div class="blog-post-content">
   <?php  ?>
   <h3> <a href="<?php echo $this->getUrl('news/').$row['post_name'];?>"> <?php echo $row['post_title'];?></a></h3>
   <p class="blog-content">  <?php $content = $row['post_content'];  echo $string = substr($content,0,220); if(strlen($content)>220){echo "...";}      ?></a></p>
   <a href="" class="blog-posts-cta">More Info</a>
</div>
<?php endif; ?>
<?php
   if($counter == 4)
   {
        break;
   }
   $counter++;   
   }

   ?>

要顯示3條帖子並顯示帖子標題,URL,圖像和帖子內容(或摘錄),您將需要以下代碼:

<?php $posts = Mage::getResourceModel('wordpress/post_collection')
    ->addIsViewableFilter()
    ->addPostTypeFilter('post')
    ->setPageSize(3)
    ->load() ?>
<?php if (count($posts) > 0): ?>
    <ul>
        <?php foreach($posts as $post): ?>
            <li>
                <h2>
                    <a href="<?php echo $post->getPermalink() ?>"><?php echo $this->escapeHtml($post->getPostTitle()) ?></a>
                </h2>
                <?php if ($image = $post->getFeaturedImage()): ?>
                    <a href="<?php echo $post->getPermalink() ?>" class="fimg"><img src="<?php echo $image->getAvailableImage() ?>" alt="" /></a>
                <?php endif; ?>
                <?php echo $post->getPostContent() ?>
                <?php
                /**
                 * You could also use:
                 * echo $post->getPostExcerpt(20)
                 * This would display the first 20 words of the post content
                 * or the manually entered excerpt
                 **/
                ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

這是手工快速寫的,尚未經過測試,但應該可以正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM