Jump to content

How to create a simple news system for PW


Joss

Recommended Posts

This is a bit more complicated. In the Simple News System, the "category" pages have the TUT_news_index template to display the news articles. That means that the script just needs to look at the "name" of the page to see which category to display - the name being the category itself.

With authors, you need to retrieve the information in a different way.

The most obvious way is to create a simple form that adds the user "name" to the URL which you can then retrieve.

To get a list of users you can use

foreach ($users as $u) {
    echo $u->id;
}

So, you could create a basic form an use the above code to populate a select drop down.

The form action would need to be a specific listing page, probably using variation of TUT_news_index as the template. If the page were called news-author, the form field would be something like:

<form id='search_form' action='<?php echo $config->urls->root?>news-author/' method='get'>  

<select name="author">
<?php
foreach ($users as $u) {
    echo "<option value='{$u->id}'>{$u->name}</option>";
}
?>
</select>
</form>
 

Look at the search template that comes with the default ProcessWire to see how this works.

On your template for your news-author page, you can then retrieve the value using

$author = $input->get->author

You can now add that to your find statement

$newsposts = $pages->find("parent=/news-articles/, created_user_id= $author, limit=10");  (Obvbiously, use wire("pages") if it is part of a function)

After that, carry on the same.

I am nowhere near anything useful like a server, so this is completely untested, but look through the search template file that comes with the default PW installation and that will give you lots of info.

Link to comment
Share on other sites

Thank you Joss you're more a programmer than you admit ;-)

I have been playing with the search template but I can't figure out the selector to make it look at created_users_id 

seems to work for or the fields but not this one ?

$matches = $pages->find("title|article_introtext~=$q, limit=50"); 
Link to comment
Share on other sites

I think in your selector you put 

$matches = $pages->find("created_user_id=$author, limit=50"); 

That will be where $author is the user id you have picked up from wherever. It will then return all pages that are associated with that user. So you need to add the user id to the search.

Obviously, the existing search template is looking at other search criteria so you will probably find it easier to start from scratch using clues from that template rather than try and modify it.

Worth noting that there is also a modified_user_id field if you want to pick up the last person to edit the page. So if you wanted to search for both:

$matches = $pages->find("title|article_introtext~=$q, created_users_id|modified_users_id=$author, limit=50");

Sorry, this would be easier if I was in a position to actually try this, but I am stuck in the studio this week. 

Link to comment
Share on other sites

Don't be sorry you 've been a great help

I finally been playing with the Authors template from the blog profile of Ryan

I'm not sure what i did but it works  ^-^

i'm really discovering programming so I take it step by step

again thanks for your tips

  • Like 1
Link to comment
Share on other sites

  • 5 weeks later...
  • 2 weeks later...

OrganizedFellow, did you figure out your problem with the Category list? I realize it's been a few days.

It may be that you didn't select a "Parent of Selectable Pages" for your field. It's under Input when you edit your field.

Link to comment
Share on other sites

OrganizedFellow, did you figure out your problem with the Category list? I realize it's been a few days.

It may be that you didn't select a "Parent of Selectable Pages" for your field. It's under Input when you edit your field.

No sir I have not.

I assumed I had missed a critical step in the tutorial. I followed the steps three times, but came upon the error each time.

I have since given up and I moved on to breaking down the Default Site Profile in a new installation and replacing the styles with a new CSS framework I have for responsive design. So far so good.

I will return to this tutorial/project later this week.

Thanks for checking in on me thistimj.

Link to comment
Share on other sites

Joss, thank you for this inspiring tutorial.

Everything works like a charm I only get this error (clicking on the categories list):

Error:     Exception: Field does not exist: category.name (in
/home/marcoang/public_html/processwire/wire/core/PageFinder.php line
248)


#0 /home/marcoang/public_html/processwire/wire/core/Wire.php(271): PageFinder->___getQuery(Object(Selectors))

#1 /home/marcoang/public_html/processwire/wire/core/Wire.php(229): Wire->runHooks(Array, Array)

#2 /home/marcoang/public_html/processwire/wire/core/PageFinder.php(145): Wire->__call('getQuery', Array)

#3 /home/marcoang/public_html/processwire/wire/core/PageFinder.php(145): PageFinder->getQuery('getQuery', Array)

#4 /home/marcoang/public_html/processwire/wire/core/Pages.php(144): PageFinder->find(Object(Selectors))

#5 /home/marcoang/public_html/processwire/wire/core/Wire.php(271): Pages->___find(Object(Selectors), Array)

#6 /home/marcoang/public_html/processwire/wire/core/Wire.php(229): Wire->runHooks('parent=/news-ar...')

#7 /home/marcoang/public_html/processwire/site/templates/newsfunctions.inc(178): Wire->__call(Array, Array)

#8 /home/marcoang/public_html/processwire/site/tem

What am I doing wrong?

Link to comment
Share on other sites

Sorry, I have been out of the loop for a bit.

To be honest, I really am not sure at all.

The first thing to check, however, is what you have called your category field, since it seems to be complaining that it can't find that field. It should be called article_category, if I remember right.

Link to comment
Share on other sites

