如何在函数中包装php代码? [英] How to wrap php code inside a function?

查看:93
本文介绍了如何在函数中包装php代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我需要将这段代码封装到一个函数中,以便以后使用它,我是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:?>

谢谢

解决方案 div>

它可能有帮助,小格式,并使用变量来返回整个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, )
{
$ 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:可能为空输出* /
$ output。='';
}
return $ output;
}
?>


Hi i need to wrap this code inside a function to use it later , i am php newbie any help please

$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: ?>

Thanks

解决方案

It might help, little format,and use variable to return entire html if needed to make it function,

<?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;
}
 ?>

这篇关于如何在函数中包装php代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