Count images in php and add class on ul
How can I add a certain class to the ul based on how many images there are?
So when there is 2 images the ul get class"images one-photo"
and when there are 5 images the ul gets the class class"images five-photo"
and so on...
What I now have in code:
<?php if( have_rows('blog_item') ): ?>
<?php while( have_rows('blog_item') ): the_row(); ?>
<ul class="images">
<?php if( get_sub_field('photo-01') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-01'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-02') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-02'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-03') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-03'); ?>" alt="" /></li>
<?php endif; ?>
</ul>
<?php endwhile; ?>
<?php endif; ?>
php image list count
add a comment |
How can I add a certain class to the ul based on how many images there are?
So when there is 2 images the ul get class"images one-photo"
and when there are 5 images the ul gets the class class"images five-photo"
and so on...
What I now have in code:
<?php if( have_rows('blog_item') ): ?>
<?php while( have_rows('blog_item') ): the_row(); ?>
<ul class="images">
<?php if( get_sub_field('photo-01') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-01'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-02') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-02'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-03') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-03'); ?>" alt="" /></li>
<?php endif; ?>
</ul>
<?php endwhile; ?>
<?php endif; ?>
php image list count
add a comment |
How can I add a certain class to the ul based on how many images there are?
So when there is 2 images the ul get class"images one-photo"
and when there are 5 images the ul gets the class class"images five-photo"
and so on...
What I now have in code:
<?php if( have_rows('blog_item') ): ?>
<?php while( have_rows('blog_item') ): the_row(); ?>
<ul class="images">
<?php if( get_sub_field('photo-01') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-01'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-02') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-02'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-03') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-03'); ?>" alt="" /></li>
<?php endif; ?>
</ul>
<?php endwhile; ?>
<?php endif; ?>
php image list count
How can I add a certain class to the ul based on how many images there are?
So when there is 2 images the ul get class"images one-photo"
and when there are 5 images the ul gets the class class"images five-photo"
and so on...
What I now have in code:
<?php if( have_rows('blog_item') ): ?>
<?php while( have_rows('blog_item') ): the_row(); ?>
<ul class="images">
<?php if( get_sub_field('photo-01') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-01'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-02') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-02'); ?>" alt="" /></li>
<?php endif; ?>
<?php if( get_sub_field('photo-03') ): ?>
<li><img src="https://www.website.com/<?php the_sub_field('photo-03'); ?>" alt="" /></li>
<?php endif; ?>
</ul>
<?php endwhile; ?>
<?php endif; ?>
php image list count
php image list count
edited Nov 14 '18 at 21:09
Maanstraat
asked Nov 14 '18 at 20:23
MaanstraatMaanstraat
72942857
72942857
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Something like this would come to mind:
<?php
$count = 0;
$image_display = '';
for($i=1 ; $i <= 3; $i++)
if( get_sub_field('photo-0'.$i) ):
$count++; // count photos here
$image_display .= '<li><img src="https://www.website.com/'.the_sub_field("photo-0".$i).'" alt="" /></li>';
endif;
$class = '';
switch ($count)
case 1: $class = 'one-photo';break;
case 2: $class = 'two-photo';break;
case 3: $class = 'three-photo';break;
case 4: $class = 'four-photo';break;
case 5: $class = 'five-photo';break;
echo '<ul class="images '.$class.'">'. $image_display.'</ul>';
?>
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
How high is your$count
variable counting up to?
– Edwin Krause
Nov 14 '18 at 20:55
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
Did you extend theswitch
statement accordingly? In code above only 1 to 5 photos would work
– Edwin Krause
Nov 14 '18 at 21:00
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
|
show 4 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308198%2fcount-images-in-php-and-add-class-on-ul%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like this would come to mind:
<?php
$count = 0;
$image_display = '';
for($i=1 ; $i <= 3; $i++)
if( get_sub_field('photo-0'.$i) ):
$count++; // count photos here
$image_display .= '<li><img src="https://www.website.com/'.the_sub_field("photo-0".$i).'" alt="" /></li>';
endif;
$class = '';
switch ($count)
case 1: $class = 'one-photo';break;
case 2: $class = 'two-photo';break;
case 3: $class = 'three-photo';break;
case 4: $class = 'four-photo';break;
case 5: $class = 'five-photo';break;
echo '<ul class="images '.$class.'">'. $image_display.'</ul>';
?>
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
How high is your$count
variable counting up to?
– Edwin Krause
Nov 14 '18 at 20:55
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
Did you extend theswitch
statement accordingly? In code above only 1 to 5 photos would work
– Edwin Krause
Nov 14 '18 at 21:00
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
|
show 4 more comments
Something like this would come to mind:
<?php
$count = 0;
$image_display = '';
for($i=1 ; $i <= 3; $i++)
if( get_sub_field('photo-0'.$i) ):
$count++; // count photos here
$image_display .= '<li><img src="https://www.website.com/'.the_sub_field("photo-0".$i).'" alt="" /></li>';
endif;
$class = '';
switch ($count)
case 1: $class = 'one-photo';break;
case 2: $class = 'two-photo';break;
case 3: $class = 'three-photo';break;
case 4: $class = 'four-photo';break;
case 5: $class = 'five-photo';break;
echo '<ul class="images '.$class.'">'. $image_display.'</ul>';
?>
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
How high is your$count
variable counting up to?
– Edwin Krause
Nov 14 '18 at 20:55
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
Did you extend theswitch
statement accordingly? In code above only 1 to 5 photos would work
– Edwin Krause
Nov 14 '18 at 21:00
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
|
show 4 more comments
Something like this would come to mind:
<?php
$count = 0;
$image_display = '';
for($i=1 ; $i <= 3; $i++)
if( get_sub_field('photo-0'.$i) ):
$count++; // count photos here
$image_display .= '<li><img src="https://www.website.com/'.the_sub_field("photo-0".$i).'" alt="" /></li>';
endif;
$class = '';
switch ($count)
case 1: $class = 'one-photo';break;
case 2: $class = 'two-photo';break;
case 3: $class = 'three-photo';break;
case 4: $class = 'four-photo';break;
case 5: $class = 'five-photo';break;
echo '<ul class="images '.$class.'">'. $image_display.'</ul>';
?>
Something like this would come to mind:
<?php
$count = 0;
$image_display = '';
for($i=1 ; $i <= 3; $i++)
if( get_sub_field('photo-0'.$i) ):
$count++; // count photos here
$image_display .= '<li><img src="https://www.website.com/'.the_sub_field("photo-0".$i).'" alt="" /></li>';
endif;
$class = '';
switch ($count)
case 1: $class = 'one-photo';break;
case 2: $class = 'two-photo';break;
case 3: $class = 'three-photo';break;
case 4: $class = 'four-photo';break;
case 5: $class = 'five-photo';break;
echo '<ul class="images '.$class.'">'. $image_display.'</ul>';
?>
edited Nov 14 '18 at 21:09
answered Nov 14 '18 at 20:41
Edwin KrauseEdwin Krause
1,11611122
1,11611122
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
How high is your$count
variable counting up to?
– Edwin Krause
Nov 14 '18 at 20:55
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
Did you extend theswitch
statement accordingly? In code above only 1 to 5 photos would work
– Edwin Krause
Nov 14 '18 at 21:00
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
|
show 4 more comments
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
How high is your$count
variable counting up to?
– Edwin Krause
Nov 14 '18 at 20:55
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
Did you extend theswitch
statement accordingly? In code above only 1 to 5 photos would work
– Edwin Krause
Nov 14 '18 at 21:00
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
I get no error but the ul stays empty and no classes gets added in the ul.
– Maanstraat
Nov 14 '18 at 20:47
How high is your
$count
variable counting up to?– Edwin Krause
Nov 14 '18 at 20:55
How high is your
$count
variable counting up to?– Edwin Krause
Nov 14 '18 at 20:55
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
The max sub_field for the photo is 15 but not everytime every 15 fields are filled.
– Maanstraat
Nov 14 '18 at 20:59
Did you extend the
switch
statement accordingly? In code above only 1 to 5 photos would work– Edwin Krause
Nov 14 '18 at 21:00
Did you extend the
switch
statement accordingly? In code above only 1 to 5 photos would work– Edwin Krause
Nov 14 '18 at 21:00
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
Yup, made in the 'for' the 3 to 15 and i added 10 extra cases. Still the ul stays empty with li's.
– Maanstraat
Nov 14 '18 at 21:02
|
show 4 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308198%2fcount-images-in-php-and-add-class-on-ul%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown