Jump to content

How to create a simple news system for PW


Joss

Recommended Posts

Thanks Adrian

Unfortunately that didn't change anything.

In my test I have four events. One of them is in the past, three of them in the future. When I use

Datum>time()

all of them are displayed. With

Datum<time()

none of them is displayed.

Link to comment
Share on other sites

Okay, this is not an area that I am great at....

but assuming your date field is called "date" I would think you need to do something like:

$todaysdate = time()

$posts = wire("pages")->find( blah blah .... date>$todaysdate .... ):
 
Link to comment
Share on other sites

Good grief - I got something right!

You might also find this useful:

http://processwire.com/talk/topic/4991-sub-arrays-dates-times-and-pure-hell/#entry48484

This was about taking the date, splitting it into years (looping it), splitting it into months, then days ....

The result can be seen here:

http://harlestonanglingclub.co.uk/fixture-list/

Link to comment
Share on other sites

It should work fine the other way, but if you are using the time() function you need to format like so:

$newsposts = wire("pages")->find('parent=/news-articles/, $category, template=TUT_news, Datum>'.time().', limit=10');

Note the single quotes and concatenation with periods.

Link to comment
Share on other sites

  • 2 months later...

Quick very newbie question:

On the TUT_news_index.php file we have this:

<?php 
include("./TUT_header.inc"); 
include("./TUT_news_functions.inc");
 
// Give the news index page a title an a bit of an intro
 
		echo "<h3>{$page->title}</h3>";
		echo $page->body;
 
// Render the Category List

		categoriesList();
		
 
// Render the News List
 
		newsList();
 
include("./TUT_footer.inc");

This works nicely. One thing though: what is the best way to add CSS to the categoriesList UL that is generated here? I would like to give this list a class or ID name so I can customize the list style etc. 

Thanks...

Link to comment
Share on other sites

categoriesList() is a function. Look at the code of this function...

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

...that should give you a hint.... ;)

Link to comment
Share on other sites

Thanks, I did get that:

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

Generally: what is the correct way to approach adding divs to blocks of PHP? For example: say I want to add a div wrapper around three instances of functions, etc. I have been doing a little reading on mixing html in with PHP, but I am still unsure of how to add markup and CSS divs without breaking the PHP logic...

Link to comment
Share on other sites

You can mix and match php and html as much as you like - the main rule to remember with both is that you must start and finish each thing logically. Make sure tags are closed in html or brackets close in php and so on.

So, for instance, if you want to put php within a div you can write:

<div>

<?php

echo "my bit of php";

?>

</div>

So, you have basically just inserted some php within the DIV.

You can mix and match php and html in two ways:

1. As I have done with a function, you can stick with php and put any html within quotes

2. You can open and close php tags as often as you need

When learning, it can sometimes be easier to use the second method, simply because the logic is more apparent. But once you work that out, the first method can be neater.

So, the code used within the function could instead be written directly into the template like:

<ul class="categoriesList">

<?php

$categories = wire("pages")->find("parent=/categories/, template=TUT_news_index, sort=title");

foreach($categories as $category){ 

?>

<li><a href="<?php echo $category->url; ?>"><?php echo $category->title; ?></a></li>

<?php } ?>

</ul>

It does the same job, but you can now see how it fits with the flow of the html. It is messier however, and the reason I used functions in this case was so all those were kept together and were reusable in any template.

  • Like 1
Link to comment
Share on other sites

Great post, thanks Joss!

Your tutorials and the photo gallery tutorial by Everfreecreative have been most useful. I have explored all three. My next stage is implementing the core functionality of these lessons into the couple of sites that I am working on: merging a static concept site structure with the PW API, a news section, multiple navigation lists, news section, gallery and custom CSS. Its slowly coming together. 

Your post on using functions (or not) helps clear some of the fog...

Link to comment
Share on other sites

  • 2 weeks later...

First post so I will start off by saying hello to everyone here - these forums look to have a lot of great reading.



I am an absolute rookie so I have to say a big thank you Joss for these tutorials they have been a huge help in getting familiar with processwire and I have been really enjoying moving some very simple static sites onto it locally. Very appreciative of the time you took to put those together.



One little exercise has me a bit stumped though. I've never touched php so I'm having a little trouble adapting the logic from the simple news site tutorial to solve the following problem.



I would like to try and make an extremely simple blog that displays all posts on the front page. No nav, categories or archive functionality, purely just to get my head around a couple of things.



From my limited understanding:



