Forum

Thread tagged as: Problem, Blog

Dynamic blog slug with multiple blogs

I've got multiple (two) blogs and I want to use the same date/tags/cats templates for them both. The href was /blog/category/<perch:category id="catSlug" />, which worked fine for my blog when it was just one blog. But now that I've got another blog ('resources') I need the /blog/ bit to be /blog/ when on the 'blog' blog and /resources/ when on the 'resources' blog. If that makes sense :P

I've tried using <perch:blog id="blogSlug" /> but it doesn't return anything. Can you see what I'm doing wrong?

Thanks for taking a look :)

Martin.

Martin Underhill

Martin Underhill 5 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

It should do. What does <perch:showall /> show you?

Ah good shout. It only returns setID… Is there some way of hooking that up to the set's slug value?

ID  Value
domain  https://tempertemper.local
url https://tempertemper.local/blog/
sharing_image   /cms/addons/feathers/tempertemper/img/tempertemper-web-design-logo-facebook.jpg
twittername @tempertemper
sitename    tempertemper Web Design
author  Martin Underhill
title   Resources for website owners
type    website
perch_page_path /blog
desc    
catID   23
setID   3
catParentID 0
catTitle    Business
catSlug business
catPath blog/business/
catDisplayPath  Business
catOrder    12
catTreePosition 003-012
catDynamicFields    {"desc":{"_flang":"markdown","raw":"","processed":""}}
count.blog.post 1
perch_desc  
catDepth    1
perch_item_first    true
perch_item_last true
perch_item_zero_index   0
perch_item_index    1
perch_item_rev_index    1
perch_item_rev_zero_index   0
perch_item_odd  
perch_item_count    1
perch_namespace perch:category
Drew McLellan

Drew McLellan 2638 points
Perch Support

Hmm, yes, I see the dilemma. Do you know from the page which blog you're displaying?

I think so, yeah. It's in the URL. <?php perch_page_url(); ?> returns 'https://tempertemper.local/resources'. Is there a way of just getting the 'resources' bit?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Is this Perch or Runway?

Runway

Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you see the value at the top of the debug?

Here's my debug from the page:

