Forum

Thread tagged as: Question, Problem, Blog

Related Blog Posts with Multiple Blogs

Hi there,

I am trying to display related articles on a blog post page, but its not working for me. I have implemented this code from another forum post

$categories = perch_blog_post_categories(perch_get('s'), 'cat_slug.html', true);


if (count($categories)) {
    $cat_slugs = array();
    foreach($categories as $cat) {
        $cat_slugs[] = $cat['catSlug'];
    }

    perch_blog_custom(array(
        'filter' => 'postSlug',
        'match' => 'neq',
        'value' => perch_get('s'),
        'category' => $cat_slugs,
        'count'=> 3,
        'blog'     => REGION,
        'template'=> 'blog/post_sidebar.html'
    ));
}

The cat_slug.html template is the following

<perch:category id="catSlug" type="slug" for="catTitle" order="2" />

and my post_sidebar.html template is the following

<perch:before>
<ul class="popular-news-articles">
</perch:before>
    <li>
        <div class="popular-news-article__img">
            <a href="<perch:blog id="postURL" />">
                <img src="<perch:if exists="image"><perch:blog id="image" type="image" width="142" height="94" crop="true" /><perch:else />/assets/img/news/default-142x94.jpg</perch:if>" alt="<perch:blog id="postTitle" />" />
            </a>
        </div>
        <time class="c700  zeta  caps  news-date">
            <perch:blog id="postDateTime" format="%d %B %Y" />
        </time>
        <a href="<perch:blog id="postURL" />"><perch:blog id="postTitle" /></a>
    </li>
<perch:after>
</ul>
</perch:after>

REGION global variable

Within the perch_blog_custom code you will notice REGION

This is defined in my config file as

$explode = explode('/', $_SERVER["REQUEST_URI"]);
define('REGION', $explode[1]);

It is parsing the first part of the URL i.e

  • /en-us
  • /en-eu

My blogs are named the same (See Image below) https://www.awesomescreenshot.com/image/997786/2bbaefa9059778dd859412c77d90a033

Archive Page (Might Help)

What might help is, I had problems with displaying categories within Multiple Blogs on my archive page and to get around this I had to use

if (perch_get('cat')) {

    perch_blog_custom(array(
        'category'   => REGION.'/'.perch_get('cat'),
        'template'   => $template,
        'count'      => $posts_per_page,
        'sort'       => $sort_by,
        'blog'       => REGION,
        'sort-order' => $sort_order,
    ));

    $posts_displayed = true;
}

As my category path is (For Europe below and for the us it would be en-us)

  • en-eu/events/
  • en-eu/presentations/
  • en-eu/press-releases/

Errors on Post Page

For the code I had used posts do appear but they are not related in terms of the category and I get these two errors

Warning: file_put_contents(/Users/Barry/Sites/chicopeesolutions/admin/resources/perch_blog.perch_blog_post_categoriesc34cd185575513f9f894f5ed8358f1c9.cache): failed to open stream: Permission denied in /Users/Barry/Sites/chicopeesolutions/admin/addons/apps/perch_blog/PerchBlog_Cache.class.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0018  257904  {main}( )   ../start.php:0
2   0.1561  4340056 include( '/Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php' )   ../start.php:69
3   0.6795  5567808 perch_blog_post_categories( )   ../post.php:55
4   0.6991  5677752 PerchBlog_Cache::save_static( ) ../runtime.php:292
5   0.6991  5678000 file_put_contents ( )   ../PerchBlog_Cache.class.php:53

( ! ) Warning: Invalid argument supplied for foreach() in /Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php on line 60
Call Stack
#   Time    Memory  Function    Location
1   0.0018  257904  {main}( )   ../start.php:0
2   0.1561  4340056 include( '/Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php' )   ../start.php:69
- See more at: https://chico.pee/en-eu/news/2012-07-03-cleaning-and-the-environment#sthash.Bo7ykeCG.dpuf

Perch is up to date and below is my Diagnostics Report

