Jump to content

How to create a simple news system for PW


Joss

Recommended Posts

A short tutorial/help package aimed at new users

(Note, a more step by step version of this tutorial is now available on the Wiki as a follow on to the Basic Website Tutorial - http://wiki.processwire.com/index.php/Simple_News_System ) - BE CAREFUL - don't mix the two. Either use the wiki or my files, not both at the same time. To be honest, you will learn more using the wiki version.

This is an example script showing how to create a very basic news system with categories in PW.

It includes templates and functions and full instructions of how to use it, which fields to create and so on.

This is NOT a module or an instant solution, but it will help get you going faster if you have not done this sort of thing before.

This example is for when you have a site carefully constructed round the page hierarchy, but want to add a basic news or blog that sits at least partly outside that tree.

The system helps you to create a single news page that lists articles that are stored under a hidden parent.

The articles are sorted by category - categories are created under another hidden parent and are selected with a page field.

Full instructions are in the main newsfunctions.inc script and everything is commented.

You can find it all here:

https://github.com/jsanglier/Pw-News

Joss

PS: This is just one way to do a news system and should be taken as an example, not as the best way. 

  • Like 6
Link to comment
Share on other sites

Oh I got a couple of things up there already. (and I used to have a ton of things on Source Forge about 12 years ago - and on Fresh Meat)

Though this one was a hassell. The first version Uploaded fine, then I went to update it and it complained about a Detached Head and then a bunch of other errors .... 

So, tried again. But I did add categories in the gap between the two! :)

Link to comment
Share on other sites

I'm a new developer but am really loving PW. I'm finding it pretty easy to build simple sites.  More complicated things have eluded me up until now. Joss, your tutorials are awesome. I'm learning tons from them and can see how to expand the concepts you're teaching to other tasks I want to accomplish.

Please keep publishing. Your lessons are valuable - and very much appreciated.

Aaron

Link to comment
Share on other sites

Hi,

I'm new to ProcessWire and i'm learning how to implement it as a CMS. So far i'm finding it much more suited to my needs than WordPress, which is what i've used before.

I've followed your Basic Website Tutorial but i'm having a problem with this one, basically this is what's happening:

XQtpTfG.png

Heres my news-index.php file: 

<?php
include("./header.inc");
include("./news_functions.inc");

// Give the news index page a title an a bit of an intro
 
		echo "<h1>{$page->title}</h1>";
		echo $page->body;
 
// Render the Category List
 
		categoriesList();
 
// Render the News List
 
		newsList();

include("./footer.inc");

news.php

<?php
include("./header.inc");
include("./news_functions.inc");

// Output the newsDisplay function
 
newsDisplay();

include("./footer.inc");

and news-functions.inc

/* LIST ARTICLES
 
This displays a list of articles in div containers on the news-index page. 
 
 
*/
 
function newsList(){
 
	// Grab the page name from the url
 
	$thisCategory = wire("page")->name;
 
	// If the category is not called "news" then output the category name as a selector for the find.
 
	if($thisCategory !="news") {
		$category = "article_category.name=" . $thisCategory;
	}	
 
	// Get the news posts - limited to ten for later pagination
 
	$newsposts = wire("pages")->find("parent=/news-articles/, $category, template=news, limit=10");
 
	$out =" ";
 
	//Loop through the pages
 
	foreach($newsposts as $newspost){
		$out .="<div class='clearfix'>";
		if($newspost->article_newsimage){
			$out .="<a href='{$newspost->article_newsimage->url}' class=''>";
			$out .="<img class='align_left' src='{$newspost->article_newsimage->getThumb(listingthumb)}'>";
			$out .="</a>";
		}
		$out .="<a href='{$newspost->url}'><h3>{$newspost->title}</h3></a>";
		$out .="<p>{$newspost->article_introtext}</p>";
		$out .="</div>";
 
	}
	// Pagination
 
	$out .="<div class='pagination'>";
	$out .= $newsposts->renderPager(array(
 
				    'nextItemLabel' => "Next",
				    'previousItemLabel' => "Prev",
				    'listMarkup' => "<ul>{out}</ul>",
				    'itemMarkup' => "<li>{out}</li>",
				    'linkMarkup' => "<a href='{url}'>{out}</a>"   
 
					));
	$out .="</div>";
 
	echo $out;
 
}

/* CATEGORIES LIST
 
This lists available categories
 
*/
 
function categoriesList(){
	$categories = wire("pages")->find("parent=/categories/, template=news_index, sort=title");
	$out =" ";
	foreach($categories as $category){
		$out .="<li><a href='{$category->url}'>{$category->title}</a></li>";
	}
 
	echo "<ul>$out</ul>";
 
}

I don't have much understanding of php so all the code is basically copy-pasted from your tutorial.

I was able to get the news page to load correctly when i replaced "include("./news_functions.inc");" with the code from news-functions.inc in news-index.php, but it doesn't work with the include.

Link to comment
Share on other sites

I think your news functions file is missing the <?php tags

That was the problem.

The news index, news article pages, and categories are now working perfectly.

Only two issues i'm trying to figure out now. 

The news index shows the oldest post first. How do i get posts to display from the newest to the oldest? 

And the pagination does not work. I created 12 test articles under the news articles page, the first ten show up in the news index, but clicking next or 2 in the pagination does not load the second news index page. I can see ?page=2 in the address bar but the same page displays again.

Link to comment
Share on other sites

