Forum

Thread tagged as: Question, Blog, Field-Types

Use value from template in custom blog function

I'm using a template to set the heading of an area as the name of a category, as set via the admin panel.

<h2>                  
  <span class="bold_line"><span></span></span>
  <span class="solid_line"></span>
  <perch:categories id="highlighted_category" set="blog" label="Higlighted Category" max="1" supress="true" display-as="checkboxes" help="Select one category to be highlighted on the main blog page. This will show the two most recent posts within that category.">
    <a href="archive.php?cat=<perch:category id="catPath" />" class="title_text">    
      <perch:category id="catTitle" type="text" />
      <perch:category id="catSlug" type="slug" supress="true" hidden="true">
    </a>
  </perch:categories>
</h2>

As well as using that category as the name, I then want to use that category to filter a cutom_blog function

<?php perch_blog_custom(array(
                                  'count'      => 2,
                                  'template'   => 'post_in_list_highlighted_category.html',
                                  'sort'       => 'postDateTime',
                                  'sort-order' => 'DESC',
                                  'category'   => perch_post('highlighted_category'), 
                              ));
                  ?>

Unfortunately this doesn't work, as it doesn't display any posts.

I feel like it's a syntax problem but I've tried a few different ways with no luck.

Diag details:

Am I even on the right track with what I'm trying to do?

Perch: 2.8.26, PHP: 5.5.9-1ubuntu4.13, MySQL: 5.5.44, with PDO
Server OS: Linux, apache2handler
Installed apps: content (2.8.26), assets (2.8.26), categories (2.8.26), perch_blog (5.0)
App runtimes: <?php $apps_list = array( 'content', 'categories', 'perch_blog', );
PERCH_LOGINPATH: /perch
PERCH_PATH: /home/www/client/blog.johnsonvillemedical.co.nz/public/perch
PERCH_CORE: /home/www/client/blog.johnsonvillemedical.co.nz/public/perch/core
PERCH_RESFILEPATH: /home/www/client/blog.johnsonvillemedical.co.nz/public/perch/resources
Image manipulation: GD
PHP limits: Max upload 50M, Max POST 50M, Memory: 128M, Total max file upload: 50M
F1: 2edba60ed1f613d6dd804feb202456a2
Resource folder writeable: Yes
HTTP_HOST: blog.johnsonvillemedical.co.nz
DOCUMENT_ROOT: /home/www/client/blog.johnsonvillemedical.co.nz/public
REQUEST_URI: /perch/core/settings/diagnostics/
SCRIPT_NAME: /perch/core/settings/diagnostics/index.php
Rob Stewart

Rob Stewart 0 points

  • 5 years ago
<?php perch_blog_custom(array(
                                  'count'      => 2,
                                  'template'   => 'post_in_list_highlighted_category.html',
                                  'sort'       => 'postDateTime',
                                  'sort-order' => 'DESC',
                                  'category'   => perch_get('cat'), 
                              ));
                  ?>

Thanks Robert, I've given this a go again but no luck. My understanding is the perch_get calls the information from the url of the page rather than from an external variable, and I'm trying to use this to output into is the blog index so there is no suitable information in the url to call from.

Have I misunderstood this?

perch_get() is used to access variables from url query string. perch_post() would access form variable if it was posted in the previous action. If you need to pass a variable you could do this on the query string, then capture it again using perch_get().

What I seem to have missed is where/how your passing this information, therefore I don't know how to instruct you to access like...

I'm actually not sure myself, I'm following what I've previously read in the forum but I may have the wrong approach.

The value I'm setting the selection of a category, and this is set by an administrator in the admin panel, using a perch_content function based on a custom template. This should mean that this value is stored in the database once the information is saved in the admin panel.

I then want to be able to get this value and use it as a filter within my perch_content_custom function, so that the section of the page displays the most recent posts which have the value as one of their categories.

bump - Anyone able to help me understand how to achieve what I'm trying to do?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Where is the information about your category coming from?

I'm currently using a perch_content (which uses a custom template) on the page on which I want to display the list. This template only asks which category the admin would like to use.

Never mind, I flagged it and went with an easier option.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Right, so you'd use

'category'   => perch_content('Category', true),