Choice of Featured Blog Posts On Home Page

Hey guys,

I currently have my website setup nicely. Its setup like this.

Home.php
About.php
Blog.php
   - Blog Post 1
   - Blog Post 2
   - Blog Post 3
   - Blog Post 4

etc…

At the moment, I have my homepage displaying and linking the first 3 blog posts using the limit function.

What I would like to do is give the user the ability to choose which 3 blog posts she wants featured on the homepage.

For this, I assume I’ll need to use the selector plugin(?) and the checkbox field.

I am a little unsure how to setup the blueprints first of all for this.

At the minute my blog parent page has no special fields just the default blueprint with title.

Each of my blog sub pages have these blueprints below:

title: Blog Article
pages: true
files:
  sortable: true
fields:
  name:
    label: Title
    type: textarea
    size: large
  blogdate:
    label: Date
    type:  text
    width: 1/4
    help: eg. January 2016
  category:
    label: Category
    type: select
    width: 1/4
    options:
      treats: Treats
      thoughts: Thoughts
      jobfairy: Job Fairy
  text:
    label: Text
    type:  textarea
    size:  large
  blogimage: 
    label: Featured Image
    type: structure
    entry: >
      <img src="{{image}}" />
    fields: 
      image:
        label: Image
        type: select
        options: query
        query:
          fetch: files
          value: '{{url}}'
          text: '{{filename}}'

Do i setup the checkbox on the blog article pages or wouldn’t it make more sense to have the choice on the parent blog page panel?

Any help with this would be amazing, thank you so much.

I think it would be easier to use a field on the parent blog page. I would probably use the multiselect field.

1 Like

I went ahead and installed the plugin.

I cant even load my site now without getting an error on line 4 of the field-multiselect.php.

Notice: Undefined variable: kirby in C:\Users\#\Desktop\work\sites\#\app\site\plugins\field-multiselect\field-multiselect.php on line 4

All i did was follow the instructions exactly, copy the zip into the site/plugins folder.

Fixed : I renamed field-multiselect folder to just multiselect and it seems to have worked. Strange(?)

That’s indeed strange, because the folder should have the same name as the file, i.e. field-multiselect. I just did a quick test and for me it works out of the box.

Are you using the latest Kirby version, 2.3.0?

Yeah, I just made sure of it. Im using 2.3.

Its very strange.

My renaming did nothing, the field wasn’t recognized then. When i switched it back, the errors came back. Seems like nobody else is having this issue either.

So the error appears on the front-end when the site is loaded, not in the Panel, right?

So I finally came back to this problem and the error was fixed by updating kirby.

I now have the multi select plugin working nicely, and ive my blueprint setup like this:

title: Blog
pages:
  template: entry
files: true
fields:
  title:
    label: Title
    type:  text
  blogposts:
  	label: Featured Blog Posts
  	type: multiselect
  	required: true
  	search: true
  	options: children

This is picking up all the child pages in the panel and allowing me to check the ones I want.

My question now is how to I output these posts into my template?

My template code is currently setup like this

<section class="blog full">
	<div class="three">	
			<?php $childPages = page('blog')->children()->flip()->limit(3);
	 	foreach($childPages as $p): ?>

			<div>
				<?php foreach ($p->blogimage()->toStructure() as $item): ?>
				<a href="/blog">
					<img src="<?php echo $item->image()?>">
					<div class="info">
						<span class="category"><?php echo $p->category()?></span>
						<h4><?php echo $p->name()->kirbytext()?></h4>
					</div>
				</a>
				<?php endforeach; ?>	
			</div>
		
		<?php endforeach ?>
	</div>
</section>

This is pulling the latest 3 blog child posts and putting them on the site.
Im a little unsure how to modify this code to get the 3 posts i’ve selected to show up instead.

Any help would be amazing, thank you

I’m not quite sure, what is saved to the content file, hopefully the uid … Try this:

<?php
  // create array from the pages selected in the blogposts field
  $childPages = page('blog')->blogposts()->split();
  // pass the array to the find method
  foreach(page('blog')->find($childPages) as $p): ?>
  // the rest of your code
<?php endforeach ?>
1 Like

If it helps, here is whats being saved to the content file

Title: Blog

----

Text: 

----

Blogposts: blog-one, blog-two, blog-three

Those 3 posts ive selected from the panel.

Ah, ok, yes, the uids as a comma separated list, great. Then the above code should actually work. The find method accepts an array of uris since Kirby 2.3.0

1 Like

That’s good to know !

Thank you very much Tex. Working perfectly.

Hey again, Sorry for being so difficult :smiley:

Because of the presentation aspect of the multi select field inside the panel, I have decided to use checkboxes instead of the multi select plugin. The checkboxes are much friendlier to view and check off your desired posts etc.

So now my blueprint is like this:

<?php if(!defined('KIRBY')) exit ?>

title: Blog
pages:
  template: entry
files: true
fields:
  title:
    label: Title
    type:  text
  blogposts:
   label: Featured Blog Posts
   type: checkboxes
   options: query
   help: Select the 3 blog posts here you want featured on the homepage
   query:
     page: blog
     fetch: children
     value: '{{uri}}'
     text: '{{title}}'

But now its not saving the uids in the content file like before, its saving them like this:

Title: Blog

----

Text: 

----

Blogposts: blog/blog-one, blog/blog-two, blog/blog-three

And the code which was working before now just doesnt output anything. Completely blank.

Sorry to keep pestering you, but is there a special way to output the selected checkboxes?

This was the code you posted above, working when integrated into my template when we were still using the multi select field. I see the new bool value should be used here since we changed to checkboxes(?). Thank you for all your time.

 	<section class="blog full">
		<div class="three">	
		  
		  <?php $childPages = page('blog')->blogposts()->split(); 
			
  			foreach(page('blog')->find($childPages) as $p): ?> 

  				<div>
					<?php foreach ($p->blogimage()->toStructure() as $item): ?>
					<a href="<?php echo $p->url(); ?>">
						<img src="<?php echo $item->image()?>">
						<div class="info">
							<span class="category"><?php echo $p->category()?></span>
							<h4><?php echo $p->name()->kirbytext()?></h4>
						</div>
					</a>
					<?php endforeach; ?>	
				</div>
			<?php endforeach ?>
		</div>
</section>

Try

<?php foreach(pages()->find($childPages) as $p): ?> 

or

<?php $site->index()->find($childPages) as $p): ?>
1 Like

Thank you, this worked for me

foreach(site()->find($childPages) as $p): ?>