On the sort, in the find() statement you can add a sort=created or sort=-created depending on which direction you want to head.

Have you got page numbers turned on for the news index template?

  • Like 1
Link to comment
Share on other sites

Hi Joss,

thanks for this. Good source for me to getting closer with pw, but a question appear.

Your files just located inside the template folder and contain functions with all the output mixed with php. If the functions wrap with a class and the output strip out to single templates. How the file structure looks then? Where to place the class and the corresponding files? Would it be a module then?

thanks, Stefan

Link to comment
Share on other sites

HI Stephan - I have a really bad cold and I am not totally sure what you are asking.

You can put anything anywhere you want really without it having to be a module. In fact, a module would probably make this too limited.

The only reason I put these into functions was to demonstrate how simple the template files can be. But you could write these as includes, you could write them directly into the template files themselves and so on. You can follow any kind of file structure you want within the template directory.

Basically, you can separate logic and display out completely if you want, or work more inline. I tend to mix it up because I never seem to do things the same way twice!

Link to comment
Share on other sites

Thanks! that worked  :)

Is it possible to add a simple archives feature to this system? For browsing the articles by year and month?

No reason why not. Pages automatically have two values "created" and "modified" which are unix time stamps. So, when you do your "find" statement you could add that to the mix.

Probably run it in the same way I have done the categories - create a list of years or months, pass them via the URL as a get, then retrieve them using "input" as I have done with the category. Then use that as a selector somehow. 

Need to check with Ryan or someone as to what parameters you could use to get the year bit from the unix time stamp.

Alternatively, you could add an extra field that was a year into each of the news article pages, and use that as one of your selectors. 

Like with most things with ProcessWire, there is probably a lot of ways you can do this!

Link to comment
Share on other sites

Hello Joss

I have a really bad cold and I am not totally sure what you are asking.

Well done, you guessed my question. thanks for your explanation.

I tend to mix it up because I never seem to do things the same way twice!

sounds logical
 
Get well!


adios, stefan

Link to comment
Share on other sites

No reason why not. Pages automatically have two values "created" and "modified" which are unix time stamps. So, when you do your "find" statement you could add that to the mix.

Probably run it in the same way I have done the categories - create a list of years or months, pass them via the URL as a get, then retrieve them using "input" as I have done with the category. Then use that as a selector somehow. 

Need to check with Ryan or someone as to what parameters you could use to get the year bit from the unix time stamp.

Alternatively, you could add an extra field that was a year into each of the news article pages, and use that as one of your selectors. 

Like with most things with ProcessWire, there is probably a lot of ways you can do this!

I tried doing something similar to how you've done the categories and the DateArchiver module. I was able to get a list of the years and months similar to the categories list but was not able to get the year/month pages to display the articles. Started a new topic here:http://processwire.com/talk/topic/3099-need-some-help-with-making-a-news-section-with-archives/

Link to comment
Share on other sites

Also, in the tuto you have a step to create the field article_thumbnails, but in the code it is called article_newsimage.

And you have listingthumb while it should be listingimage:

$out .="<img class='align_left' src='{$newspost->article_newsimage->getThumb(listingthumb)}'>"; 
Link to comment
Share on other sites

  • 3 weeks later...

Hi,

thank you so much Joss, doing your tutorial answered so much of my questions. 

This step by step was more than useful.

I'm trying to push it a step further by sorting the articles with the default field createdUser.

so I'm hijacking your category example but I doing something wrong

here is the authors functions i have so far

function newsList(){
 
	// Grab the page name from the url
 
	$thisAuteur = wire("page")->name;
 
	// If the category is not called "news" then output the category name as a selector for the find.
 
	if($thisAuteur !="jetag") {
		$auteur = "createdUser.title=" . $thisAuteur;
	}	
 
	// Get the news posts - limited to ten for later pagination
 
	$newsposts = wire("pages")->find("parent=/news-articles/, $auteur, template=TUT_news, limit=10");
 
	$out =" ";
 
	//Loop through the pages
 
	foreach($newsposts as $newspost){
		$out .="<div class='clearfix'>";
		if($newspost->article_thumbnails){
			$out .="<a href='{$newspost->url}' class=''>";
			$out .="<img class='align_left' src='{$newspost->article_thumbnails->getThumb(listingthumb)}'>";
			
		}
		$out .="<h3>{$newspost->title}</h3>";
		$out .="<p>{$newspost->article_introtext}</p>";
		$out .="</a></div>";
 
	}
	// Pagination
 
	$out .="<div class='pagination'>";
	$out .= $newsposts->renderPager(array(
 
				    'nextItemLabel' => "Next",
				    'previousItemLabel' => "Prev",
				    'listMarkup' => "<ul>{out}</ul>",
				    'itemMarkup' => "<li>{out}</li>",
				    'linkMarkup' => "<a href='{url}'>{out}</a>"   
 
					));
	$out .="</div>";
 
	echo $out;
 
}

/* CATEGORIES LIST
 
This lists available categories
 
*/
 
function auteursList(){
	$auteurs = wire("pages")->find("parent=/admin/access/users/, template=TUT_news_index, sort=title");
	$out =" ";
	foreach($auteurs as $auteur){
		$out .="<li><a href='{$auteur->url}'>{$auteur->title}</a></li>";
	}
 
	echo "<ul>$out</ul>";
 
}

Does the createdUsers field works a different way from the other fields or am I just forgetting something in the code?

thank you very much for sharing all this 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...