0

I'm currently trying to develop a custom Wordpress theme, and on my homepage I need to add a second content block. I am using a plugin to do this, which simply requires me to add the following where I want the content block to be.

<?php the_block('Latest Products')?> 

However when I add this it seems to have no effect which I believe is due to the formatting of my php. I'm fairly new to php, so any help is greatly appreciated.

My code is as follows - I've cut out the best part of the HTML. I think it's something to do with that 'endforeach' tag?

<?php get_header(); ?>

<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>

<?php the_content(); ?>

<?php
 global $post;
 $myposts = get_posts('numberposts=4&category=1');
 foreach($myposts as $post) :
 ?>                     
<div class="blogsnippet">
<div class="postdate">
    <span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>

<?php endforeach;?>             

<?php the_block('Latest Products')?>

<?php endwhile; endif; ?>

<?php get_footer(); ?>

EDIT

Okay, so apparently it needs to be put outside the loop, however it still won't work. Any ideas?

<?php get_header(); ?>

<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>

<?php the_content(); ?>

<?php
 global $post;
 $myposts = get_posts('numberposts=4&category=1');
 foreach($myposts as $post) :
 ?>                     
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php     the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>

<?php endforeach;?>             
<?php endwhile; endif; ?>

<?php the_block('Latest Products')?>

<?php get_footer(); ?>

1 Answer 1

1

This mostly depends on what the plugin is actually doing because your code syntax is correct.

If you are using the Multiple Content Blocks plugin and are using the latest Wordpress version 3.5.1 then I believe the plugin may not be compatible. I'd check the version compatibility of the plugin to your Wordpress install as this could be your issue.

EDIT:

The plugin works by applying a filter to the function the_content() so that is why it only works by declaring the_block() before the_content() function is called.

A solution could be to capture the output the_block() and use print it out later, as an example:

<?php 
    ob_start();  
    the_block('Latest Products'); 
    $latest_products_contents = ob_get_contents(); 
    ob_end_clean();
?>
<!-- Further down.. -->
<?php echo $latest_products_contents; ?>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Ryan - there's not enough data to determine it's compatibility yet - one person says it works but that's all. For some reason, I just don't see the second editable region in the page editor whilst it is outside the loop. If put it inside the loop, specifically above the section calling the latest 4 posts it appears and works fine, even within the loop, but obviously I need it below there. This leads me to think something is incorrect with that code, but I'm not experienced enough to be able to tell.
I made an edit to my answer that should resolve the problem. Your code is correct, it's just the plugin that requires the specific ordering.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.