在类函数中使用HTML有什么问题吗?我在DOM中调用它,所以不需要返回字符串。
所以,正如您所看到的,我必须将一个字符串返回给WordPress核心。
public function the_contact_table(){
?>
<div>
some html here
</div>
<?php
}
当我需要这个字符串时,我是否可以使用这种方法?还有更好的方法吗,或者这是相对标准的方法吗?
public function get_single(){
ob_start();?>
<div class='staff-member single'>
<div class='col left'>
<div class='thumbnail'>
thumbnail
</div>
<?php $this->the_contact_table(); ?>
</div>
<div class='col right'>
</div>
</div>
<?php
$content = ob_get_contents();
ob_end_clean();
return $content;
}
更新
我应该解释一下为什么要这样做。我正在制作一个WordPress插件,想要控制文章类型的输出。因此,我使用了以下类似的过滤器。
public function filter_single($content){
global $post;
if ($post->post_type == 'staff-member') {
$sm = new JM_Staff_Member($post);
$content = $sm->get_single();
}
return $content;
}
所以,正如您所看到的,我必须将一个字符串返回给WordPress核心。