Forum

Thread tagged as: Question

Categories call

Sorry - this is such a basic thing, I assume I'm missing someplace where it's spelled out, but I've looked and can't find.

I have articles that have two different types of category - one for the topic of the article, one for the type of article - both have only one category for each type. How do I, when pulling the content out via perch_content_custom get the name and / or slug for the categories that have been allotted to that article?

So, for instance, I may wish to have a different icon depending on what type of article it is. At the moment, I can filter what articles I get by category, but can't see simply how to output each article's category onto the page.

Mallen Baker

Mallen Baker 0 points

  • 7 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should be able to use the <perch:categories> tags from your main template.

So what am I doing wrong here - I'm trying to get the category and assign it to a php variable so I can use it for a 'related content' call. The template used is one that purely has the <perch:categories> tags.

    $category = perch_content_custom('Analysis', array(
        'page' => '/topics.php',
        'template' => '_just_category.html',
        'filter' => 'slug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'count' => 1
    ));

This returns the category correctly and prints it, but doesn't assign the value to the variable $category. If I run the following instead

    $category = perch_content_custom('Analysis', array(
        'page' => '/topics.php',
        'template' => '_just_category.html',
        'filter' => 'slug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'count' => 1
    ), true);

it completely disappears from the page. It doesn't completely die - the footer is still there. But everything related to this code and the subsequent perch_content_custom calls using the variable do not print.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does the variable contain at that point?

In the case of the first one, the variable contains nothing.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What does debug output for the page?

Nothing jumps out at me.

    <?php
        //get category and put it into a variable
        $category = perch_content_custom('Analysis', array(
        'page' => '/topics.php',
        'template' => '_just_category.html',
        'filter' => 'slug',
        'match' => 'eq',
        'value' => perch_get('s'),
        'count' => 1
    ),true);

    ?>

produces the following:

SELECT * FROM perch2_categories ORDER BY catTreePosition ASC
SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx 
JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID
JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID='6' AND idx.itemRev='47')) AND ((idx.indexKey='slug' AND idx.indexValue='how-to-get-to-grips-with-deforestation-risks')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 1
Using template: /templates/content/_just_category.html

Then the subsequent call:

    $results_found = false;
    //use that category to get related features
    $result = perch_content_custom('Analysis', array(
        'page' => '/topics.php',
        'template' => '_secondary_listing.html',
        'filter' => 'slug',
        'match' => 'neq',
        'value' => perch_get('s'),
        'count' => 2,
        'category' => "analysis/$category"
        ),true);

    if ($result){
        ?>
    <section class="related_stories">
    <h2>Related content</h2>
    <?php
        echo $result;
        $results_found = true;
    ?>

produces this:

SELECT * FROM perch2_categories ORDER BY catTreePosition ASC
SELECT idx.itemID FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID WHERE ((idx.regionID='6' AND idx.itemRev='47')) AND (idx.indexKey='_category' AND idx.indexValue LIKE 'analysis/\n Deforestation\n%')
SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx 
JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID
JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID='6' AND idx.itemRev='47')) AND idx.itemID IN (NULL) AND ((idx.indexKey='slug' AND idx.indexValue != 'how-to-get-to-grips-with-deforestation-risks')) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev
) as tbl GROUP BY itemID ORDER BY sortval ASC LIMIT 0, 2
Using template: /templates/content/_secondary_listing.html
Drew McLellan

Drew McLellan 2638 points
Perch Support

Looks like you need to check your inputs

idx.indexValue LIKE 'analysis/\n Deforestation\n%'

Thanks. I hadn't noticed that I needed to pull the slug, not the title - and I don't know why the variable was coming in with new line characters, but I've used trim() to strip them off and now it works.