Output blog posts in date order whilst limiting by category?

  • Hello,


    I am trying to output blog posts in date order whilst limiting other parameters such as the category.


    Ultimately I want six blog posts listed in date order; however, I am always showing x1 video, x1 podcast, x1 blog and x3 news.


    Whilst I have come close with a pure PHP way of achieving the goal, I've hit a snag whilst trying to output the Category Icon I have created. This appears to be ignoring the category array altogether?



    The above is my current code.


    Ideally it would be nice to build up some filters and then just do something like…


    PHP
    1. perch_blog_custom([
    2. 'sort' => 'postDateTime',
    3. 'template' => 'home/_blog-card.html',
    4. 'sort-order' => 'DESC',
    5. 'filter' => $news,
    6. ]);

    Appreciate any helped offered

  • This is pretty close to what I am after…


    I just need them in date order now…

  • Could you not do what you were doing before then use perch_template to output the results? Should let you use the category tags as well.

  • It might be the way the arrays are added together? As the items that return a single post will actually be $news[0], so if we just the single items in and update the sort function that could work

  • I really appreciate the help, but unfortunately this outputs the template, but the perch fields are empty.


    So there is no confusion, here is the current code…


  • I now get a Template file not found: templates/home/_blog-card.html even though the template is defiantly in the right place.


    /templates/content/home/_blog-card.html


    Debug also shows…


    Code
    1. Array
    2. (
    3. [type] => 8
    4. [message] => Trying to access array offset on value of type null
    5. [file] => /Users/grant/Sites/Development/180-Cargilfield-Perch-Updates/cms/templates/layouts/home-blog-grid.php
    6. [line] => 33
    7. )

    Which is…


    Code
    1. return strtotime($b[0]['postDateTime']) - strtotime($a[0]['postDateTime']);
  • I think

    Code
    1. return strtotime($b[0]['postDateTime']) - strtotime($a[0]['postDateTime']);

    Should be

    Code
    1. return strtotime($b['postDateTime']) - strtotime($a['postDateTime']);

    For the template, what is the path in 'perch_template()' now, and where did you add the new template?

  • Hi Byron Fitzgerald ,


    With some tweaks we have success! However, this does cause one further issue…


    Here is the current code


    Which now calls the content/home/_blog-card.html. Ideally I wanted to use the /blog/home/_blog-card.html template as it uses the category info to do other stuff.


    Here is that template…


    So whilst I can move this to content/home/… and change perch:blog to perch:content. The same is not true for the perch:categories


    Thanks ever so much for the help thus far…

  • Perch category tags are templated differently to perch:blog and perch:content, and exist in their own namespace. You should be able to to keep using the <perch:categories> and <perch:category/> tags as you would in your blog template. Only the <perch:blog/> namespaces require changing as it's the primary namespace

  • Oh really? Thats odd then, as the category stuff doesn't work in this template…


    When that is called via perch_blog_custom everything works as expected?

  • Actually I found a way, I used <perch:categories id="perch_categories" set="blog">. So id="perch_categories" instead of id="categories".


    Thanks all for the help. My final card template now looks like this…