Perch Runway: 2.8.25
Production mode: Production (100)
Installed apps: content (2.8.25), assets (2.8.25), categories (2.8.25), perch_blog (5.0), perch_forms (1.8.3), perch_twitter (3.5.1)
DB driver: PDO
DB tables: perch2_backup_plans (0), perch2_backup_resources (0), perch2_backup_runs (0), perch2_blog_authors (3), perch2_blog_comments (0), perch2_blog_index (2478), perch2_blog_posts (109), perch2_blog_posts_to_tags (0), perch2_blog_sections (1), perch2_blog_tags (0), perch2_blogs (3), perch2_categories (60), perch2_category_counts (7), perch2_category_sets (7), perch2_collection_index (11108), perch2_collection_items (317), perch2_collection_revisions (110), perch2_collections (13), perch2_content_index (9972), perch2_content_items (1411), perch2_content_regions (117), perch2_forms (2), perch2_forms_responses (7), perch2_navigation (5), perch2_navigation_pages (23), perch2_page_routes (45), perch2_page_templates (25), perch2_pages (60), perch2_resource_log (3426), perch2_resource_tags (0), perch2_resources (951), perch2_resources_to_tags (0), perch2_settings (29), perch2_twitter_scheduled_tweets (0), perch2_twitter_settings (1), perch2_twitter_tweets (690), perch2_user_passwords (0), perch2_user_privileges (42), perch2_user_role_privileges (16), perch2_user_roles (2), perch2_users (5)
Users: 5
App runtimes:
<?php
    $apps_list = array(
        'content',
        'categories',
        'perch_blog',
        'perch_twitter',
        'perch_forms',
    );
Scheduled tasks for perch_blog: delete_spam_comments (1440 mins)
Scheduled tasks for perch_twitter: post_tweets (1 mins), update_tweets (60 mins)
Editor plug-ins: markitup
H1: 708816c55ecbf2cc5a65adc98b79822c
L1: 20240be7944a7944e62b3cd88072c3ae
F1: dc1fef2ad0fcd9f943c02ebb43d85dbc
headerColour: #e61e26
content_singlePageEdit: 1
helpURL:
siteURL: /
hideBranding: 0
content_collapseList: 1
lang: en-gb
update_2.8.16: done
perch_blog_update: 5.0.1
headerScheme: dark
update_runway_2.8.16: done
latest_version: 2.8.15
on_sale_version: 2.8.25
update_runway_2.8.19: done
dashboard: 0
hide_pwd_reset: 0
content_hideNonEditableRegions: 0
content_frontend_edit: 0
perch_blog_post_url: /{blogSlug}/news/{postSlug}
perch_blog_site_name: Chicopee Solutions
perch_blog_slug_format: %Y-%m-%d-{postTitle}
perch_blog_akismet_key:
perch_blog_max_spam_days: 1
perch_blog_comment_notify: 0
update_runway_2.8.23: done
logoPath: /admin/resources/1453300953_chicopee-logo.png
perch_twitter_update: 3.5
update_runway_2.8.24: done
update_runway_2.8.25: done
PERCH_DEVELOPMENT: 10
PERCH_STAGING: 50
PERCH_PRODUCTION: 100
PERCH_SITEPATH: /Users/Barry/Sites/chicopeesolutions
PERCH_SCHEDULE_SECRET: 8wJeeQvIgirzvJPg
PERCH_DB_USERNAME: teamfcuk_chicope
PERCH_DB_SERVER: 87.117.252.235
PERCH_DB_DATABASE: teamfcuk_chicopee
PERCH_DB_PREFIX: perch2_
PERCH_YOUTUBE_API_KEY: AIzaSyBeSDvZackLJIyG4btIaz8qppXIDf8I0mo
PERCH_EMAIL_FROM: barry@teamfc.co.uk
PERCH_EMAIL_FROM_NAME: Barry Corrigan
PERCH_LOGINPATH: /admin
PERCH_PATH: /Users/Barry/Sites/chicopeesolutions/admin
PERCH_CORE: /Users/Barry/Sites/chicopeesolutions/admin/core
PERCH_RESFILEPATH: /Users/Barry/Sites/chicopeesolutions/admin/resources
PERCH_RESPATH: /admin/resources
PERCH_CLEAN_RESOURCES:
PERCH_HTML5: 1
PERCH_TZ: UTC
PERCH_RUNWAY: 1
PERCH_ERROR_MODE: DIE
PERCH_DATE_LONG: %d %B %Y
PERCH_DATE_SHORT: %d %b %Y
PERCH_TIME_SHORT: %H:%M
PERCH_TIME_LONG: %H:%M:%S
PERCH_RUNWAY_ROUTED:
PERCH_STRONG_PASSWORDS:
PERCH_DEBUG:
PERCH_PREVIEW_ARG: preview
PERCH_TEMPLATE_PATH: /Users/Barry/Sites/chicopeesolutions/admin/templates
PERCH_DEFAULT_DOC: index.php
PERCH_DEFAULT_EXT: .php
PERCH_PRODUCTION_MODE: 100
PERCH_RWD:
PERCH_HTML_ENTITIES:
PERCH_SSL:
PERCH_STRIPSLASHES:
PERCH_PROGRESSIVE_FLUSH: 1
PERCH_AUTH_PLUGIN:
PERCH_DB_CHARSET: utf8
PERCH_DB_PORT:
PERCH_DB_SOCKET:
PERCH_SESSION_TIMEOUT_MINS: 20
PERCH_APPS_EDITOR_PLUGIN: markitup
PERCH_APPS_EDITOR_MARKUP_LANGUAGE: textile
HOSTING SETTINGS

