Jump to content

Best way to list multiple news items on Home page?


PhotoWebMax
 Share

Recommended Posts

I have had a long absence from PW and seem to have forgotten how to do some things...

I have a site I am working on with a successful Home page and working template, etc. At the bottom of the this page I would like to have a two column block that shows News and Upcoming Events items. These are not nav links but rather blocks that show the actual title, date and text copy for each item.

I started with the following in my Home page template: 

<div id="lowerWidgets">
	<div class="widgetNews">
	<h3>News...</h3>
	<h4><?php echo $page->newsTitle; ?></h4>
	<h4><?php echo $page->newsDate; ?></h4>
	<p><?php echo $page->newsSummary; ?></p>
</div><!--widgetNews ends-->
...

I have the CSS and the Fields working nicely. 

What I want to do now is to be able to add more than one News item to the News block. I want this to be as simple and direct for the site owner as possible. What is the best way of doing this, being able to repeat entering the Fields for widgetNews? I have another site I am working on where I have something similar happening. What I did there is create child pages for each entry and then use a foreach statement in the template to aggregate the list of generated content blocks on one page. 

But before I go any further here I want to make sure that I am not missing a simple way of doing this that would be easy for a non techy site owner to manage?

Thanks

Max

Link to comment
Share on other sites

The most obvious thing is to use a foreach loop to loop through your news items.

Look up selectors in the API and create a $pages->find() select statement to select your news articles and then loop through them using a standard php foreach statement.

If you limit the select to, perhaps, the most recent 5 articles, then that will be shown automatically every time a new article is added.

There you go - I have been really mean and left all kinds of information out, but there should be enough there to get you started!

:)

Link to comment
Share on other sites

Well, you don't have to! But if they are effectively the same template, yep, that would be good.

You can either go the whole hog and create category pages that are then selected with a page field, or you can add a single check box that says "check this if this is an event"

Then, in your find statement, you can look to see if that has been checked or not, depending on what you are listing.

Link to comment
Share on other sites

My head is bleeding...

I removed what I had and created two hidden parent pages for the News and Events and added a custom template and the three fields to each. So far that seems good. The News and Events child pages do not appear in the main site navigation (intended) but I can view them live from the admin.

But I am a million miles from getting the code right in the section of my Home page where I want the content for the News and Events to appear? I thought I could use the $pages->find( ) selector to grab the two templates and then follow that with a foreach statement to get them to aggregate on the Home page.

I have messed with this for some time but I either get nothing to show or a parse error. 

This is where I am at currently (demonstrating my ignorant newbyness in all its glory):

<div id="lowerWidgets">
	<div id="widgetNews">
	<h3>News...</h3>
	<?php
	$pages->find("template=widgetNews")
	foreach ($page->children as $newsChild):?>
	<h4><?php=$newsChild->  $page->newsTitle; ?></h4>
	<h4><?php=$newsChild->   $page->newsDate; ?></h4>
	<p><?php=$newsChild->   $page->newsSummary; ?></p>
	
	?>
	
</div><!--widgetNews ends-->
...
Link to comment
Share on other sites

If I am understanding correctly, you have news entries using the template "widgetNews". I assume the parent News page uses a different template. If this is the case, you could do this:

$newsItems = $pages->find("template=widgetNews");
foreach($newsItems as $newsItem) { ?>
  <h4><?php echo $newsItem->newsTitle; ?></h4>
  <h4><?php echo $newsItem->newsDate; ?></h4>
  <p><?php echo $newsItem->newsSummary; ?></p>
  <?php
}
?>

I think that should work.

  • Like 1
Link to comment
Share on other sites

Hmmm,

Nothing shows up in the News block?

I have this: 

...

</div><!-- end sidebar -->

<div id="lowerWidgets">
	<div id="widgetNews">
	<h3>News...</h3>
	<?php
	$newsItems = $pages->find("template=widgetNews");
	foreach($newsItems as $newsItem) { ?>
	  <h4><?php echo $newsItem->newsTitle; ?></h4>
	  <h4><?php echo $newsItem->newsDate; ?></h4>
	  <p><?php echo $newsItem->newsSummary; ?></p>
	  <?php 
	}
	?>
	
</div><!--widgetNews ends-->
   <div id="widgetUpcomingEvents">
