Home » Php » php – wordpress fishpig magento installation – getPostListHtml()

php – wordpress fishpig magento installation – getPostListHtml()

Posted by: admin July 12, 2020 Leave a comment

Questions:

I have a wordpress installation on my magento store utilizing the 3rd party module fishpig.

I have a page called list.phtml which posts all the content from a category using $this->getPostListHtml() I added another div and replicated it but now it’s just duplicating the posts from the first div into the second. How do i make a unique second div so when posts are made on the backend it only posts to the second div on the same page. Currently i’m getting category by slug and post collection and then setting order (asc) and then i’m looping through category and riders for each page that is pulling similar content. I have several pages but on each rider page it will still have to have Team 2 posts below Team. I hope that makes sense.

cpt/team/list.phtml

<?php
/**
 * Team (Rider) - Landing Page
 */
/* @var $this test_Wordpress_Block_Cpt_Type_View */

/* @var $helper test_Wordpress_Helper_Data */
$helper = $this->helper('wordpress');
/* @var $postType Fishpig_Wordpress_Addon_CPT_Model_Type */
$postType = $this->getPostType();
$categorySlug = $this->getCurrentCategorySlug();
$allCategories = $helper->getAllCategories();

$instagramHandles = array(
    'skate' => 'testskate',
    'surf' => 'testsurf',
    'snow' => 'testsnow',
    'mx' => 'testmx',
    'testunity' => 'testunity',
    'girls' => 'testgirls',
);

$textRider = ($categorySlug == 'testunity') ? 'Ambassador' : 'Rider';
$htmlRiderPicks = $this->getChildHtml('picks');
$hasEvents = ($events = $this->getChild('events') && $this->getChild('events')->getTotal());
$_theseRiders = array();
?>
<?php if($categorySlug): ?>
    <?php $curCategory = $helper->getCategoryBySlug($categorySlug) ?>
    <?php $riders = $curCategory->getPostCollection()->addPostTypeFilter($postType->getPostType())->setOrder('menu_order','asc'); ?>
    <div class="team-header row">
        <div class="columns">
            <h1 class="team-page-title"><?php echo $this->__('Team'); ?></h1>

            <ul class="team-categories j-team-category-list">
                <?php foreach($helper->getTeamCategories() as $category): /* @var $category Fishpig_Wordpress_Model_Post_Category */ ?>
                    <li  class="j-team-title <?php if($category->getSlug() == $categorySlug): ?>active<?php endif; ?>">
                        <a href="<?php echo $this->getCategoryUrl($category) ?>"><?php echo $helper->escapeHtml($category->getName()) ?></a>
                        <?php $riders = $category->getPostCollection()->addPostTypeFilter($postType->getPostType())->setOrder('menu_order','asc'); ?>
                            <div class="riders-names-list j-riders-names-list only-mobile-show">
                                <ul>
                                    <?php foreach($riders as $rider): /* @var $rider Fishpig_Wordpress_Model_Post */ ?>
                                        <li>
                                            <a href="<?php echo $rider->getPermalink() ?>"><?php echo $helper->escapeHtml($rider->getPostTitle()) ?></a>
                                        </li>

                                    <?php endforeach; ?>
                                </ul>
                            </div>

                    </li>
                    <?php
                        if ($category->getSlug() == $categorySlug) {
                            $_theseRiders = $riders;
                        }
                    ?>
                <?php endforeach; ?>
            </ul>
            <div class="riders-names-list mobile-hide">
                <ul>
                    <?php foreach($_theseRiders as $rider): /* @var $rider Fishpig_Wordpress_Model_Post */ ?>
                        <li>
                            <a href="<?php echo $rider->getPermalink() ?>"><?php echo $helper->escapeHtml($rider->getPostTitle()) ?></a>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        </div>
    </div>

    <div id="team-panel" class="team-panel">
        <div class="riders-panel" id="Rider">
            <div class="riders-list team-riders-list">
                <div class="row">
                    <?php echo $this->getPostListHtml() ?>
                </div>
            </div>
        </div>
    </div>

    <br><br>
    <div class="team-header row">

    <div class="columns">
    <h1 class="team-page-title"><?php echo $this->__('Second Team'); ?></h1>

    <div id="second-team-panel" class="second-team-panel">
        <div class="second-riders-panel" id="Second_Rider">
            <div class="riders-list team-riders-list-second">
                <div class="row">
                    <?php echo $this->getPostListHtml() ?>
                </div>
            </div>
        </div>
    </div>
</div></div>  

