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)













0















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?










share|improve this question
























  • What's broken? What is the result of your code?

    – dave
    Mar 8 at 21:02















0















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?










share|improve this question
























  • What's broken? What is the result of your code?

    – dave
    Mar 8 at 21:02













0












0








0








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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















2














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






share|improve this answer























  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16












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
);



);













draft saved

draft discarded


















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









2














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






share|improve this answer























  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16
















2














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






share|improve this answer























  • Exactly, the right snippet is: if (get_field('industry_partner_links')) :

    – user3833012
    Mar 8 at 21:16














2












2








2







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










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


















  • 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




















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Understanding generators in Python2019 Community Moderator ElectionGenerator function not working pythonFor loop not executing two timesGenerators - Printing generated valuesWhy can a python generator only be used once?What exactly do generators do?What does the “yield” keyword do?What does “list comprehension” mean? How does it work and how can I use it?sklearn Kfold acces single fold instead of for loopIs a generator the callable? Which is the generator?Apply Border To Range Of Cells Using OpenpyxlCalling an external command in PythonWhat are metaclasses in Python?What is the difference between @staticmethod and @classmethod?Finding the index of an item given a list containing it in PythonDifference between append vs. extend list methods in PythonHow can I safely create a nested directory in Python?Does Python have a ternary conditional operator?Understanding slice notationUnderstanding Python super() with __init__() methodsDoes Python have a string 'contains' substring method?

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page