Limit content to one category?

  • Hi,


    I'm using id="cd_form" from a category template in a blog post - this bit works...


    The problem is when a blog post is associated with more than one category, id="cd_form" is displayed twice (or however many categories the post is linked to).


    How do I limit the output to just one category?


    I've hardcoded a few values below, to make the code easier to read


    Code
    1. perch_blog_custom([
    2. 'blog' => 'news',
    3. 'category' => 'news/control-and-display/', //Limit to one category
    4. 'filter' => 'postSlug',
    5. 'match' => 'eq',
    6. 'value' => perch_get('s'),
    7. 'template'=> my-template.html'
    8. ]);


    my-template.html looks like this:


    Code
    1. <perch:categories id="categories" set="news">
    2. <perch:category id="cd_form" type="text" />
    3. </perch:categories>


    I guess the problem is related to the way I'm filtering the post. I'm using 'filter' => 'postSlug' and the slug isn't unique (when a post is associated with more than one category)


    I thought using the category option in perch_blog_custom would fix this?


    I then thought perhaps a double filter might work:



    But that doesn't work, adding the second filter breaks perch_blog_custom, nothing shows...


    What's the best way to achieve this?




    Summary

  • Hey Stephen,


    Just trying to work out what you're trying to do here as I'm not 100% sure...


    Which category do you want the form to output, the first main category? If so, you could use a:

    Code
    1. <perch:categories id="categories" set="news">
    2. <perch:if exists="perch_item_first"><perch:category id="cd_form" type="text" /></perch:if>
    3. </perch:categories
  • Hi,


    I've used perch_item_first in the past, but in this instance I need the ID to be pulled from a specific category.


    A bit of background: I've added a third-party form to my website, each category has a different form. The form is identified by a unique ID, unfortunately I can't use perch_item_first, as that would only display the ID from the first category a post is linked to.

  • Hi


    At the moment I've hardcoded the control-and-display category, this will be a variable


    I would have thought  'category' => 'news/control-and-display/' would have done the trick - but it looks like it's being ignored as perch_blog_custom  is still displaying id="cd_form" from other categories?



    The original code works, in the sense that it displays id="cd_form". The problem is when a post is associated with more than one category. When that happens id="cd_form" is output more than once. Does that make sense?:/

  • All that'll do is filter all blog posts within the category, and then by your filters, so any post that has a category of control-and-display will be included. When it then outputs the post, all the categories will still be included.


    You'll need to find some mechanism to filter out the rest of the categories, and only return the one you want (be it the parent), or some sort of if to say if category is control-and-display, or another variable that you can set/pass through and then check with a `perch:if`.