Jump to content

Displaying the most recent blog posts using different layouts for each


mike62
 Share

Recommended Posts

Here are two ways:

<?php namespace ProcessWire;
/** @var $pages Pages */
$posts = $pages->find('template=post, sort=-published');
$latest = $posts->shift(); // removes first item off of $posts

?>
<ul class="post-list">
    <li class="item--latest item">
        <?= $latest->title ?>
    </li>
    <?php if ($posts->count): ?>
        <?php foreach ($posts as $i => $p): ?>
            <?php $itemClass = ($i % 3 == 0) ? "item--hoverable" : ''; ?>
            <li class="<?= $itemClass ?> item"><?= $p->title ?></li>
        <?php endforeach; ?>
    <?php endif; ?>
</ul>

<!-- OR -->
<?php if ($posts->count): ?>

    <ul class="post-list">
        <?php foreach ($posts as $i => $post): ?>
            <?php if ($i === 0): // LATEST POST ?>
                <li class="item--latest item">
                    Latest: <?= $post->title ?>
                </li>
            <?php elseif ($i === 1): // BASIC ?>
                <li class="item--basic item">
                    Basic: <?= $post->title ?>
                </li>
            <?php elseif ($i % 3 === 0): // HOVERABLE  ?>
                <li class="item--hoverable item">
                    Hover Me: <?= $post->title ?>
                </li>
            <?php endif; ?>
        <?php endforeach; ?>
    </ul>
<?php else: ?>
    <p>No posts found</p>
<?php endif; ?>

 

Edited by abdus
changed semicolon to colon
  • Like 2
Link to comment
Share on other sites

Optionally, you can create a select field, or better, a page field and select flavors for each post while editing and render items depending on that

https://processwire.com/blog/posts/making-efficient-use-of-fields-in-processwire/#use-page-fields-rather-than-individual-checkboxes

if($item->flavors->get("name=hoverable")) {
    // this page has the "hoverable" flavor set
}
  • Like 2
Link to comment
Share on other sites

  • 1 month later...
2 minutes ago, mike62 said:

@kongondo Thanks! I literally JUST found both those in the docs. Some things are hard for me to find in these docs. Is there a way to search *just* the docs, on the site?

https://processwire.com/talk/topic/6196-easy-search-on-pw-forums-with-google/?tab=comments#comment-60632

https://processwire.com/talk/topic/6196-easy-search-on-pw-forums-with-google/?do=findComment&comment=153343

 

  • Like 1
Link to comment
Share on other sites

3 minutes ago, mike62 said:

*just* the docs, on the site?

I bookmarked these and update the search word after clicking them and press enter. I know it's crude but it is the fastest way for me :-[

Link to comment
Share on other sites

Thanks for the tip @Robin S I cannot se how it can be faster than my dead simple solution. Sure, I perform an otherwise useless search but Google is sooo fast ;)

With the "keyword search feature", I need to be able to remember the keyword(s). Well, I already use a lot of them in other contexts so I find it hard to memorize more. Next, I need to type the get there. One click in the Bookmarks Bar and another double click to highlight the dummy search word is what I can do quickly without mistakes. Yeah, it's lame but it works for me.

Link to comment
Share on other sites

12 minutes ago, mike62 said:

Cool, thanks @SamC that's a good one to know. I ended up using the page field as @abdus suggested, so that each post can "choose" its own layout for the homepage. It's working well. o/

Good to hear! That's a nice technique, will certainly be looking into that one.

Link to comment
Share on other sites

  • 1 year later...

Hey @SamC  I was looking for a way to style some results of a selector differently than others and found your 'slicing' suggestion fitting perfectly to my scenario. Here is a short example of what I have and would allow any results from to be styled one way and the rest - another:

<?php 			
	// Check if there are any news added
	if (count($news->children)) {
		
		// Create am array with the latest 5 news
		$newsposts = $pages->find("template=news-inner, status=published, sort=-date, limit=6");
		
		// Create a slice of 2 posts for the large image style
		// 0 means the starting record, 2 means the number of records to slice
		$left = $newsposts->slice(0,2);
		
		// Grab the rest of the posts from the list for styling with just a title and a date
		// 2 means that everything except the first two would be assigned.
		// The results limit is set initially at the selector
		$right = $newsposts->slice(2);
		
	}
