0

我正在尝试在 Wordpress 中创建一个自定义画廊,该画廊显示第一个画廊项目的图像和所有其他项目的文本。

我想出了以下内容,但这仅输出第一项,而其他项则不输出。

知道我做错了什么吗?

$i = 0;
foreach ( $attachments as $id => $attachment ) {
    if ($i == 0) {
        echo 'the first one';
    }
    if ( $i !== 0) {
        $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, 0, false, false) : wp_get_attachment_link($id, 0, false, false);
        $output .= "<{$itemtag} class='gallery-item'>";
        $output .= "
            <{$icontag} class='gallery-icon'>
                $link
            </{$icontag}>";
        if ( $captiontag && trim($attachment->post_excerpt) ) {
            $output .= "
                <{$captiontag} class='wp-caption-text gallery-caption'>
                " . wptexturize($attachment->post_excerpt) . "
                </{$captiontag}>";
        }
        $output .= "</{$itemtag}>";
        if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= '<br style="clear: both" />';
        }
        $output .= 'Images: ' . count($attachments);
        $output .= "
                <br style='clear: both;' />
             </div>\n";

        return $output;
    }
    $i++;
}
4

3 回答 3

2

你正在做return $output;- 这正在停止整个事情。

试试echo $output;吧。否则我认为你已经明白了。

正如另一个回应指出的那样,您也有一些语法错误。例如,在这一行缺少一个左大括号{

        if ( $columns > 0 && ++$i % $columns == 0 )

您还需要清除 $output 变量。$output = '';每次都在循环的开头设置,否则您将继续追加$output并重复您的结果。

于 2013-05-27T09:58:13.897 回答
1

此行缺少一个左大括号:

if ( $columns > 0 && ++$i % $columns == 0 )
            $output .= '<br style="clear: both" />';

然后你的for循环就在之前结束$i++并且$i永远不会增加。

于 2013-05-27T10:04:16.517 回答
0

您可以将其用作函数,然后您可以在模板中回显该函数并

像你的functions.php中的东西

   function countimages($id)
    {
    $i = 0;
    $countthem = count($attachments); //count them
    foreach ( $attachments as $id => $attachment ) {
        if ($i == 0) {
            echo 'the first one';
        }
        if ( $i !== 0) {
            $link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, 0, false, false) : wp_get_attachment_link($id, 0, false, false);
            $output .= "<{$itemtag} class='gallery-item'>";
            $output .= "
                <{$icontag} class='gallery-icon'>
                    $link
                </{$icontag}>";
            if ( $captiontag && trim($attachment->post_excerpt) ) {
                $output .= "
                    <{$captiontag} class='wp-caption-text gallery-caption'>
                    " . wptexturize($attachment->post_excerpt) . "
                    </{$captiontag}>";
            }
            $output .= "</{$itemtag}>";
            if ( $columns > 0 && ++$i % $columns == 0 )
                $output .= '<br style="clear: both" />';
            }
            $output .= 'Images: ' . count($attachments);
            $output .= "
                    <br style='clear: both;' />
                 </div>\n";

            return $output;
        }
        $i++;
    }
    }
then echo it inside your template page
$id=the_ID();
echo countimages($id);
于 2013-05-27T10:04:37.990 回答