Update variable regarding number of same blocks in page
Permalink
Hi,
I'm trying to update a number (variable : XX) on a page.
i need to update this : XX feedback(s)
The number of Feedbacks Should be the amount of the same blocks Type in the page.
I just have no idea how to do this or if it is possible.
If any idea your welcome
Chris
I'm trying to update a number (variable : XX) on a page.
i need to update this : XX feedback(s)
The number of Feedbacks Should be the amount of the same blocks Type in the page.
I just have no idea how to do this or if it is possible.
If any idea your welcome
Chris
Thanks for your solution.
Nice snippet but i need to be able to dynamic change value in Content Block.
I guess its something not possible...
See ya
Nice snippet but i need to be able to dynamic change value in Content Block.
I guess its something not possible...
See ya
@chrismodlao
This could be done using a Content block custom template:
When you type "{{feedback}}" into a Content block with the custom template, it will be replaced with the $featureBlockCount value.
This could be done using a Content block custom template:
<?php defined('C5_EXECUTE') or die('Access Denied.'); $c = Page::getCurrentPage(); if (!$content && is_object($c) && $c->isEditMode()) { ?> <div class="ccm-edit-mode-disabled-item"><?=t('Empty Content Block.')?></div> <?php } else { $pageBlocks = $c->getBlocks(); $featureBlockCount = 0; if ($pageBlocks) { foreach ($pageBlocks as $block) { $blockTypeHandle = $block->getBlockTypeHandle(); if ($blockTypeHandle == 'feature') { ++$featureBlockCount; } }
Viewing 15 lines of 18 lines. View entire code block.
When you type "{{feedback}}" into a Content block with the custom template, it will be replaced with the $featureBlockCount value.
You save my day with this code !!
But unfortunately it brakes the link to the images in the Content block.
It looks like you forgot the PHP end code like at the end of your exemple :
But with this one or without, It returns this :
And do you think there is any possibilities to check number of blocks in a apge but display the amount in another content block's page ??
But unfortunately it brakes the link to the images in the Content block.
It looks like you forgot the PHP end code like at the end of your exemple :
?>
But with this one or without, It returns this :
<img src="/files/">
And do you think there is any possibilities to check number of blocks in a apge but display the amount in another content block's page ??
@chrismodlao
The closing PHP tag is not required with this code snippet.
I tested this code in 5.7 and in version 8 and images displayed correctly in Content blocks.
"And do you think there is any possibilities to check number of blocks in a apge but display the amount in another content block's page ??"
Instead of using the current page, you can get a specific page by ID.
The closing PHP tag is not required with this code snippet.
I tested this code in 5.7 and in version 8 and images displayed correctly in Content blocks.
"And do you think there is any possibilities to check number of blocks in a apge but display the amount in another content block's page ??"
Instead of using the current page, you can get a specific page by ID.
<?php defined('C5_EXECUTE') or die('Access Denied.'); $c = Page::getCurrentPage(); if (!$content && is_object($c) && $c->isEditMode()) { ?> <div class="ccm-edit-mode-disabled-item"><?=t('Empty Content Block.')?></div> <?php } else { $pageObject = Page::getByID(1); $pageBlocks = $pageObject->getBlocks(); $featureBlockCount = 0; if ($pageBlocks) { foreach ($pageBlocks as $block) { $blockTypeHandle = $block->getBlockTypeHandle(); if ($blockTypeHandle == 'feature') { ++$featureBlockCount; }
Viewing 15 lines of 19 lines. View entire code block.
Hi Thanks again for this.
Thats exacly this.
But unfortunately i still have the problem with images.
I will dig it out...
Thanks
Chris
Thats exacly this.
But unfortunately i still have the problem with images.
I will dig it out...
Thanks
Chris
You can try this:
- this example gets the page block count for the Feature block