我想在the_content之后添加包含有关自定义帖子信息的简码,但是当我添加显示在the_content之前的代码时
这是我的代码
<?php
function foobar_func() { ?>
<h3>Title : </h3>
<ul>
<li>foo : </li>
</ul>
<?php }
add_shortcode( 'project_information', 'foobar_func' );
function wpdev_before_after( $content ) {
$beforecontent = '';
if ( is_single() ) {
$aftercontent = '[project_information]';
} else {
$aftercontent = '';
}
$fullcontent = $beforecontent . $content . $aftercontent;
return $fullcontent;
}
add_filter('the_content', 'wpdev_before_after');
?>
我的foobar_func应该是这样的,我还有更多自定义分类法要列出,如果我使用变量来显示它们,我的代码将很混乱
function foobar_func() { ?>
<h3>Title : </h3>
<ul>
<li>foo : <?php
$terms = wp_get_post_terms( $post->ID, 'taxonomy', $args );
foreach( $terms as $term ) {
if ( end($terms) == $term ){
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a>';
} else {
echo '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a> , ';
}
}
?></li>
</ul>
<?php }
答案 0 :(得分:0)
尝试将html分配给变量,然后将其返回到foobar_func()
function foobar_func() {
$html = "
<h3>Title : </h3>
<ul>
<li>foo :
";
$terms = wp_get_post_terms( $post->ID, 'taxonomy', $args );
foreach( $terms as $term ) {
if ( end($terms) == $term ){
$html .= '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a>';
} else {
$html .= '<a href="' . esc_url( get_term_link( $term ) ) . '" title="' . esc_attr( sprintf( __( '%s', 'tiads' ), $term->name ) ) . '">' . $term->name . '</a> , ';
}
}
$html .= "
</li>
</ul>";
return $html;
}