Get collection for item and display header

I would like to have a different header display if an item is in a specific collection (for both the collection and item pages). Below is the code I'm using. Just having the collection page specific info works fine, but when I add in to display a header if the item belongs to a collection, I can't view anything in Omeka if it's not in that collection and I get an error page. Any advice appreciated.

<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];

if( strpos( $url, 'collections/show/62') !== false ) {
echo '<div id="ferg-header">';
}
elseif( strpos( $url, 'collection=62') !== false ) {
echo '<div id="ferg-header">';
}
elseif (item_belongs_to_collection('Documenting Ferguson')){
echo '<div id="ferg-header">';
}
else {
echo '<div id="header">';
}?>

The item_belongs_to_collection function will try to look up the item to display, and throw an error if it is being called on a page that doesn't have an item to display.

Something like this should let you work around that:

if (! $item ) {
    $item = __v()->item;
}

if( strpos( $url, 'collections/show/1') !== false ) {
echo '<div id="ferg-header">';
}
elseif( strpos( $url, 'collection=1') !== false ) {
echo '<div id="ferg-header">';
}
elseif ($item && $item->Collection->id == 62){
echo '<div id="ferg-header">';
}
else {
echo '<div id="header">';
}

The if at the top will try to look up the item, but won't fail so badly if there isn't one. Then the replacement to item_belongs_to_collection does basically the same check.

Perfect - thank you!

Any idea why this would work in an earlier version of Omeka and not 2.2.2? I just tried to reuse this code and in 2.2.2, the item page won't render any code past this point, so the item/show page stops at <!-- end primary-nav -->

Many of the functions changed between Omeka 1.5 and Omeka 2.

For 2.0 and above, you could use the get_collection_for_item() function to get the collection.

Then, to test the collection's id, use metadata()

<?php
$collection = get_collection_for_item();
$collectionId = metadata($collection, 'id')

//branch however on $collectionId
?>

Thanks - that worked. I'm now having the same problem where no pages will display if they're not in that specific collection. This bit of code seems to break
if (! $item ) {
$item = __v()->item;
}
Is there an updated version for 2.2.2?

yeah, the __v() function to get the view is no longer in Omeka 2.

The get_collection_for_item() function does the same job, though -- it looks up $item if it is not set.

So, you can get rid of that code, and just check to see if $collection is not null. Something like

<?php

if ($collection) {

//do whatever for the collection

} else {
    echo '<div id="header">';
}

?>

should do the trick