?>

Having done that, it allowed me to simply loop through the $left and $right results (using foreach) and apply the proper markup for first appearance and the rest. Using this elegant technique would allow to have as many slices for different styling if a need be.

Unfortunately, my markup for the first news was in a row div that was closing before the rest of the posts appear in another row so I had to loop twice with foreach. I am sure there is a better way to include the opening and closing divs to avoid double/tripple etc. looping but as far as the result are not more than 6, I am not that worried about slowing the page down...

Link to comment
Share on other sites

3 hours ago, MilenKo said:

I was looking for a way to style some results of a selector differently than others

If it's just styling, you should first try a CSS-only solution. You can do a lot with just :nth-child or flexbox.

  • Like 1
Link to comment
Share on other sites

Thanks @dragan will try your approach to see how would that fit, however it is not just styling but the markup is different where the first 2 news are required to have a large image with some intro text, author, date, likes etc. where the next 4 are just title and date of posting.

That is why initially I thought to "slice" the array but it did not work properly so with $a->slice it did the trick. I will see how to serve the posts with a single foreach to avoid duplication, however the classes of divs are different, some javas are included in the no-image divs and some data handling. Not sure if I add a variable for the class, one for the javas and one for data- values to include in the div which one would be faster since I am looping once through 2 results and once through 4 others.

Maybe I could try using modulus operator or other approach as well ?

Link to comment
Share on other sites

Well I initially tried with the good old counter approach but things got messed up a bit because of the differences in the markup of large image post and others. Will revise the code and see how to include the opening and closing divs properly. Here is the markup I am working with:

Spoiler

<div class="row">

	<!-- Large image post x 2 -->
	<div class="news-block col-lg-4 col-md-6 col-sm-12 wow fadeInUp animated" style="visibility: visible; animation-name: fadeInUp;">
		<div class="inner-box">
			<div class="image-box">
				<figure class="image"><a href="#"><img src="images/resource/news-1.jpg" alt=""></a></figure>
			</div>
			<div class="lower-content">
				<div class="date">26 Aug. 2018</div>
				<h5><a href="blog-detail.html">Mac Keychain Bug Could Be Big Problem For Apple</a></h5>
				<div class="text">Fed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt ...</div>
				<div class="link-box"><a href="blog-detail.html">Read more</a></div>
			</div>
		</div>
	</div>


	<!-- News Block -->
	<div class="news-list col-lg-4 col-md-12 col-sm-12 wow fadeInUp animated" data-wow-delay="800ms" style="visibility: visible; animation-delay: 800ms; animation-name: fadeInUp;">
		<div class="inner-box">
		
			<!-- List Item x 4 -->
			<div class="list-item">
				<h5><a href="blog-detail.html">Nanotechnology immersion along the information.</a></h5>
				<span class="date">Aug 26 2018</span>
			</div>

		</div>
	</div>
</div>

 

Where Large image post shows twice and List Item show 4 times ?

Link to comment
Share on other sites

9 minutes ago, MilenKo said:

<div class="news-block col-lg-4 col-md-6 col-sm-12 wow fadeInUp animated"

<div class="news-list col-lg-4 col-md-12 col-sm-12 wow fadeInUp animated" data-wow-delay="800ms"

I like the developer satisfacion coming through these lines ? 

  • Like 1
  • Haha 1
Link to comment
Share on other sites

@tpr Tell me about it ? Just because of the div opening markup I was crushing my head for some time before I "split" the selector in two and then applied the every foreach inside of every div ? This could all goe to the CSS but hey - different people, different taste ?

Link to comment
Share on other sites

Guess you could be clever with modulus and some dynamic class names etc. Or, just go for two loops and call it a day. I did something before and tried to be uber clever about it, turns out it's not so clever when you go back and have to waste time working out how it works.

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

×
×
  • Create New...