Jump to content

Need some help with making a news section with archives


santaji
 Share

Recommended Posts

I want to have a basic news system with the ability to filter posts by year and month to use on my websites.

I used the code from this tutorial by Joss to set up the basic news system, than i installed and set up the DateArchiver module by u-nikos.

I don't know php but i managed to reuse some of the code from the news_functions file from Joss's tutorial and got it to display the year and  months:

function archivesList(){
	$years = wire("pages")->find("parent=/news-articles/, template=archives_index, sort=title");
	$yearout =" ";
	foreach($years as $year){
		$yearout .="<li><a href='{$year->url}'>{$year->title}</a></li>";
	}
 
	echo "<ul>$yearout</ul>";
	
	$pageName = wire("page")->name;
	
	if($pageName !="news") {
	$months = wire("pages")->find("parent=/news-articles/$pageName, template=archives_index, sort=title");
	$monthout =" ";
	foreach($months as $month){
		$monthout .="<li><a href='{$month->url}'>{$month->title}</a></li>";
		}
	}	
	
	echo "<ul>$monthout</ul>";
	
 
}

Made an archives_index.php template for the year/month pages:

<?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 Archives List
 
		archivesList();
 
// Render the News List
 
		ArchivesNewsList();

include("./footer.inc");

But i can't figure out how to make the newsList() function from the tutorial filter the articles on the year and month pages.

Link to comment
Share on other sites

Anybody have any ideas?

This is the code i'm trying to get to display the posts in the archives pages:

Basically it's function newsList() from Joss's tutorial, but i've changed has_parent=/news-articles to has_parent=$thisCategory 

Again, i don't know php, but the variable $thisCategory in the original code seems to be just the name of the current page? So shouldn't ("pages")->find("has_parent=$thisCategory get all the posts that are children of the current page?

function ArchivesNewsList(){
 
	// 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("has_parent=$thisCategory, template=news, sort=-date_time, 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;
 
}

I tried removing template=news just to see what happens and this happened: YXWQIKT.png

Why is it displaying the options from the admin interface!?

If i keep template=news in the wire("pages")->find selector nothing is displayed at all.

So basically what i'm trying to do is getting the ArchivesNewsList() function to display all the article pages are are children of the current page, so if i'm on /2013 it should display all articles which are children of that page, /2013/03 should display articles that are children of /03, etc..

Link to comment
Share on other sites

I'm not sure I understand all the code examples or what the structure is underneath well enough to say what's wrong. The logic here is difficult to follow. :) But I see a few things I would change with the code:

This block of code has problems with the $thisCategory variable: 

// 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("has_parent=$thisCategory, template=news, sort=-date_time, limit=10"); 
 

Note that the $category variable above never got used–was it supposed to go somewhere? I'm making the assumption that was supposed to become $thisCategory, but don't know for sure.

Double check that your "has_parent" shouldn't really be a "parent" instead? I can't say without knowing the structure, but the use of has_parent seems unusual here to me. 

Also, I don't think there's any reason to have $thisCategory be the page name. You should just keep that as the page object instead. This becomes a problem in particular where you are using it in that last find() because find("has_parent=$thisCategory") is resolving to something like "has_parent=some-page-name", and that's invalid syntax because has_parent expects a page ID, object or path. So I would change it to this:

// Grab the page name from the url
$thisCategory = wire("page"); 

// If the category is not called "news" then output the category name as a selector for the find.
if($thisCategory->name != "news") $selector = "article_category=$thisCategory";
  else $selector = "has_parent=$thisCategory"; // or maybe: parent=$thisCategory ?

// Get the news posts - limited to ten for later pagination
$newsposts = wire("pages")->find("$selector, template=news, sort=-date_time, limit=10");

I would also recommend passing in $thisCategory as an argument to the archivesNewsList() function, rather than pulling it from wire('page'); just because pulling it from wire('page'); means that the archivesNewsList() function can only be used on category pages, and maybe you'll want to use it somewhere else?

Next up is an observation or a question. But if this is valid PHP syntax, I didn't know it:

$out .="<img class='align_left' src='{$newspost->article_newsimage->getThumb(listingthumb)}'

Does that really work? If so, I guess I didn't realize you could make a function call like that from within a string. So if that really works, then I guess I've learned something new. But if it doesn't, I'd suggest  changing it to this:

$out .="<img class='align_left' src='" . $newspost->article_newsimage->getThumb("listingthumb") . "'>";

You really don't need to redefine all the markup for the pager unless you want to. This is unnecessary extra code since it's already outputting an unordered list with these same labels:

$out .= $newsposts->renderPager(array(
  'nextItemLabel' => "Next",
  'previousItemLabel' => "Prev",
  'listMarkup' => "<ul>{out}</ul>",
  'itemMarkup' => "<li>{out}</li>",
  'linkMarkup' => "<a href='{url}'>{out}</a>"   
  ));

You can just do this:

$out .= $newsposts->renderPager(); 

Last thing to mention is that there's no point concatenating everything into an $out variable when you are just echoing it at the end of the function. The purpose of stuffing into a $out variable is so that the function can return the value, to be output by the caller (which is what I usually do). So I would suggest making your function return $out; rather than echo it, and making the function call: echo archivesNewsList($categoryPage). If you'd rather have the function echo it, then don't bother with $out, just echo the strings as you go. 

Link to comment
Share on other sites

\// Grab the page name from the url
$thisCategory = wire("page"); 

// If the category is not called "news" then output the category name as a selector for the find.
if($thisCategory->name != "news") $selector = "article_category=$thisCategory";
  else $selector = "has_parent=$thisCategory"; // or maybe: parent=$thisCategory ?

// Get the news posts - limited to ten for later pagination
$newsposts = wire("pages")->find("$selector, template=news, sort=-date_time, limit=10");

I tried this code, doesn't work either.

The original code was not written by me so i'm not sure how most of it works. Like i said, it's from this tutorial: http://wiki.processwire.com/index.php/Simple_News_System

I will be trying to write my own code from scratch now.

What i want to do is really simple, I have a page called "News Articles", and have set up the DateArchiver module to organize any pages created under it by year and month. So the structure is like this:

- News Articles

   - 2013

      - 01

         - Articles made in January 2013

      - 02

         - Articles made in February 2013

So when on the 2013 page, the ArchivesNewsList function should display all articles that have 2013 as a parent, so all articles from 01 and 02.

When on the 01 page for example, it should display all articles that have 01 as a parent, so only articles from that month.

Link to comment
Share on other sites

What's wrong with this code? 

function newsListTest() {
	
	//grab the page name
	
	$thisPage = wire("page")->path;
	
	//get the news posts
	
	$newsposts = wire("pages")->find("has_parent=$thisPage, template=news, sort=-date_time, limit = 10");
	
		//Loop through the posts 
		
		foreach($newsposts as $newspost){
			$out .="<article class='newsarticle'>";
			$out .="<a href='{$newspost->url}'><h3>{$newspost->title}</h3></a>";
			$out .="<p>{$newspost->article_introtext}</p>";
			$out .="</article>";
			}
}

Getting these errors:

iEU84ae.png?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

  • Recently Browsing   0 members

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