Jump to content

News Section Setup Query


ridgedale
 Share

Recommended Posts

Hi,

I just wanted to check whether or not the following topic is still valid with respect to PW 3.x:

https://processwire.com/talk/topic/22-how-to-build-a-news-page/

I am trying to integrate a news section into a basic multisite PW website installation (voluntary project) intended for a community of two villages.
Currently there are 4 subdomains (hca-site, hfi-site, hhg-site, hps-site) that link back to the main domain website (https://thehanneys.uk) by clicking on their home buttons. I had a look at the Blog (ProcessBlog & MarkupBlog) module but it is not really what I'm looking for - a simple News section.

At the moment we're just concentrating on getting the functionality in place. The styling can come later.

The first issue encountered is that there is no checkbox under the Advanced section for "Page Numbers" to turn them on for the news-index template.
Secondly, I don't understand why the following text is being displayed above the navigation when I expected it to be displayed after the page title.
Thirdly, are the story pages displayed on the index page simply by creating a new child under News (page based on news-index.php template)?

Showing 0 of 0 Article(s) | Page 1 of 0

The current code for the news-index.php template is as follows:

<?php namespace ProcessWire;

// basic-page.php template file
// See README.txt for more information

// Primary content is the page's body copy
$content = $page->body;

// If the page has children, then render navigation to them under the body.
// See the _func.php for the renderNav example function.
if($page->hasChildren) {
    $content .= renderNav($page->children);
}

// if the rootParent (section) page has more than 1 child, then render
// section navigation in the sidebar
if($page->rootParent->hasChildren > 1) {
    $sidebar = renderNavTree($page->rootParent, 3) . $page->sidebar;
}

// start the news stories list
echo "<ul>";

// get the stories for this page
$stories = $page->children("limit=6, sort=-date");

// note if you set the stories to sort by date descending on the /news/ page
// in the admin, then you can omit the "sort=-date" above.

// cycle through each story and print it in a <li>
foreach($stories as $story) {
   echo "
       <li><a href='{$story->url}'>{$story->title}</a>
       <p><strong>Date:</strong> {$story->date}</p>
       {$story->body}
       </li>
       ";      
}

echo "</ul>";

// get values for our placemarker headline
$start = $stories->getStart();
$end = $start + count($stories);  
$total = $stories->getTotal();
$num = $input->pageNum;
$lastNum =  ceil($total / $stories->getLimit());

// output the placemarker headline
echo "<h5>Showing $start - $end of $total Article(s) | Page $num of $lastNum</h5>";
          
// output pagination links
echo $stories->renderPager();

The current code for the news-story.php template is as follows:

<?php namespace ProcessWire;

// basic-page.php template file
// See README.txt for more information

// Primary content is the page's body copy
echo "<div id='bodycopy'>";
    $content = $page->body;
echo "</div>";

// If the page has children, then render navigation to them under the body.
// See the _func.php for the renderNav example function.
if($page->hasChildren) {
    $content .= renderNav($page->children);
}

// if the rootParent (section) page has more than 1 child, then render
// section navigation in the sidebar
if($page->rootParent->hasChildren > 1) {
    $sidebar = renderNavTree($page->rootParent, 3) . $page->sidebar;
}

P.S. Are there any books/tutorials specifically covering PW 3.x?

Any assistance would be greatly appreciated.

 

Link to comment
Share on other sites

@ridgedale

42 minutes ago, ridgedale said:

The first issue encountered is that there is no checkbox under the Advanced section for "Page Numbers" to turn them on for the news-index template.
Secondly, I don't understand why the following text is being displayed above the navigation when I expected it to be displayed after the page title.
Thirdly, are the story pages displayed on the index page simply by creating a new child under News (page based on news-index.php template)?

1. check the URLs tab

2. you might be using delayed output, the template files are used to set some variables which are later echoed in another file included at the end of the template file

Something like this

<?php

$header = "<header>...</header>";
$content = "<div class='content'><h1>{$page->title}</h1><div>{$page->content}</div></div>";
$footer = "<footer>...</footer>";

// echoing something here will display above the header

include ("main.php");

The main.php file

<?php
// here we echo the variables set in the template file
echo "<html><body>...";
echo $header;
echo $content;
echo $footer;
echo "</body></html>";

3. 

$stories = $page->children("limit=6, sort=-date");

the above takes the 6 latest child pages of the current page, so on pages using the news-index.php template it gets the 6 latest child pages .

 

https://processwire.com/docs/tutorials/

  • Like 1
Link to comment
Share on other sites

@ridgedale I don't use the delayed output method but looking at the page source on your site, looks like your templates are being automatically included like this to produce a final page output at yoursite.com/news/:

1) _init.php
2) news-index.php
3) _main.php