<h3>Upcoming Events...</h3>

</div><!--widgetUpcomingEvents ends-->
</div><!-- lowerWidgets ends -->
<?php include('./_foot.php'); // include footer markup ?>

Is that <?php near the end of the foreach statement supposed to be there? If I remove it I get a parse error in line 112? But the final line of my template is actually line 110. Line 112 is empty?

Thanks!

Link to comment
Share on other sites

If I remove it I get a parse error in line 112? But the final line of my template is actually line 110. Line 112 is empty?

PHP error line numbers will sometimes do this - if you having a missing "}" it will keep looking to the last line of the script and if it still doesn't find it, that will be the number that is associated with parse error. It may not seem very helpful, but in reality there isn't really a better line to refer to when something is missing :)

PS I am guessing that you have a couple of empty lines after 110 - hence the 112?

Link to comment
Share on other sites

Updated code in my Home page template:

...

</div><!-- end sidebar -->

<div id="lowerWidgets">
	<div id="widgetNews">
	<h3>News...</h3>
	<?php
	$newsItems = $pages->find("template=widgetNews");
	foreach($newsItems as $newsItem) { ?>
	  <h4><?php echo $newsItem->title; ?></h4>
	  <h4><?php echo $newsItem->newsDate; ?></h4>
	  <p><?php echo $newsItem->newsSummary; ?></p>
	  <?php 
	}

	?>
	
</div><!--widgetNews ends-->
   <div id="widgetUpcomingEvents">
<h3>Upcoming Events...</h3>

</div><!--widgetUpcomingEvents ends-->
</div><!-- lowerWidgets ends -->...

My output is this:

</div><!-- end sidebar -->

<div id="lowerWidgets">
	<div id="widgetNews">
	<h3>News...</h3>
		
</div><!--widgetNews ends-->
   <div id="widgetUpcomingEvents">
<h3>Upcoming Events...</h3>

</div><!--widgetUpcomingEvents ends-->
</div><!-- lowerWidgets ends -->

So, the <h4> and <p> content I am looking of is not being generated...

Edit**: here is my code for the widgetNews template for the News posts...

<div class="widgetNews">
<h4><?php echo $page->title; ?></h4>
<h4><?php echo $page->newsDate; ?></h4>
<p><?php echo $page->newsSummary; ?></p>
</div><!--widgetNews ends-->

OK, Now that I post this I see have a div ID and Class name with the same names! This might be the issue?

Edit**: I changed the ID to News from widgetNews but the hoped for content is still not showing. Feeling closer though?

Link to comment
Share on other sites

Whenever I run into something weird like this (which is often), I'll add in some sort of test to see if I am missing something.

Try pasting this code somewhere on your page where you will notice the word "yay" or "nope" appear:

<?php
if($pages->get("title=news")->child->id) {
	echo "yay";
} else {
	echo "nope";
}
?>

If your news page is called something else, change "title=news" to whatever your page is called. This will let us know that you have at least one child page under that parent page. If it says "yay", I have no idea what is going on, but if it says "nope", maybe your child pages are hidden or unpublished.

  • Like 1
Link to comment
Share on other sites

thistimj,

Thanks so much! I am getting closer. You are correct. My child pages under News were unpublished. I changed this. I have two entries. Only one displays right now, but this is a major step in the right direction. 

I have family over waiting for a late dinner. Will get back to this in the morning...

Cheers,

Max

Link to comment
Share on other sites

Back again...

Did some more work just now and everything clicks into place! I now have a two column section that pulls in News & Events pages from two hidden parent pages. Still have some CSS work to do but it works great. 

Also got a dynamic gallery system with pagination, jQuery eye-candy and custom CSS working. Real progress. 

Thanks to all!

Max

Next up: learn how to create a custom private page that only vetted site volunteers can access...

Link to comment
Share on other sites

Max,

Btw, don't know if you've heard about lightning.pw? https://processwire.com/talk/topic/7400-instant-processwire-dev-hosting-lightningpw/

I mention this just in case if you've tried something multiple times and it still doesn't work, you can always set up a test site there for some other person to have a look. Coupled with Nico's excellent 'Template File Editor' {saved me a couple of times when FTP was down}, one can even view your template files right within your PW Admin.

  • Like 3
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...