Jump to content

Cannot put blog template as home


antoiba86
 Share

Recommended Posts

Well, I am trying to put the blog template like the home template but when I change the home template to the blog template the page explote. The version is processwire 3.055 and this is the website.

http://www.antoibaprogramming.com/

I think that the error is because that when is the home, the blog template try to look for the blog post but it didn't find anything so it crashes.

Here is the error that it gave me.

Quote
( ! ) Fatal error: Uncaught Error: Call to a member function count() on null in /home/antoiba/public_html/ProcessWire/site/templates/_uikit.php on line 660
( ! ) Error: Call to a member function count() on null in /home/antoiba/public_html/ProcessWire/site/templates/_uikit.php on line 660

And here is the code. The line 660 is the "$n = $page->comments->count();"

function ukBlogPost(Page $page, $options = array()) {
	
	$defaults = array(
		'summarize' => null, // Display blog post summary rather than full post? (null=auto-detect)
		'metaIcon' => 'info',
		'moreIcon' => 'arrow-right',
		'moreText' => __('Read more'), 
		'categoryIcon' => 'hashtag',
		'bylineText' => __('Posted by %1$s on %2$s'), 
	);

	$options = _ukMergeOptions($defaults, $options);
	$title = $page->title;
	$date = $page->date ? $page->date : $page->createdStr;
	$name = $page->createdUser->name; 
	$body = $page->body;
	$metaIcon = ukIcon($options['metaIcon']);
	$moreIcon = ukIcon($options['moreIcon']);
	$categoryIcon = ukIcon($options['categoryIcon']);
	$n = $page->comments->count();
	$numComments = $n ? "<a href='$page->url#comments'>" . ukIcon('comments') . " $n</a>" : "";
	
	if($options['summarize'] === null) {
		// auto-detect: summarize if current page is not the same as the blog post
		$options['summarize'] = page()->id != $page->id;
	}
	
	$categories = $page->categories->each($categoryIcon . 
		"<a class='uk-button uk-button-text' href='{url}'>{title}</a> "
	);

	if($options['summarize']) {
		// link to post in title, and use just the first paragraph in teaser mode
		$title = "<a href='$page->url'>$title</a>";
		$body = explode('</p>', $body); 
		$body = reset($body) . ' ';
		$body .= "<a href='$page->url'>$options[moreText] $moreIcon</a></p>";
		$class = 'blog-post-summary';
	} else {
		$class = 'blog-post-full';
	}

	if($options['summarize']) {
		$heading = "<h2 class='uk-margin-remove'>$title</h2>";
	} else {
		$heading = "<h1 class='uk-article-title uk-margin-remove'>$title</h1>";
	}
	
	$byline = sprintf($options['bylineText'], $name, $date); 
	
	// return the blog post article markup
	return "
		<article class='uk-article blog-post $class'>
			$heading
			<p class='uk-margin-small'>
			<span class='uk-article-meta'>
				$metaIcon
				$byline
			</span>
			<span class='categories'>
				$categories
			</span>
			<span class='num-comments uk-margin-small-left uk-text-muted'>
				$numComments
			</span>
			</p>
			
			$body
		</article>
		<hr>
	";	
}

 

The question is how can I make to put the blog like the principal page. I don't have any idea how to do it.

Link to comment
Share on other sites

Hi, the blog works by using page()->children... etc. (see blog.php). When switching the template, the children will be the pages under Home which are not pages of blog-post templates, so there are no comments (among other things) to display. You need at least to look for the selector(s) being used by templates, and change them accordingly.

Edited by szabesz
typo
  • Like 1
Link to comment
Share on other sites

On 6/2/2017 at 8:42 AM, szabesz said:

Hi, the blog works by using page()->children... etc. (see blog.php). When switching the template, the children will be the pages under Home which are not pages of blog-post templates, so there are no comments (among other things) to display. You need at least to look for the selector(s) being used by templates, and change them accordingly.

Many thanks. Sometimes I think that my brain it's broken because I think of that but I didn't actually "see" it so with your comment I could resolve. I work every day and later I am very tired.

I put the code. I know it's a bit dirty but well, it's my blog and I don't mind very much. It's work and that's the only important thing.

In the blog template, I wrote this code and now it works.

<div id='content'>
	<?php
	echo ukHeading1(page()->title, 'divider'); 
	$pages_not_blog = page()->children('limit=10');
        foreach ($pages_not_blog as $page_not_blog) {
            if ($page_not_blog->name === "blog") {
                $posts = $page_not_blog->children('limit=10');
                echo ukBlogPosts($posts); 
            }
        }
        
	
	?>
</div>

 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...