LinkArray include category permalink
2 posts by 2 authors in: Forums > CMS Builder
Last Post: April 29, 2014 (RSS)
Hi, I'm having issues pulling in the _link field within a linkarray when listing categories assigned to each blog post.
My code currently looks like this:
<?php $linkArray = array_combine($record['category:values'], $record['category:labels']); ?>
<?php foreach($linkArray as $num => $title): ?>
<a href="/category.php?<?php echo $num;?>"><?php echo $title; ?></a>,
<?php endforeach; ?>
I'd prefer to pull the _link field from each category record as I'm using permalinks and don't want to have duplicate URL's. Manually adding each category to the permalinks db isn't feasible as the client will be constantly adding new categories.
I've always used linkArray to generate a list of assigned categories on the detail page for each blog post, but haven't tried it when using permalinks so it's proving a bit of a conundrum for my designer brain.
Any help you can offer would be greatly appreciated.
Cheers,
Jeremy.
By Chris - April 29, 2014
Hi Jeremy,
Generating the _link field will require another call to getRecords(), whether you're using the standard _link (which is generated from other fields) or a permalink _link. A simple solution would be to replace your code with this:
<?php
$where = 'num IN (' . mysql_escapeCSV($record['category:values']) . ')';
list($linkRecords) = getRecords(array(
'tableName' => 'category', // TODO: set this to the tableName for your link records
'where' => $where,
'allowSearch' => false,
));
foreach ($linkRecords as $linkRecord): ?>
<a href="<?php echo $linkRecord['_link']; ?>"><?php echo $linkRecord['title']; ?></a>,
<?php endforeach ?>
Does that work?
However, if your code is already inside of a foreach, this is going to mean an extra getRecords() call per $record you're displaying, which may slow down your page. Let me know if this is a problem and I can show you how to load all the child records all at once (caching them at the top of your page).
Chris