You're getting this on your page:

<!-- THIS LINE IS OUTPUTTING FROM NEWS-INDEX.PHP BECAUSE YOU GOT ECHO COMMANDS IN THERE -->
<ul></ul><h5>Showing 0 - 0 of 0 Article(s) | Page 1 of 0</h5>
<!-- THEN _MAIN.PHP LOADS AFTER THE PREVIOUS TEMPLATE -->
<!DOCTYPE html>
<html lang="en">
<head>

Your news-index.php is supposed to populate placeholders (i.e. not echo out anything), then the _main.php actually outputs the page using the predefined values. So maybe you need to do something like:

// news-index.php
// define the placemarker headline
$placeholderHeadline = "Showing $start - $end of $total Article(s) | Page $num of $lastNum";

// main.php - this outputs the final page
// put this line where you want it to show on the page
<h5><?php echo $placeholderHeadline; ?></h5>

https://processwire.com/docs/tutorials/how-to-structure-your-template-files/page4

 

Another approach is what I do, which I find easier and more obvious what's going on:

 

1) Go to config.php and modify it so that the _main.php file IS NOT appended (this is what's happening to you).

/**
 * Prepend template file
 *
 * PHP file in /site/templates/ that will be loaded before each page's template file.
 * Example: _init.php
 *
 * @var string
 *
 */
$config->prependTemplateFile = '_init.php';

/**
 * Append template file
 *
 * PHP file in /site/templates/ that will be loaded after each page's template file.
 * Example: _main.php
 *
 * @var string
 *
 */
// I COMMENT OUT THE FOLLOWING LINE
// $config->appendTemplateFile = 'main.php';


2) Create file main.php (this is used to serve ALL of my pages
3) Create template, services-index.php, create file services-index.php
4) Go to template > files tab > alternate template filename - type 'main' (I do this for EVERY template that needs its own page)

1.thumb.png.22a6179b4d4f8ef8ae7566fc1eb596f4.png

5) Put the code in the files

// main.php
<!DOCTYPE html>
<html lang='en' class='no-js'>
<head>

</head>

<body>
  <!-- my header is in another file at /templates/includes/header.inc -->
  <?php include('./includes/header.inc'); ?>

  <!-- start main content -->
  <?php include($page->template->name); ?> 

  <!-- my footer is in another file at /templates/includes/footer.inc -->
  <?php include('./includes/footer.inc'); ?>
</body>
</html>

// services-index.php
<p>I'm loaded now through main when you go to mysite.com/services/ how awesome is that!</p>

I figure that the only part that generally changes on  page is the middle section between header and footer. And if I need something to change in the header, I just include a header file like I did with the footer. Then I can use logic in that file to output different things depending on what's needed.

Hope this makes sense and gives you some ideas of the different approaches available.

Link to comment
Share on other sites

  • 4 weeks later...

Hi fbg13 / SamC,

Apologies for not responding sooner and thank you for all your feedback.

In the end I decided to go down the route of removing everything and re-installing the development branch v. 3.0.54 that allows the regular-master profile (incorporating a blog module by default that can be customised as News) referred to in Ryan's Introducing a new ProcessWire site profile and ProcessWire 3.0.54 and Uikit 3 admin theme blog posts. This greatly simplified the integration of a news section in my offline testing.

I'm now in the process of removing everything, installing the development branch v. 3.0.58 (as this dev branch appears to be close to the stable version that is intended to be released) with the regular-master profile. The initial master site intallation went smoothly apart from a locale error/warning issue which I have detailed in the Warning about Server Locale after update from 3.0.52 > 3.0.53 - Help!  forum topic and am hoping to to resolve.

The issue I've found with the tutorials referred to is that they practically all deal with version 2.x or earlier of Processwire, btw.

Many thanks again for your help.

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...