PHP: 5.6.10
Zend: 2.6.0
OS: Darwin
SAPI: apache2handler
Safe mode: not detected
MySQL client: mysqlnd 5.0.11-dev - 20120503 - $Id: 3c688b6bbc30d36af3ac34fdd4b7b5b787fe5555 $
MySQL server: 5.5.48-cll
Free disk space: 693.47 GB
Extensions: Core, date, ereg, libxml, openssl, pcre, sqlite3, zlib, bcmath, bz2, calendar, ctype, curl, dom, hash, fileinfo, filter, ftp, gd, SPL, iconv, intl, json, ldap, mbstring, session, standard, mysqlnd, mysqli, PDO, pdo_mysql, pdo_sqlite, Phar, posix, Reflection, mysql, SimpleXML, soap, sockets, exif, tokenizer, wddx, xml, xmlreader, xmlwriter, xsl, zip, apache2handler, imap, gettext, mcrypt, yaz, pgsql, pdo_pgsql, xdebug
GD: Yes
ImageMagick: No
PHP max upload size: 32M
PHP max form post size: 32M
PHP memory limit: 128M
Total max uploadable file size: 32M
Resource folder writeable: Yes
Session timeout: 24 minutes
Native JSON: Yes
Filter functions: Yes
Transliteration functions: Yes
HTTP_HOST: chico.pee
HTTP_CONNECTION: keep-alive
HTTP_ACCEPT: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
HTTP_UPGRADE_INSECURE_REQUESTS: 1
HTTP_USER_AGENT: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.47 Safari/537.36
HTTP_REFERER: https://chico.pee/admin/core/settings/diagnostics/
HTTP_ACCEPT_ENCODING: gzip, deflate, sdch
HTTP_ACCEPT_LANGUAGE: en-US,en;q=0.8,fr;q=0.6
HTTP_COOKIE: region=default; __unam=6bc6479-152a14de0f7-3a3a7cbb-823; cmsa=1; PHPSESSID=28f26756d3c0257a7bae3663338a8fb0
PATH: /usr/bin:/bin:/usr/sbin:/sbin
SERVER_SOFTWARE: Apache
SERVER_NAME: chico.pee
SERVER_ADDR: ::1
SERVER_PORT: 80
REMOTE_ADDR: ::1
DOCUMENT_ROOT: /Users/Barry/Sites/chicopeesolutions
SERVER_ADMIN: you@example.com
SCRIPT_FILENAME: /Users/Barry/Sites/chicopeesolutions/admin/core/settings/diagnostics/index.php
REMOTE_PORT: 49487
GATEWAY_INTERFACE: CGI/1.1
SERVER_PROTOCOL: HTTP/1.1
REQUEST_METHOD: GET
QUERY_STRING: extended
REQUEST_URI: /admin/core/settings/diagnostics/?extended
SCRIPT_NAME: /admin/core/settings/diagnostics/index.php
PHP_SELF: /admin/core/settings/diagnostics/index.php
REQUEST_TIME_FLOAT: 1455554311.05
REQUEST_TIME: 1455554311
argc: 1
Fishtank Creative

Fishtank Creative 2 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Your resources folder needs to be writable for Blog to be able to cache its output.

My resources folder is now writeable (Change from 755 to 777) not sure if that makes a difference though,

Some posts have the warnings of,

Warning: file_put_contents(/Users/Barry/Sites/chicopeesolutions/admin/resources/perch_blog.perch_blog_post_categoriesb8a7c04b276f55537b00b29a173c7d34.cache): failed to open stream: Permission denied in /Users/Barry/Sites/chicopeesolutions/admin/addons/apps/perch_blog/PerchBlog_Cache.class.php on line 53
Call Stack
#   Time    Memory  Function    Location
1   0.0010  257472  {main}( )   ../start.php:0
2   0.1510  4339312 include( '/Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php' )   ../start.php:69
3   0.6859  5511296 perch_blog_post_categories( )   ../post.php:55
4   0.7059  5621304 PerchBlog_Cache::save_static( ) ../runtime.php:292
5   0.7059  5621552 file_put_contents ( )   ../PerchBlog_Cache.class.php:53