Hey, wait a minute...It works now!!!!

----------------------------------

------ RECAP ------------------

----------------------------------

The wiki states:

You will need to create the following fields (some are already created if you have done the Basic Website Tutorial):


  • Title - this field already exists.
  • article_introtext - textarea field
  • article_maintext - textarea/tinymce field
  • article_thumbnails - a Crop Image field (see below)
  • article_category - a Page field (see below)

...while the REAL custom fields used in newsfunction.inc are:

Fields Used:
------------

Title - both for the title of the news article and the title of the news index page
Body - for Main content
Summary - to be used on the index page
Headline - for an article headline at the begining of the article
news_image - for a main image that is used at the head of the article and on the news index page.
(Note: Set this image for ONE image only!)
category - a page field for selecting categories.

...so be careful the custom fields you create with processwire match those appearing in newsfunction.inc

Joss, thanks again for taking the time to write this tutorial.

Link to comment
Share on other sites

I have just added a note to my original post to not try and use a mix of the github version and the wiki version - use one or the other.

For learning purposes it is probably best to use the wiki version as that explains more of the how and why :)

Link to comment
Share on other sites

  • 3 weeks later...

I've implemented the news system to create a blog for my company's website: http://www/answerhub.com/blog and it works great!

The only issue I'm having is with a separate piece of php I wrote to list the 5 most recent blog posts in the sidebar. On the blog homepage it works fine, but when you go to page 2 of the blog, the list does not show the most recent blog posts, but the most recent blog posts from that page. Here's the php:

 <?php foreach ($pages->get("/article")->children('sort=-created, limit=5') as $articles) { ?>
        <li class="nav-header" style="text-transform: none; font-size: 13px;"><a class="primary-color" href="<?php print $articles->httpUrl ?>"><?php print $articles->title?></a></li>
<?php } ?>

Any idea why this would be happening?

Link to comment
Share on other sites

Sounds like something is getting paginated that you don't want paginated. Add a "start=0" to your children() selector, i.e. 

<?php foreach ($pages->get("/article")->children('sort=-created, start=0, limit=5') as $articles) { ?>

See the "side effects" section in the pagination documentation

  • Like 1
Link to comment
Share on other sites

Sounds like something is getting paginated that you don't want paginated. Add a "start=0" to your children() selector, i.e. 

<?php foreach ($pages->get("/article")->children('sort=-created, start=0, limit=5') as $articles) { ?>

This worked perfectly and the documentation is a big help. Thanks Ryan!

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

hallo Joss very big help your tutorials!! and hallo forum!!

i try to make a real estate site folow your guide is allmost what i need to do, but i need to add one more category for cities that property belong.

can you please guide me how to add one more category, i dont know what to do.

thank you

Link to comment
Share on other sites

  • 1 month later...

Those are the ID of the pages. My guess is that you chose a field that doesn't exist on the selectable pages as the pages identifier for the dropdown. Go to that field settings, and under the input tab change the "Label field" setting.

  • Like 2
Link to comment
Share on other sites

  • 4 months later...

Hello

I am playing around with this tutorial, trying to build some sort of event list. Thanks to Joss that works fairly well.

I am stuck though, trying to display only events, that lie in the future. Old ones should not be displayed in the list (without deleting or unpublishing the corresponding page). All event-pages have a date field. So I tryed to achieve that like so:

	foreach($newsposts as $newspost){
		$t = time();

		if($t<$newspost->Datum) {
		
			$out .="<hr><div class='clearfix'>";
			if($newspost->Artikelbild){
				$out .="<a href='{$newspost->Artikelbild->url}' class=''>";
				$out .="<img class='align_left' src='{$newspost->Artikelbild->url}'>";
				$out .="</a>";
			}
			$out .="<p>{$newspost->Datum}</p>";
			$out .="<a href='{$newspost->url}'><h3>{$newspost->title}</h3></a>";
			$out .= $newspost->body;
			$out .="</div>";
 
		}
	}

But that doesn’t display anything.

As you can see, I am by no means a pro with php, so help is very much apreciated.

Link to comment
Share on other sites

joe_ma,

I think the cleanest option would be to limit posts by date in your initial selector. That saves the resources of getting all the posts and foreach'ing through all of them.

Something like this should work for you.

$newsposts = wire("pages")->find("parent=/news-articles/, $category, template=TUT_news, Datum>time(), limit=10");
    
foreach($newsposts as $newspost){
    $out .="<hr><div class='clearfix'>";
    if($newspost->Artikelbild){
        $out .="<a href='{$newspost->Artikelbild->url}' class=''>";
        $out .="<img class='align_left' src='{$newspost->Artikelbild->url}'>";
        $out .="</a>";
    }
    $out .="<p>{$newspost->Datum}</p>";
    $out .="<a href='{$newspost->url}'><h3>{$newspost->title}</h3></a>";
    $out .= $newspost->body;
    $out .="</div>";
}
 

I am not sure if that selector is exactly what you need - I grabbed most of it from the tutorial, just adding in the date check. Also, it will depend on how the date is stored. You might need to change time() to something like date("Y-m-d").

Let us know how you go.

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