-4

嗨,我需要将此代码包装在一个函数中以供以后使用,我是 php 新手,请提供任何帮助

$term_slug = get_query_var( 'term' );
$taxonomyName = get_query_var( 'taxonomy' );
$current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
$args = array( 'child_of' => $current_term->term_id, 'hide_empty'=>false);
$terms = get_terms( 'tagportifolio', $args);
$assoc = taxonomy_image_plugin_get_associations();

if (!empty($terms)):
?>

<?php foreach( $terms as $child ): ?>
<?php if(array_key_exists( $child->term_taxonomy_id, $assoc )){echo wp_get_attachment_image( $assoc[$child->term_taxonomy_id], array(), false, 'thumbnail'); }
?>
<a href="<?php echo get_term_link( $child->name, $taxonomyName );?>">
<?php echo $child->name; ?></a ><br/>
<?php endforeach; ?>
<?php else: ?>

谢谢

4

1 回答 1

0

如果需要使它起作用,它可能会有所帮助,格式很少,并使用变量返回整个 html,

<?php
function formatOutput()
{
    $term_slug = get_query_var( 'term' );
    $taxonomyName = get_query_var( 'taxonomy' );
    $current_term = get_term_by( 'slug', $term_slug, $taxonomyName );
    $args = array( 'child_of' => $current_term->term_id, 'hide_empty'=>false);
    $terms = get_terms( 'tagportifolio', $args);
    $assoc = taxonomy_image_plugin_get_associations();
    $output = '';
    if (!empty($terms))
    {
        foreach($terms as $child ) 
        {
            if(array_key_exists( $child->term_taxonomy_id, $assoc ))
            {
                $output .= wp_get_attachment_image( $assoc[$child->term_taxonomy_id], array(), false, 'thumbnail'); 
            }
            $output .= "<a href=get_term_link( $child->name, $taxonomyName ) >$child->name;</a >";  
        }
    }
    else
    {
        /* else code  : might be empty output*/
        $output .= '';
    } 
    return $output;
}
 ?>
于 2012-07-30T10:32:01.813 回答