0

I am trying to change my breadcrumbs in magento 2 but getting error with blank page. for example I am trying to do like this attachment.

   <?php if ($crumbs && is_array($crumbs)) : ?>
    <div class="breadcrumbs">
        <ul class="items">
            <?php foreach ($crumbs as $crumbName => $crumbInfo) :?>
    <?php
            $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $title = $objectManager->get('Magento\Framework\View\Page\Title');
    print_r($title);

    ?>
                <li class="item <?php /* @escapeNotVerified */ echo $crumbName ?>">
                <?php if ($crumbInfo['link']) : ?>
                    <a href="<?php /* @escapeNotVerified */ echo $crumbInfo['link'] ?>" title="<?php echo $block->escapeHtml($crumbInfo['title']) ?>">
                        <?php echo $block->escapeHtml($crumbInfo['label']) ?>
                    </a>
                </li>
                <li>
                <?php elseif ($crumbInfo['last']) : ?>
                    <strong><?php echo $block->escapeHtml($crumbInfo['label']) ?></strong>
                <?php else: ?>
                    <?php echo $block->escapeHtml($crumbInfo['label']) ?>
                <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>

</div>

enter image description here

8
  • You can do same thing in controller or layout xml file magento.stackexchange.com/questions/106295/…
    – Jackson
    Commented Apr 27, 2017 at 9:25
  • have you got $title value? Commented Apr 27, 2017 at 9:26
  • Rakesh Jesadiya@I am not getting this $ $title value . Can you help me regarding this Commented Apr 27, 2017 at 9:30
  • You can get title by echo $title->getShortHeading(); Commented Apr 27, 2017 at 9:30
  • Ankit saha @this is only use for contact us page .I need all pages Commented Apr 27, 2017 at 9:31

2 Answers 2

1

You can easily do by adding below code above for loop in breadcrumbs.phtml file.

<?php $lastTitle = end($crumbs);?>
   <?php if(isset($lastTitle)):?>
        <span><strong><?php echo $lastTitle['label'];?></strong></span>
   <?php endif;?>

Full file code will be as follow:

<?php if ($crumbs && is_array($crumbs)) :  ?>
<div class="breadcrumbs">
    <?php $lastTitle = end($crumbs);?>
    <?php if(isset($lastTitle)):?>
        <span><strong><?php echo $lastTitle['label'];?></strong></span>
    <?php endif;?>
    <ul class="items">
        <?php foreach ($crumbs as $crumbName => $crumbInfo) : ?>
            <li class="item <?php /* @escapeNotVerified */ echo $crumbName ?>">
            <?php if ($crumbInfo['link']) : ?>
                <a href="<?php /* @escapeNotVerified */ echo $crumbInfo['link'] ?>" title="<?php echo $block->escapeHtml($crumbInfo['title']) ?>">
                    <?php echo $block->escapeHtml($crumbInfo['label']) ?>
                </a>
            <?php elseif ($crumbInfo['last']) : ?>
                <strong><?php echo $block->escapeHtml($crumbInfo['label']) ?></strong>
            <?php else: ?>
                <?php echo $block->escapeHtml($crumbInfo['label']) ?>
            <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
</div>
<?php endif; ?>
5
  • For category pages title will not get because meta title is not added from backend, so it is better to use Label to display. Commented Apr 27, 2017 at 13:04
  • Brijal@ thanks for this , how can i follow you , Impressive Commented Apr 28, 2017 at 5:40
  • Brijal@ let me know how can I add link with this , code is workig and I am getting same that I want. but link is not working . any help Commented Apr 28, 2017 at 7:42
  • @SanjayYadav which link you are asking about? In above code it only shows last link label as a title. And there is no need to show link to that label because we are on that page only. Commented May 4, 2017 at 6:48
  • yes but when i click on current page . then it's redirect me on home page Commented May 4, 2017 at 9:14
0

Your page breaks because of your code

 $objectManager->get('Magento\Framework\View\Page\Title');

it should be create not get. To get page title add below code in breadcrumb.php block file

public function getPageTitle()
{
    if (!empty($this->pageTitle)) {
        return $this->pageTitle;
    }
    return __($this->pageConfig->getTitle()->getShort());
}

Now call this function in phtml file

1
  • Brijal@ thanks for this is Another good solution . Thanks for this Commented Apr 28, 2017 at 5:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.