Hello.
On v1 Omeka website, I could display on the recent item part the last records related to a specific collection with this code :
<?php set_current_collection(get_collection_by_id(4));
I noticed that some functions have changed with v2 version on the Updating Plugins For 2.0 codex page but when I update the code :
<?php set_current_record(get_record_by_id('collection', 4)); ?>
I have this error message :
Catchable fatal error: Argument 2 passed to set_current_record() must be an instance of Omeka_Record_AbstractRecord, none given, called in /var/www/themes/mh_alger/index.php on line 45 and defined in /var/www/application/libraries/globals.php on line 1807
Can someone help me understand what is wrong, please ?
Thanks !
set_current_record
requires you to pass the type of record you're setting as the first argument:
set_current_record('collection', get_record_by_id('collection', 4));
Thank you ! I should have been more careful with the syntax in the codex.
I tried on the index.php of my theme
<div id="recent-items">
<h2><?php echo __('Notices ajoutées récemment'); ?>
</h2>
<?php set_current_record('collection', get_record_by_id('collection', 4));
echo recent_items($recentItems); ?>
<p class="view-items-link"><a>"><?php echo __('View All Items'); ?></a></p>
</div><!--end recent-items -->
but I still get the recent items of every collection, not only the collection with the id 4.
Hi Jorge,
saw this post looking for something else, any solution yet ?
Why don't you create your own recent_items() function with the collection id as an argument ?
In your function.php file, you could do something like :
function get_recent_items_by_collection($num = 10, $collection)
{
return get_db()->getTable('Item')->findBy(array('collection' => $collection, 'sort_field' => 'added', 'sort_dir' => 'd'), $num);
}
Now that you have a collection argument, you can use it in a recent_items_by_collection() function that you also create, adding the $collection argument to the recent_items() function :
function recent_items_by_collection($count = 10, $collection)
{
$items = get_recent_items_by_collection($count, $collection);
if ($items) {
$html = '';
foreach ($items as $item) {
$html .= get_view()->partial('items/single.php', array('item' => $item));
release_object($item);
}
} else {
$html = '<p>' . __('No recent items available.') . '</p>';
}
return $html;
}
And that's it. Now on your index.php, you just have to use this new function, with the id of the collection you want as a argument :
<h2><?php echo __('Notices ajoutées récemment'); ?>
</h2>
<?php echo recent_items_by_collection($recentItems, 4); ?>
<p class="view-items-link">"><?php echo __('View All Items'); ?></p>
</div><!--end recent-items -->
Maybe there's another way to do it, but I think it could work and it allows you to use it with any collection, you just have to change the id.
Thomas
Hi TomL,
Thanks a lot. It works nicely !