Jump to content

Featured Blog Page


Edward Ver
 Share

Recommended Posts

Hello. I'm learning PW and I have a Blog page which shows blog entries and a featured blog using a field.

But if I use the $field featured_post=1, no page is showing. Using limit=1 works fine.

<?php 
$blogger = $pages->find("template=blog_entry,featured_post=1"); // my template
foreach($blogger as $bloggers) {
foreach($bloggers->blog_entries as $blog_entry) { // my repeater field
   
echo "<div><a href='{$blog_entry->url}'><h1 class='text-xl'>{$blog_entry->title}</a></h1>";
echo "<a href='{$blog_entry->url}'><img src='{$blog_entry->blog_main_image->getCrop('blogsmall')->url}' alt='{$blog_entry->blog_main_image->description}'></a>";
echo "{$sanitizer->truncate($blog_entry->blog_text , 100, array('type'=>'sentence', 'keepFormatTags'=>false /* true or false */))}";
echo "<p class='test_to_show_value_of_featured_filed'>{$blog_entry->featured_post}</p>";
echo "</div>";
}}
?>

Any help will be very appreciated. Thanks.

Link to comment
Share on other sites

Hello,

I'm glad you find the issue.

Instead of echoing long strings, why don't you simply mix HTML and PHP this way (written in the browser, so it might contain typos):

<?php namespace ProcessWire;
$blogger = $pages->find("template=blog_entry,featured_post=1"); // my template
?>
<?php if (wireCount($blogger)) : // Check if there are any bloggers ?>
    <?php foreach ($blogger as $bloggers) : ?>
        <?php if (wireCount($bloggers->blog_entries)) : // Check if there are any blog entries ?>
            <?php foreach ($bloggers->blog_entries as $blog_entry) : // my repeater field ?>
              <div>
                  <a href="<?= $blog_entry->url ?>">
                      <h1 class="text-xl"><?= htmlspecialchars($blog_entry->title) ?></h1>
                  </a>
                  <a href="<?= $blog_entry->url ?>">
                      <img src="<?= $blog_entry->blog_main_image->getCrop('blogsmall')->url ?>" alt="<?= $blog_entry->blog_main_image->description ?>">
                  </a>
                  <?= $sanitizer->truncate($blog_entry->blog_text, 100, ['type' => 'sentence', 'keepFormatTags' => false]) ?>
                  <p class="test_to_show_value_of_featured_filed"><?= $blog_entry->featured_post ?></p>
              </div>
			<?php endforeach; // End of blog_entries loop ?>
       <?php endif; // End of blog_entries check ?>
   <?php endforeach; // End of bloggers loop ?>
<?php endif; // End of bloggers check ?>

Which is much more readable, I think. You can even add else blocks to display something when nothing is found.

And don't forget <?php namespace ProcessWire;. While ProcessWire has a built-in compiler that adds the namespace for you, there may come a day when that compiler is removed from ProcessWire. Additionally, specifying the namespace helps IDEs understand the context of the code.

Link to comment
Share on other sites

I tried your suggestion but I get an error.
 

"Snapsicles… Fatal Error: Uncaught Error: Call to undefined function wireCount()"
<?php $blogger = $pages->find("template=blog_entry,featured_post=1"); // my template ?>
<?php if (wireCount($blogger)) : // Check if there are any bloggers ?>
    <?php foreach ($blogger as $bloggers) : ?>
        <?php if (wireCount($bloggers->blog_entries)) : // Check if there are any blog entries ?>
            <?php foreach ($bloggers->blog_entries as $blog_entry) : // my repeater field ?>
              <div>
                  <a href="<?= $blog_entry->url ?>">
                      <h1 class="text-xl"><?= htmlspecialchars($blog_entry->title) ?></h1>
                  </a>
                  <a href="<?= $blog_entry->url ?>">
                      <img src="<?= $blog_entry->blog_main_image->getCrop('blogsmall')->url ?>" alt="<?= $blog_entry->blog_main_image->description ?>">
                  </a>
                  <?= $sanitizer->truncate($blog_entry->blog_text, 100, ['type' => 'sentence', 'keepFormatTags' => false]) ?>
                  <p class="test_to_show_value_of_featured_filed"><?= $blog_entry->featured_post ?></p>
              </div>
			<?php endforeach; // End of blog_entries loop ?>
       <?php endif; // End of blog_entries check ?>
   <?php endforeach; // End of bloggers loop ?>
<?php endif; // End of bloggers check ?>

 

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...