( ! ) Warning: Invalid argument supplied for foreach() in /Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php on line 60
Call Stack
#   Time    Memory  Function    Location
1   0.0010  257472  {main}( )   ../start.php:0
2   0.1510  4339312 include( '/Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php' )   ../start.php:69
- See more at: https://chico.pee/en-eu/news/2016-02-05-a-spotless-image-at-moscow-airport#sthash.MblfgnEX.dpuf

But then other posts have

Warning: Invalid argument supplied for foreach() in /Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php on line 60 Call Stack #TimeMemoryFunctionLocation 10.0009257568{main}( )../start.php:0 20.13384339800include( '/Users/Barry/Sites/chicopeesolutions/admin/templates/pages/blog/post.php' )../start.php:69 - See more at: https://chico.pee/en-eu/news/2016-02-05-hygienic-cleaning-in-qsrs-ishus-aps-kfc-denmark-franchisee#sthash.vzm7XaeH.dpuf

But even if these warnings are fixed, the articles displayed are still not related to the category.

Drew McLellan

Drew McLellan 2638 points
Perch Support

What do your options resolve to?

print_r(array(
        'filter' => 'postSlug',
        'match' => 'neq',
        'value' => perch_get('s'),
        'category' => $cat_slugs,
        'count'=> 3,
        'blog'     => REGION,
        'template'=> 'blog/post_sidebar.html'
    ));

Isn't outputting the category path

Array
(
    [filter] => postSlug
    [match] => neq
    [value] => 2016-02-05-a-spotless-image-at-moscow-airport
    [category] => Array
        (
        )

    [count] => 3
    [blog] => en-eu
    [template] => blog/post_sidebar.html
)
Drew McLellan

Drew McLellan 2638 points
Perch Support

$categories is a string, so you can't loop through it.

So is this not possible?

Drew McLellan

Drew McLellan 2638 points
Perch Support

I think you probably want to use skip-template to get the data back. That, or output the string in a way you can split it programatically.

Ok thanks for your help. Have you an idea of how I would go about that?

Or can anyone point me in the right direction :-)

Drew McLellan

Drew McLellan 2638 points
Perch Support

I thought I just did! Did you try skip-template ?

I've just tried this, but it's still outputting the same as before


$categories = perch_blog_post_categories(perch_get('s'), 'cat_slug.html', true); if (count($categories)) { $cat_slugs = array(); foreach($categories as $cat) { $cat_slugs[] = perch_blog_category($cat['catSlug'], array( 'skip-template' => 'true', )); } print_r(array( 'filter' => 'postSlug', 'match' => 'neq', 'value' => perch_get('s'), 'category' => $cat_slugs, 'count'=> 3, 'blog' => REGION, 'template'=> 'blog/post_sidebar.html' )); }
Drew McLellan

Drew McLellan 2638 points
Perch Support

The problem is here:

$categories = perch_blog_post_categories(perch_get('s'), 'cat_slug.html', true);

try

$categories = perch_blog_post_categories(perch_get('s'), [
    'skip-template' => true,
]);

I now get this

Array
(
    [filter] => postSlug
    [match] => neq
    [value] => 2016-01-07-chicopee-needs-your-vote
    [category] => Array
        (
            [0] => news
        )

    [count] => 3
    [blog] => en-us
    [template] => blog/post_sidebar.html
)

But when I replace print_r with perch_blog_custom nothing at all appears.

I wonder if the category path is the issue as well?

Here is a post with all categories ticked



Array ( [filter] => postSlug [match] => neq [value] => 2016-01-07-chicopee-needs-your-vote [category] => Array ( [0] => events [1] => case-studies [2] => news ) [count] => 3 [blog] => en-us [template] => blog/post_sidebar.html )

This is now working by changing the category path to this

$cat_slugs = array();
foreach($categories as $cat) {
    $cat_slugs[] = REGION.'/'.$cat['catSlug'];
}

Full code is listed below

$categories = perch_blog_post_categories(perch_get('s'), [
    'skip-template' => true,
]);

if (count($categories)) {

    $cat_slugs = array();
    foreach($categories as $cat) {
        $cat_slugs[] = REGION.'/'.$cat['catSlug'];
    }

    perch_blog_custom(array(
        'filter' => 'postSlug',
        'match' => 'neq',
        'value' => perch_get('s'),
        'category' => $cat_slugs,
        'count'=> 3,
        'blog'     => REGION,
        'template'=> 'blog/post_sidebar.html'
    ));

    // print_r(array(
    //     'filter' => 'postSlug',
    //     'match' => 'neq',
    //     'value' => perch_get('s'),
    //     'category' => $cat_slugs,
    //     'count'=> 3,
    //     'blog'     => REGION,
    //     'template'=> 'blog/post_sidebar.html'
    // ));
}