bsod99
bsod99

Reputation: 1297

Fishpig Wordpress - get post categories within post list

with the Fishpig wordpress integration with magento, I don't seem to be able to access a post's categories using the method provided by the docs. In post/list.phtml I'm using the below to try and extract the current post's categories to add to the list item class, but dumping the object shows that the array of category terms is empty. I've also tried this from view.phtml when viewing a single post and get the same result. Can anyone provide any pointers on what I'm doing wrong here? Thanks

<?php foreach ($posts as $post):
<?php $categories = $post->getTermCollection('category') ?>
            <li class="<?php echo $categories ?>

Upvotes: 0

Views: 2203

Answers (1)

Ben Tideswell
Ben Tideswell

Reputation: 1697

If you look at the code a little closer, you will see that the method you are calling is called getTermCollection. This tells you that the method returns a collection of terms (ie. categories). You cannot simply echo a collection to the screen. Instead you need to loop through the collection.

<?php $categories = $post->getTermCollection('category') ?>
<?php if (count($categories) > 0): ?>
    <?php foreach($categories as $category): ?>
        <a href="<?php echo $category->getUrl() ?>"><?php echo $this->escapeHtml($category->getName()) ?></a>
    <?php endforeach; ?>
<?php endif; ?>

This loops through the categories and prints a link to each category to the screen.

Upvotes: 3

Related Questions