/**
     * Generates and returns the collection of posts
     *
     * @return Fishpig_Wordpress_Model_Mysql4_Post_Collection
     */
    protected function _getPostCollection()
    {
        return Mage::getResourceModel('wordpress/post_collection');
    }

    /**
     * Returns the HTML for the post collection
     *
     * @return string
     */
    public function getPostListHtml()
    {
        return $this->getPostListBlock()->toHtml();
    }

    /**
     * Gets the post list block
     *
     * @return Fishpig_Wordpress_Block_Post_List
     */
    public function getPostListBlock()
    {
        if (is_null($this->_postListBlock)) {
            if (($block = $this->getChild($this->_postListBlockName)) === false) {
                $block = $this->getLayout()
                    ->createBlock('wordpress/post_list', $this->_postListBlockName . rand(1111, 9999))
                    ->setTemplate($this->getPostListTemplate());
            }

            $block->setWrapperBlock($this);

            $this->_postListBlock = $block;
        }

        return $this->_postListBlock;
    }

cpt/team/renderer/list.phtml

<?php
/**
 * Team (Riders) - List View Renderer
 */
/* @var $this Fishpig_Wordpress_Block_Post_List_Renderer */

/* @var $helper test_Wordpress_Helper_Data */
$helper = $this->helper('wordpress');
/* @var $post Fishpig_Wordpress_Model_Post */
$post = $this->getPost();
$featuredImage = $post->getFeaturedImage();
/** @var test_Wordpress_Helper_Image $teamHelper */
$imageHelper = $this->helper('test_wordpress/image');
$background = $imageHelper->getBackground($post);
?>
<div class="m-grid-6 columns">
    <div class="rider-list-item fade-item">
        <a class="rider-info" href="<?php echo $post->getPermalink() ?>" title="<?php echo $this->escapeHtml($post->getPostTitle()) ?>">
             <span class="post-img">
            <img class="rider-background fade-image" src="<?php echo $background; ?>" alt="test Riders" />
            <?php if ($featuredImage): ?>
                <img class="rider-photo " src="<?php echo $featuredImage->getImageByType('thumb-240x220') ?>" alt="<?php echo $this->escapeHtml($post->getPostTitle()) ?>">
            <?php endif; ?>
                 </span>
            <span class="rider-list-name"><?php echo $this->escapeHtml($post->getPostTitle()) ?></span>
        </a>
    </div>
</div>

cpt/team/renderer/view.phtml

How to&Answers:

Based on your partially posted code, I can give you only a conceptual answer.

From what you’ve posted by now your only difference between new and old posts could be synthesised in two ways:

  1. By post_date, and you can call almost the same function, but with the last displayed post’s post_date as parameter so you can filter the new results that has newer post_date. Adding a new closure before or after setOrder(...) or by changing the wpQuery;

  2. Or by offset. Considering that you won’t have backdated posts, all new posts will have the offset equal with the initial posts count. so you’ll have to setOffset($oldCount); This way you’ll get latest posts starting from the last displayed post.

You’ll just have to update the wp query with the desired parameter, or find a way to tag your posts in some other way:

  1. Eaiser way would be to keep track of your displayed post IDs in an array $displayed_post_ids, and than query with post__not_in, like the following example:


$args = array(
'post_type' => 'post',
//... other args
'post__not_in' => array($displayed_post_ids) // Don't show this posts
);

  1. Another alternative might be the post_meta fields. You’ll have more control this way and you can use same old categories for new posts also.
    You can add a post_meta information that won’t be displayed in page and that will keep track if your posts have been displayed in the first div or not. Considering is more easy to alter the posts that you already have, you can add a post_meta _wasDisplayed (using something like update_post_meta( $post_id, '_wasDisplayed', true); when you display the post in the first div can also do the trick )

You can set a default false value of that post_meta value for all your posts on creation. This will help you on the query.
On the second div you will call the generator function and check if your posts have the post_meta and if it’s false.

Also you can reset the post_meta values later if you need.

For implementation is also needed to be know how many posts you intend to display, if you use pagination and other small details that could make your work a bit more complex.

Notice – this case solution:

5. As you’re business logic seem to be related more to categories, there’s a more straightforward solution:
On the backed process that adds new posts assign an additional category to every new post (something called like: “New Undisplayed Posts”) this category will be used as a filter argument in the second div.
Take into account that you’ll have to remove this category from all the posts that are posted at the time you call the getPostListHtml() from the first div, otherwise you will have unwanted posts in the second div.

You can make this happen in a wrapper function using one of the category arguments for wp query object:

Show posts associated with certain categories.

Hope it helps,
Codrut, KMM