Time    Δ   Debug Message
0.0467  0   [48] SELECT p.pagePath, pr.routePattern, pr.routeRegExp, p.pageTemplate FROM perch2_pages p LEFT JOIN perch2_page_routes pr ON p.pageID=pr.pageID ORDER BY pr.routeOrder ASC, p.pagePath ASC
0.0502  0.0035  Matched page: /resources, so not using routes.
0.0503  0.0001  Using master page: /templates/pages/resources/index.php
0.0552  0.0048  [1] SELECT * FROM perch2_pages WHERE pagePath='/resources' LIMIT 1
0.0568  0.0016  Using template: /templates/pages/attributes/default.html
0.0571  0.0003  Using sub-template: /templates/pages/attributes/seo.html
0.0572  0.0001  Using sub-template: /templates/pages/attributes/open_graph.html
0.0573  0.0001  Using sub-template: /templates/pages/attributes/twitter.html
0.0629  0.0057  [5] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1 ORDER BY pageTreePosition ASC
0.0641  0.0012  [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/resources' LIMIT 1
0.0645  0.0004  [1] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006', '000') ORDER BY pageTreePosition DESC
0.0649  0.0004  [5] Using template: /templates/navigation/item.html
0.068   0.0031  Using template: /templates/search/search-form.html
0.0715  0.0035  [3] SELECT regionKey, regionHTML FROM perch2_content_regions WHERE regionPage='/resources' OR regionPage='*' ORDER BY regionPage DESC
0.0757  0.0042  [1] SELECT blogID FROM perch2_blogs WHERE blogSlug='resources' LIMIT 1
0.0762  0.0005  [10] SELECT SQL_CALC_FOUND_ROWS DISTINCT tbl.* FROM ( SELECT idx.itemID, main.*, idx2.indexValue as sortval FROM perch2_blog_index idx JOIN perch2_blog_posts main ON idx.itemID=main.postID AND idx.itemKey='postID' JOIN perch2_blog_index idx2 ON idx.itemID=idx2.itemID AND idx.itemKey='postID' AND idx2.indexKey='postDateTime' WHERE 1=1 AND idx.itemID=idx2.itemID AND idx.itemKey=idx2.itemKey GROUP BY idx.itemID, idx2.indexValue, postID ) as tbl WHERE ( blogID=1 AND postStatus='Published' AND postDateTime<='2016-04-29 08:58:00' ) GROUP BY itemID, sortval ORDER BY sortval DESC LIMIT 0, 10
0.1401  0.0639  [1] SELECT FOUND_ROWS() AS `count`
0.1443  0.0042  [63] SELECT DISTINCT settingID, settingValue FROM perch2_settings WHERE userID=0
0.1492  0.0048  [10] Using template: /templates/blog/post_in_list.html
0.1501  0.0009  Rescoping to perch:blog
0.1502  0.0001  Using sub-template: /templates/pagination/blog.html
0.1532  0.003   [1] SELECT * FROM perch2_blog_sections ORDER BY sectionTitle ASC
0.1543  0.0011  [2] SELECT * FROM perch2_blogs ORDER BY blogTitle ASC
0.1569  0.0026  [2] SELECT * FROM perch2_categories ORDER BY catTreePosition ASC
0.1795  0.0225  [1] SELECT * FROM perch2_blogs WHERE blogSlug='resources' LIMIT 1
0.1799  0.0004  [1] SELECT setID FROM perch2_category_sets WHERE setSlug='resources' LIMIT 1
0.1803  0.0004  [nil] SELECT main.* , COALESCE(cc.countValue,0) AS `count.blog.post` FROM perch2_categories main LEFT OUTER JOIN perch2_category_counts cc ON cc.catID=main.catID WHERE 1=1 AND setID=2 AND (cc.countType='blog.post' OR cc.countType IS NULL) AND cc.countValue > 0 ORDER BY catTitle ASC
0.1808  0.0005  Using template: /templates/blog/category_link.html
0.1811  0.0003  [1] SELECT * FROM perch2_blogs WHERE blogSlug='resources' LIMIT 1
0.1814  0.0003  [5] SELECT year(postDateTime) as year, COUNT(*) AS year_qty FROM perch2_blog_posts WHERE postStatus='Published' AND postDateTime<='2016-04-29 08:58:00' AND blogID=1 GROUP BY year ORDER BY year DESC
0.1824  0.001   [5] Using template: /templates/blog/year_link.html
0.1855  0.003   [1] SELECT * FROM perch2_blogs WHERE blogSlug='resources' LIMIT 1
0.1859  0.0004  [42] SELECT t.tagTitle, t.tagSlug, COUNT(p2t.postID) AS qty FROM perch2_blog_tags t, perch2_blog_posts_to_tags p2t, perch2_blog_posts p WHERE p2t.tagID=t.tagID AND p2t.postID=p.postID AND p.postStatus='Published' AND p.postDateTime<='2016-04-29 08:58:00' AND p.blogID=1 GROUP BY t.tagID ORDER BY t.tagTitle ASC
0.1874  0.0016  [42] Using template: /templates/blog/tag_link.html
0.199   0.0116  [5] SELECT * FROM perch2_pages WHERE pageNew=0 AND pageHidden=0 AND pageDepth >=0 AND pageDepth<=1 ORDER BY pageTreePosition ASC
0.1995  0.0005  [1] SELECT pageTreePosition FROM perch2_pages WHERE pagePath='/resources' LIMIT 1
0.1998  0.0003  [1] SELECT pageID FROM perch2_pages WHERE pageTreePosition IN ('000-006', '000') ORDER BY pageTreePosition DESC
0.2005  0.0007  [5] Using template: /templates/navigation/item.html
0.2035  0.003   [1] SELECT groupID FROM perch2_navigation WHERE groupSlug='footer-nav' LIMIT 1
0.2038  0.0003  [4] SELECT np.pageID, np.pageParentID, p.pagePath, p.pageTitle, p.pageNavText, p.pageNew, p.pageOrder, np.pageDepth, p.pageSortPath, np.pageTreePosition, p.pageAccessTags, p.pageAttributes FROM perch2_navigation_pages np, perch2_pages p WHERE p.pageID=np.pageID AND np.groupID=2 AND p.pageNew=0 AND np.pageDepth >=0 AND np.pageDepth<=2 ORDER BY np.pageTreePosition ASC
0.2043  0.0004  [0] SELECT np.pageTreePosition FROM perch2_pages p, perch2_navigation_pages np WHERE np.pageID=p.pageID AND np.groupID=2 AND p.pagePath='/resources' LIMIT 1
0.2046  0.0003  [4] Using template: /templates/navigation/list.html
0.2065  0.0019  [1] SELECT regionID, regionTemplate, regionPage, regionRev AS rev FROM perch2_content_regions WHERE regionKey='Primary content' AND (regionPage='/contact' OR regionPage='*')
0.207   0.0004  [1] SELECT * FROM ( SELECT idx.itemID, c.regionID, idx.pageID, c.itemJSON, idx2.indexValue as sortval FROM perch2_content_index idx JOIN perch2_content_items c ON idx.itemID=c.itemID AND idx.itemRev=c.itemRev AND idx.regionID=c.regionID JOIN perch2_content_index idx2 ON idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev AND idx2.indexKey='_order' WHERE ((idx.regionID=324 AND idx.itemRev=62)) AND idx.itemID=idx2.itemID AND idx.itemRev=idx2.itemRev ) as tbl GROUP BY itemID, pageID, itemJSON, sortval ORDER BY sortval ASC
0.2109  0.004   [1] Using template: /templates/content//footer_address.html

Any clues?

Drew McLellan

Drew McLellan 2638 points
Perch Support

It looks like it's going directly to a page called resources - is that right?

Yep, it's the 'resources' blog (as opposed to the 'blog' blog). If I did the same with the 'blog' blog, it would say 'blog' rather than resources. The pages listing in the control panel looks roughly like this, if that helps:

  • Shared
  • Home
  • Services
  • Projects
    • Case study
  • Testimonials
    • Testimonial
  • About
  • Blog
    • Post
    • Archive
  • Resources
    • Post
    • Archive
  • Contact
  • etc.
Drew McLellan

Drew McLellan 2638 points
Perch Support

I think in future we'll add a way to retrieve all the URL segments, but for now you'll need to use a bit of PHP to do it.

Makes sense. So is perch_page_path the way to go? Actually, wouldn't I run into bother when I return categories on /resources/post or /resources/archive (or /blog/post or /blog/archive)? My php chops aren't particularly special…

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, explode() the path on / and take item 1.

Hi Drew, I'm wrestling with the explode thing and can't get it working. I'm doing this on the page:

      $category_link = explode('/', $_SERVER['REQUEST_URI'])[0];
      PerchSystem::set_var('category_link', $category_link);
      perch_blog_categories([
        'sort'       => 'catTitle',
        'sort-order' => 'ASC',
        'blog'       => 'blog',
      ]);

But neither <perch:blog id="category_link" /> nor <perch:blog id="category_link" /> returns the variable inside the category_link.html template. Can you see what I'm doing wrong?

Maybe it's better I just hold out for a blog release with the way to retrieve the category set slug. It's not pressing that I need multiple blogs – I was just trying to wrap my head around it with my own blog. That said, it would still be really useful :)

Actually, in the meantime I guess I could duplicate the category, tag and date default templates, change blog to resources and define the new templates in the perch_blog_categories, perch_blog_tags and perch_blog_date_archive_years arrays.

Uh oh, I can't do it with the 'resource' blog's categories cos of that bug. I'll hold off for now :)