Jump to content

Interrupting 'foreach' loop


Jon E
 Share

Recommended Posts

Hi,

I have looked around a few places but I couldn't find a solution here so wondered if someone could help me here.

I am making a listing page for blog items, but need to interrupt the listing after a few posts (to be decided how many) with a static div (that will pull in the most recent tweet).

Could someone help me with how to 'inject' this into my foreach loop?

Hope this makes sense and thank you.

<?php

foreach($page->children("template=blog-entry") as $album) {
    // let's grab the project thumbnail and resize it

    $image = $album->blog_thumbnail; // get the first image
    $thumb = $image->size(1000);

echo "<a href='{$album->url}'><div class="; 
echo"'grid__item cat";
echo $album->get("blog_category");
echo"' id=";
echo'"blog_listing';
echo'"';
echo">";
echo "<img src='{$thumb->url}' alt='{$album->title}' />";
echo"<div class='blog_listing_meta'>";
echo"<h2>";
echo $album->get("title");
echo"<br/>";
echo $album->get("date");
echo"</h2>";
echo"</div>";
echo'<div class="hide"><button class="action--buy"></button></div>';
echo"</div></a>";   
}
?>

 

Link to comment
Share on other sites

Hi,

Sorry thanks for helping and I might be being an idiot but if I do:

<?php

foreach($page->children("template=blog-entry") as $key => $post) {
     if($key == 3{
    // Render static content after 3 posts
        echo"<h2>static block</h2>";
  }
    // let's grab the project thumbnail and resize it

    $image = $album->blog_thumbnail; // get the first image
    $thumb = $image->size(1000);

echo "<a href='{$album->url}'><div class="; 
echo"'grid__item cat";
echo $post->get("blog_category");
echo"' id=";
echo'"blog_listing';
echo'"';
echo">";
echo "<img src='{$thumb->url}' alt='{$album->title}' />";
echo"<div class='blog_listing_meta'>";
echo"<h2><strong>";
echo $post->get("title");
echo"</strong><br/>";
echo $post->get("date");
echo"</h2>";
echo"</div>";
echo'<div class="hide"><button class="action--buy"></button></div>';
echo"</div></a>";   
}
?>

 

I get:

Parse Error: syntax error, unexpected '{' (line 25 of C:\MAMP\htdocs\dev\site\assets\cache\FileCompiler\site\templates\blog-listing.php)

This error message was shown because: you are logged in as a Superuser. Error has been logged.

 

Link to comment
Share on other sites

Hi,

Sorry to ask again, I have tried so much but I want to be able to post entries without a thumbnail. I know the 'if' statement is probably wrong below but what am I doing wrong?

Thank you

 

<?php

foreach($page->children("template=blog-entry") as $key => $post) {
     if($key == 2){
    // Render static content after 2 posts
        echo"<div class='grid__item cat1' id='blog_listing'><div id='tweet'></div><div class='clear'></div><div class='hide'><button class='action--buy'></button></div></div>";
  }
    // let's grab the project thumbnail and resize it

    $image = $post->blog_thumbnail; // get the first image
    $thumb = $image->size(1000);

echo "<a href='{$post->url}'><div class="; 
echo"'grid__item cat";
echo $post->get("blog_category");
echo"' id=";
echo'"blog_listing';
echo'"';
echo">";
if(count($post->blog_thumbnail)) {
echo "<img src='{$thumb->url}' alt='{$album->title}' />";
}
echo"<div class='blog_listing_meta'>";
echo"<h2><strong>";
echo $post->get("title");
echo"</strong><br/>";
echo $post->get("date");
echo"</h2>";
echo"</div>";
echo'<div class="hide"><button class="action--buy"></button></div>';
echo"</div></a>";   
}
?>

 

Link to comment
Share on other sites

11 minutes ago, Jon E said:

without a thumbnail

if you remove the two variables ($image and $thumb) + the if statement :

if(count($post->blog_thumbnail)) {
echo "<img src='{$thumb->url}' alt='{$album->title}' />";
}

you should get what you need - your post without thumbnail. Sorry if I misunderstood your question.

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...