Jump to content

Calling content from blog post onto blog homepage


Peter Knight
 Share

Recommended Posts

I have a working blog thanks to this excellent module by kongondo

http://modules.processwire.com/modules/process-blog/

Now that I'm tweaking it a bit, I am running into some noob problems. For example, my individual blog posts have 2 fields which I want to call onto the blogs main homepage.

The two fields are:

1. The first image used in an image field called blog_images

<!-- Display the first blog image --> 
$blog_images = $page->blog_images->first(); if($blog_images) echo "<img src='$blog_images->url'>";

2. The content of a textarea field called blog_summary

<!-- Display a summary of the blog --> 
echo $page->blog_summary;

Both of these fields have content and I can call that content onto the blog post page with the PHP above. But calling it on the Blog homepage give me nothing and no errors.

Within my Pages tree, the structure of my setup lookis like this

Blog

  Posts

     Post 1

     Post 2

     Post 3

     Post 4

Categories

Tags

Comments

Widgets

Authors

Archives

Any help much appreciated. Thanks :)

Link to comment
Share on other sites

Are you still trying to call $page->blog_summary; etc?

If you are on the homepage, you need to define the page of the post that you want to get the content from.

Perhaps this does what you need?

$lastPost = $page->children->last();
echo $lastPost->blog_summary;
Link to comment
Share on other sites

Content of $page is always the page you are currently on. In the case of visiting the home page, the fields blog_images and blog_summary do not exist, since its another template ("home", I guess).

But there's a solution: $pages, the entirety of your, well, pages. In this case you "just" have to filter by template blog-post:

$blogposts = $pages->find("template=blog-post"); 

and then:

foreach ($blogposts as $b) {
    echo "<img src={$b->blog_images->first()->url} />";
    echo "<p>{$b->blog_summary}</p>";
}
  • Like 2
Link to comment
Share on other sites

Thanks adrian and marcus. Appreciate these are very noob Qs.

The following code is working for me based on your suggestion.

Basically, I have two responsive columns (using Foundation) and the left one outputs the image while the right one outputs the summary.

           <!-- START Nested Column containing featureimage and post-summary -->            
            <div class="row">
                <div class="small-12 large-6 columns">
                <?php $blogposts = $pages->find("template=blog-post"); foreach ($blogposts as $b) {
                echo "<img src={$b->blog_images->first()->url} />";} ?>
                </div>
                
                <div class="small-12 large-6 columns">
                <?php $blogposts = $pages->find("template=blog-post"); foreach ($blogposts as $b) {
                echo "<p>{$b->blog_summary}</p>";} ?>
                </div>
            </div>
           <!-- END Nested Column containing featureimage and post-summary -->   

I'm slowly getting there. I can see how PW will gradually improve my PHP too and am slowly understanding some of this variables stuff. 

No idea why I agreed to put together an urgent client project on a tight deadline with a CMS I'm not familar with.

  • Like 1
Link to comment
Share on other sites

I'm slowly getting there. I can see how PW will gradually improve my PHP too and am slowly understanding some of this variables stuff. 

I experienced the same and had the very same $page vs. $pages situations just not long ago :)

By the way, my code example was a bit verbose for explaining reasons, so you could shorten it to...

foreach ($pages->find("template=blog-post") as $b) { ...

...as well. Same thing goes for the thumbnail example in your other topic. If you feeling save enough you can write it like so from now on - but the main thing is the understanding of its concept.

Link to comment
Share on other sites

Hmm. I think I see whats happening. Youre basically saying "for each page that uses blog-post as a template, output X".
 
I just realised my output isn't creating a new row for each blog post so I modified it to be as follows.

<!-- START Nested Column containing featureimage and post-summary --> <?php $blogposts = $pages->find("template=blog-post"); foreach ($pages->find("template=blog-post") as $b) {
 
echo "
  <div class='row'>
    <div class='small-12 large-6 columns'><img src={$b->blog_images->first()->url} /></div>
    <div class='small-12 large-6 columns'><p>{$b->title}</p><p>{$b->blog_summary}</p></div>
  </div>
";
} ?>

                <!-- END Nested Column containing featureimage and post-summary -->
  • Like 1
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...