Home (with header/footer includes)


   A hidden page that is the parent of the posts pages


      Posts (with applicable posts template)


posts index page (displays my posts the front page via a function? or would said function sit inside home.php?)



Is this an approach that would work? I have been mashing away all day like a dullard and can't seem write a function that works. I have also installed the blog theme in another dir which looks incredible but is far too advanced for me to reference at this point. 



It would be awesome If somebody could point me in the right direction.



And last but not least Thank you Ryan for processwire! it is the first time I've felt enthusiastic about properly applying myself to learn a cms and its api. Once my understanding increases, I'm sure I'll be thanking you again.


  • Like 1
Link to comment
Share on other sites

Hi Dylan!

The point with a function is that it can be kept in a separate file that is included with your templates and then can be used anywhere.

So, to take a very simple example:

lets create a file for functions:

myfunctions.php

Then include it at the head of our template (either directly or via a separate header file as I used in the tutorial):

<?php include("myfunctions.php"); ?>

Generally speaking, since this function file could be used for anything, you probably want it as the very first thing right at the very top of your template structure - otherwise it won't be useful if it has functions to do with the <head> or something.

Okay, that was the first easy bit.

Now, create a function that gets posts, based on whatever page is their parent:

function myPosts($postnumb) {

$posts = wire("pages")->find("parent=/postparent/, limit=$postnumb);

}

Right, here we are using a variable called $postnumb which we will use later to set the limit of the number of posts we want. 

Then we are creating a variable called $posts which will contain the array of pages grabbed by the find action. Obviously, the postparent will be whatever page is the parent of all your posts! (That could be a hidden page, or a page without a template file or it could be a published and displayed page - it doesn't matter really. You could also select posts based on their template, if you wished, and then it wouldn't matter what the parent was.... anything is possible)

Now, we need to loop through them:

function myPosts($postnumb) {

$posts = wire("pages")->find("parent=/postparent/, limit=$postnumb);

    $out =""; // This just sets up the variable we will use for our result
    foreach($posts as $post){

       $out .="<h3>{$post->title}</h3>";

     } //end foreach

    return $out;
} // end function

There we go; i have just looped through the title, for the sake of an example.

Also, I am returning the value of $out at the end.

Now to add this to a page:

<?php echo myPosts(5); ?>

So, I am echoing the function (or more to the point, the value of $out) and I am saying that I want to limit the number of posts to 5.

When the function is called, the 5 in the brackets is passed to the function as the $postnumb variable and then is used as the limit in the find selector.

You can now reuse this function all over your site and change the number of posts each time.

Does that sort of guide you in the right direction while still leaving you a bit to work out?

Joss

  • Like 4
Link to comment
Share on other sites

That is just music to my eyes right now Joss. I thought it would be simple but yea I'm laughing when I look back at todays frustration, perhaps I should've asked sooner but I believe that's all part of it.



Thanks again for the super quick and clear response. Very happy. 


  • Like 1
Link to comment
Share on other sites

Quick question:

On the child post pages themselves how would you add a "return to News Articles" link back to the parent news page?

I can add a direct address link, but doing this the PW way is escaping me (I have tried all kinds of things but nothing works...)

Thanks!

Link to comment
Share on other sites

In this case the parent page that houses the posts is hidden. The public container page is also using a categories container page (also hidden).

As Joss set this up: the News site presents the articles, but the articles themselves are contained in a different hidden page.

I have tried several routes here. Should be simple? but...

Link to comment
Share on other sites

Sorry I didn't realize the articles weren't directly under the parent - that's how I have always done things.

I don't really see the problem in linking to the parent news page directly with something like:

$pages->get("/news/")->url;
Link to comment
Share on other sites

There really is no ProcessWire way to do this simply because what you are doing is wanting to link to a specific page.

That page could be anywhere, really. So it is perfectly legitimate to create a direct link.

You don't actually have to set up a news system the way I have - I picked a certain way of doing things to demonstrate a range of PW principles.

Link to comment
Share on other sites

I was trying something like this with a template called articles.php: getting a child post to link back to the parent presentation page called "articles"...

<?php 
include("./head.inc"); 
include("./articles_functions.inc");
 
// Output the newsDisplay function
 
newsDisplay();
 echo '<p>Return to: <a href="$pages->get("/articles/")->url">Articles</a></p>';
include("./sideBar.inc"); 
include("./foot.inc");

But any variation I tried either links to the same child page or throws a parse error...

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