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(); ?>
4

1 回答 1

1

这主要取决于插件实际在做什么,因为您的代码语法是正确的。

如果您使用的是多个内容块插件并且使用的是最新的 Wordpress 版本 3.5.1,那么我相信该插件可能不兼容。我会检查插件与您的 Wordpress 安装的版本兼容性,因为这可能是您的问题。

编辑:

该插件通过对函数 the_content() 应用过滤器来工作,因此它只能通过在调用 the_content() 函数之前声明 the_block() 来工作。

一个解决方案可能是捕获输出 the_block() 并稍后使用 print 输出,例如:

<?php 
    ob_start();  
    the_block('Latest Products'); 
    $latest_products_contents = ob_get_contents(); 
    ob_end_clean();
?>
<!-- Further down.. -->
<?php echo $latest_products_contents; ?>
于 2013-03-10T23:46:43.293 回答