How To Limit perch_blog_tags with Count

  • hi! I'm using the perch_blog_tags but I don't know how to limit the amount of tags shown. So I've got this working, but too many tags showing:


    PHP
    1. <?php $tags = perch_blog_tags('custom_tag_link.html', true); ?>

    But how do I reference the blog_tags in a blog_custom?

    PHP
    1. <?php
    2. perch_blog_custom(array(
    3. 'count' => 10,
    4. 'template' => 'custom_tag_link.html',
    5. ));
    6. ?>
  • Hi,


    I'm not sure you can do it that way, but you can limit the number of tags you output in the template with conditional tags:


  • Cool that worked. Thanks!


    Though I realize the tags are limited, as I can't change them to random, or the order. Are their other display options I have for the tags?


    For context, this is for a "tag cloud" for a blog. I want to show 10 tags, but have them random for each load (since there are 50+ tags, I want to show some but not all). Does that make sense?

  • As far as I know there is no runtime function to do this.


    You could use the Tags class directly, just be careful when updating the app in the future. Note that your template may need to use the perch:content namespace here. And this won't get you the quantity value qty like perch_blog_tags():





    If you also need the quantity, an option would be to set up your template to render the list in JSON:



    And in PHP you can convert the JSON to an array, randomise the order of the array items, slice the array to the size you want and render the list with perch_template()

    PHP
    1. // get the rendered JSON and convert it into an array
    2. $tags = json_decode( perch_blog_tags('tag_json', true), true );
    3. if($tags !== NULL) {
    4. // randomise the order of the array items
    5. shuffle($tags);
    6. // render a template with a slice of the array (0 - 10)
    7. perch_template( 'blog/tag_link', array_slice($tags, 0, 10) );
    8. }