How to Display Attribute On Page


I made an attribute named News Topic, and underneath it, inside of categories, I have an item named Company News.

I am trying to gain access to this News Topic attribute through the php, so that I can display it as a tag on my site. An example of what I would want it to look like:

Just replacing the “Mobility & Safety” part with Company News, which is inside of the News Topic attribute.

Any help on the PHP snippet needed to gain access to the Company News part inside of the News Topic attribute would be much appreciated.

Concrete Version: “9.2.1”

PHP Version: “8.2.0”

Pull you attribute into the page, this will be an array

$newscategories = $c->getAttribute('newstopic');

The you want to check to see if there is a value and if so you want to cycle through the array and print out each value

<?php   if (isset($newscategories)) {
             foreach ($newscategories as $newscategory) { ?>
                 <div class="pill pill-primary"><?= $newscategory ?></div>
           <?php } 
} ?>

Try something like that

Thanks for your reply. Unfortunately, it seems I am having trouble retrieving the news topic attribute, as when I try a var_dump of the variable after declaring it: $newscategories = $c->getAttribute(‘News Topic’); it returns NULL on the page. This screenshot shows the way I have the News Topic area set up:

Would you happen to know how I could properly retrieve the News Topic attribute that is located in this location?

Are you using any other page attributes?

It looks like you have the wrong name for the attribute. It normally wouldn’t have a space in it. It need to be the handle or the attribute

Ooh okay I see, I found the handle and I am using that now and it no longer returns NULL for the $newscategories variable. However, when I try to run the code you sent earlier I get this error: " 1. “Object of class Concrete\Core\Tree\Node\Type\Topic could not be converted to string”

edit: was able to figure it out now I believe, thank you for your help!

My bad that code is for an option list


<?php   if (isset($newscategories)) {
             foreach ($newscategories as $newscategory) { ?>
                 <div class="pill pill-primary"><?php echo $newscategory->treeNodeName ?></div>
           <?php } 
} ?>

That worked, thank you so much!!