Add an if alternative statement inside another if statement within a loop with php to create two links (one real and one empty)How to add elements to an empty array in PHP?Creating default object from empty value in PHP?PHP Loops within LoopsPHP If statement inside For loopIf Statement within While LoopGetting ID from get_posts within a loop in WordpressExecuting two statements alternatively,inside a looptwo foreach loop on if statementUpdate available posts in all ACF Post Object fields on change (Wordpress/Advanced Custom Fields)
Obtaining database information and values in extended properties
Forgetting the musical notes while performing in concert
How obscure is the use of 令 in 令和?
What does the same-ish mean?
Why didn't Boeing produce its own regional jet?
Standard deduction V. mortgage interest deduction - is it basically only for the rich?
Which ISO should I use for the cleanest image?
Does the Idaho Potato Commission associate potato skins with healthy eating?
Do creatures with a speed 0ft., fly 30ft. (hover) ever touch the ground?
Convert seconds to minutes
Why was Sir Cadogan fired?
Mathematica command that allows it to read my intentions
Why was the shrink from 8″ made only to 5.25″ and not smaller (4″ or less)
Why do I get negative height?
Is it possible to create a QR code using text?
GFCI outlets - can they be repaired? Are they really needed at the end of a circuit?
Why is the sentence "Das ist eine Nase" correct?
How to compactly explain secondary and tertiary characters without resorting to stereotypes?
Processor speed limited at 0.4 Ghz
Finitely generated matrix groups whose eigenvalues are all algebraic
Pact of Blade Warlock with Dancing Blade
In the UK, is it possible to get a referendum by a court decision?
How to remove border from elements in the last row?
Do Iron Man suits sport waste management systems?
Add an if alternative statement inside another if statement within a loop with php to create two links (one real and one empty)
How to add elements to an empty array in PHP?Creating default object from empty value in PHP?PHP Loops within LoopsPHP If statement inside For loopIf Statement within While LoopGetting ID from get_posts within a loop in WordpressExecuting two statements alternatively,inside a looptwo foreach loop on if statementUpdate available posts in all ACF Post Object fields on change (Wordpress/Advanced Custom Fields)
First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
echo '</ul>';
?>
Any Idea why it is not working?
php loops if-statement advanced-custom-fields
add a comment |
First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
echo '</ul>';
?>
Any Idea why it is not working?
php loops if-statement advanced-custom-fields
What's broken? What is the result of your code?
– dave
Mar 8 at 21:02
add a comment |
First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
echo '</ul>';
?>
Any Idea why it is not working?
php loops if-statement advanced-custom-fields
First the problem:
My code is broken if I add an if alternative statement inside another if statment in a loop:
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<?php $partner = the_field('industry_partner_links'); ?>
<?php
if ($partner) :
?>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
else :
?>
<a href="#" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
<?php
endif;
?>
</li>
My goal is to create a loop in my template to display some image (they have a URL that will direct to an image) based on a custom post in a Wordpress environment BUT, if the image doesn't have the link (ACF), the url will be empty (href=#").
Essentially, I created a section with my sponsors (image wrapped in a link) and I feed those images and URLs from the backend of WordPress using ACF (advanced custom field). Spoiler, it works.
Tha basic code in php:
<?php
$args = [
'posts_per_page' => -1,
'order' => 'ASC',
'orderby' => 'title',
'post_type' => 'partner',
'post_status' => 'publish',
];
if ( $posts = get_posts( $args ) )
echo '<ul class="feed-industry-partners">';
foreach ( $posts as $post )
setup_postdata( $post );
if ( has_post_thumbnail( $post->ID ) ) : ?>
<li>
<a href="<?php the_field('industry_partner_links'); ?>" target="_blank">
<?php echo get_the_post_thumbnail( $post->ID, 'full' ); ?>
</a>
</li>
<?php endif;
wp_reset_postdata();
echo '</ul>';
?>
Any Idea why it is not working?
php loops if-statement advanced-custom-fields
php loops if-statement advanced-custom-fields
edited Mar 8 at 20:49
user3833012
asked Mar 8 at 20:41
user3833012user3833012
377
377
What's broken? What is the result of your code?
– dave
Mar 8 at 21:02
add a comment |
What's broken? What is the result of your code?
– dave
Mar 8 at 21:02
What's broken? What is the result of your code?
– dave
Mar 8 at 21:02
What's broken? What is the result of your code?
– dave
Mar 8 at 21:02
add a comment |
1 Answer
1
active
oldest
votes
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
add a comment |
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%2f55070701%2fadd-an-if-alternative-statement-inside-another-if-statement-within-a-loop-with-p%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
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
add a comment |
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
add a comment |
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)
From the code you gave here I can guess that function the_field('industry_partner_links') does not return anything but it prints to output. So good news, your code is correct. The problem is with logic here. Variable $partner = the_field('industry_partner_links'); will be always falsy. But there is a workaround with output buffering (but this is ugly solution): <?php ob_start(); the_field('industry_partner_links'); $product = ob_get_flush(); ?>. More proper way is to make some switch in function the_field() which will return value instead of printing it. Or maybe make another function which will do the same job but it will return the result, hm? :)
answered Mar 8 at 21:05
Damian DziaduchDamian Dziaduch
794612
794612
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
add a comment |
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
Exactly, the right snippet is: if (get_field('industry_partner_links')) :
– user3833012
Mar 8 at 21:16
add a comment |
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%2f55070701%2fadd-an-if-alternative-statement-inside-another-if-statement-within-a-loop-with-p%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
What's broken? What is the result of your code?
– dave
Mar 8 at 21:02