Wordpress ACF get_field not loading
I've been racking my brain on this one. I've looked through the related stack overflow questions and yet can not seem to piece together this.
I am attempting to render a link using custom fields in WordPress and the get_field()
function. I'm a new developer so I'm attempting to just console log it yet I keep returning "empty data"
and "null"
for my results. I understand get_field()
will return 2 results, but I can't seem to extract my desired links. Any help is appreciated.
Custom fields:
image
This is my code base:
$trend_one = get_field('trend_one');
$trend_two = get_field('trend_two');
$trend_three = get_field('trend_three');
?>
<div class='trend_bar'>
<div class="trend_item">TRENDING:</div>
<div class='trend_item'><?php console_log($trend_one); ?></div>
<div class='trend_item'><?php console_log($trend_two); ?></div>
<div class='trend_item'><?php console_log($trend_three); ?></div>
</div>
php wordpress advanced-custom-fields
add a comment |
I've been racking my brain on this one. I've looked through the related stack overflow questions and yet can not seem to piece together this.
I am attempting to render a link using custom fields in WordPress and the get_field()
function. I'm a new developer so I'm attempting to just console log it yet I keep returning "empty data"
and "null"
for my results. I understand get_field()
will return 2 results, but I can't seem to extract my desired links. Any help is appreciated.
Custom fields:
image
This is my code base:
$trend_one = get_field('trend_one');
$trend_two = get_field('trend_two');
$trend_three = get_field('trend_three');
?>
<div class='trend_bar'>
<div class="trend_item">TRENDING:</div>
<div class='trend_item'><?php console_log($trend_one); ?></div>
<div class='trend_item'><?php console_log($trend_two); ?></div>
<div class='trend_item'><?php console_log($trend_three); ?></div>
</div>
php wordpress advanced-custom-fields
console.log
is a javascript function. Not sure where you picked upconsole_log
- but if it's a function you've written / added, you need to share that code. There's no need, really, though - In PHP, you'd usevar_dump($trend_one)
(orvar_export
). Note you may get recommendations to useecho
orprint_r
, but those are limited - echo won't work for objects / arrays, and print_r will only work for objects / arrays, whereasvar_dump
orvar_export
will handle any variable contents.
– cale_b
Nov 21 '18 at 15:47
Please read how to create an Minimal, Complete, and Verifiable example - while your code is part of the equation, there's much more to the story, and if we don't know where you've put this code specifically, that could be the problem.get_field
will only work in the right context (when the WPpost
has been set up), so knowing where in your template, which template, etc. matters.
– cale_b
Nov 21 '18 at 15:52
There are cases when the field name won’t work - in these cases you have to use field ID (that’s a long, unique code for the field, the ACF documentation tells you how to get it). And the other thing: try to set the second parameter of the get_field() function - that’s the post ID of the WP object whose ACF field you’re trying to get.
– muka.gergely
Nov 21 '18 at 18:24
add a comment |
I've been racking my brain on this one. I've looked through the related stack overflow questions and yet can not seem to piece together this.
I am attempting to render a link using custom fields in WordPress and the get_field()
function. I'm a new developer so I'm attempting to just console log it yet I keep returning "empty data"
and "null"
for my results. I understand get_field()
will return 2 results, but I can't seem to extract my desired links. Any help is appreciated.
Custom fields:
image
This is my code base:
$trend_one = get_field('trend_one');
$trend_two = get_field('trend_two');
$trend_three = get_field('trend_three');
?>
<div class='trend_bar'>
<div class="trend_item">TRENDING:</div>
<div class='trend_item'><?php console_log($trend_one); ?></div>
<div class='trend_item'><?php console_log($trend_two); ?></div>
<div class='trend_item'><?php console_log($trend_three); ?></div>
</div>
php wordpress advanced-custom-fields
I've been racking my brain on this one. I've looked through the related stack overflow questions and yet can not seem to piece together this.
I am attempting to render a link using custom fields in WordPress and the get_field()
function. I'm a new developer so I'm attempting to just console log it yet I keep returning "empty data"
and "null"
for my results. I understand get_field()
will return 2 results, but I can't seem to extract my desired links. Any help is appreciated.
Custom fields:
image
This is my code base:
$trend_one = get_field('trend_one');
$trend_two = get_field('trend_two');
$trend_three = get_field('trend_three');
?>
<div class='trend_bar'>
<div class="trend_item">TRENDING:</div>
<div class='trend_item'><?php console_log($trend_one); ?></div>
<div class='trend_item'><?php console_log($trend_two); ?></div>
<div class='trend_item'><?php console_log($trend_three); ?></div>
</div>
php wordpress advanced-custom-fields
php wordpress advanced-custom-fields
edited Nov 21 '18 at 15:30
Gufran Hasan
3,47141326
3,47141326
asked Nov 21 '18 at 15:28
Marsel Gray
64
64
console.log
is a javascript function. Not sure where you picked upconsole_log
- but if it's a function you've written / added, you need to share that code. There's no need, really, though - In PHP, you'd usevar_dump($trend_one)
(orvar_export
). Note you may get recommendations to useecho
orprint_r
, but those are limited - echo won't work for objects / arrays, and print_r will only work for objects / arrays, whereasvar_dump
orvar_export
will handle any variable contents.
– cale_b
Nov 21 '18 at 15:47
Please read how to create an Minimal, Complete, and Verifiable example - while your code is part of the equation, there's much more to the story, and if we don't know where you've put this code specifically, that could be the problem.get_field
will only work in the right context (when the WPpost
has been set up), so knowing where in your template, which template, etc. matters.
– cale_b
Nov 21 '18 at 15:52
There are cases when the field name won’t work - in these cases you have to use field ID (that’s a long, unique code for the field, the ACF documentation tells you how to get it). And the other thing: try to set the second parameter of the get_field() function - that’s the post ID of the WP object whose ACF field you’re trying to get.
– muka.gergely
Nov 21 '18 at 18:24
add a comment |
console.log
is a javascript function. Not sure where you picked upconsole_log
- but if it's a function you've written / added, you need to share that code. There's no need, really, though - In PHP, you'd usevar_dump($trend_one)
(orvar_export
). Note you may get recommendations to useecho
orprint_r
, but those are limited - echo won't work for objects / arrays, and print_r will only work for objects / arrays, whereasvar_dump
orvar_export
will handle any variable contents.
– cale_b
Nov 21 '18 at 15:47
Please read how to create an Minimal, Complete, and Verifiable example - while your code is part of the equation, there's much more to the story, and if we don't know where you've put this code specifically, that could be the problem.get_field
will only work in the right context (when the WPpost
has been set up), so knowing where in your template, which template, etc. matters.
– cale_b
Nov 21 '18 at 15:52
There are cases when the field name won’t work - in these cases you have to use field ID (that’s a long, unique code for the field, the ACF documentation tells you how to get it). And the other thing: try to set the second parameter of the get_field() function - that’s the post ID of the WP object whose ACF field you’re trying to get.
– muka.gergely
Nov 21 '18 at 18:24
console.log
is a javascript function. Not sure where you picked up console_log
- but if it's a function you've written / added, you need to share that code. There's no need, really, though - In PHP, you'd use var_dump($trend_one)
(or var_export
). Note you may get recommendations to use echo
or print_r
, but those are limited - echo won't work for objects / arrays, and print_r will only work for objects / arrays, whereas var_dump
or var_export
will handle any variable contents.– cale_b
Nov 21 '18 at 15:47
console.log
is a javascript function. Not sure where you picked up console_log
- but if it's a function you've written / added, you need to share that code. There's no need, really, though - In PHP, you'd use var_dump($trend_one)
(or var_export
). Note you may get recommendations to use echo
or print_r
, but those are limited - echo won't work for objects / arrays, and print_r will only work for objects / arrays, whereas var_dump
or var_export
will handle any variable contents.– cale_b
Nov 21 '18 at 15:47
Please read how to create an Minimal, Complete, and Verifiable example - while your code is part of the equation, there's much more to the story, and if we don't know where you've put this code specifically, that could be the problem.
get_field
will only work in the right context (when the WP post
has been set up), so knowing where in your template, which template, etc. matters.– cale_b
Nov 21 '18 at 15:52
Please read how to create an Minimal, Complete, and Verifiable example - while your code is part of the equation, there's much more to the story, and if we don't know where you've put this code specifically, that could be the problem.
get_field
will only work in the right context (when the WP post
has been set up), so knowing where in your template, which template, etc. matters.– cale_b
Nov 21 '18 at 15:52
There are cases when the field name won’t work - in these cases you have to use field ID (that’s a long, unique code for the field, the ACF documentation tells you how to get it). And the other thing: try to set the second parameter of the get_field() function - that’s the post ID of the WP object whose ACF field you’re trying to get.
– muka.gergely
Nov 21 '18 at 18:24
There are cases when the field name won’t work - in these cases you have to use field ID (that’s a long, unique code for the field, the ACF documentation tells you how to get it). And the other thing: try to set the second parameter of the get_field() function - that’s the post ID of the WP object whose ACF field you’re trying to get.
– muka.gergely
Nov 21 '18 at 18:24
add a comment |
2 Answers
2
active
oldest
votes
Why are you using console.log inside PHP?
It should be:
<?php echo $trend_one['url']; ?>
<?php echo $trend_two; ?>
<?php echo $trend_three; ?>
https://www.advancedcustomfields.com/resources/link/
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you proposevar_dump
orvar_export
instead?
– cale_b
Nov 21 '18 at 15:48
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as usingecho
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".
– tadadadadi
Nov 21 '18 at 16:16
Agreed on all points. My only point is thatecho array(1,2,3);
will display the wordArray
on the screen, and you might want to suggestvar_dump
instead :)
– cale_b
Nov 21 '18 at 16:19
add a comment |
Here's that code you wanted for the console_log in PHP
function console_log( $data ) {
if ( empty( $data ) ){
print '<script>console.log("Empty Data");</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
if ( is_array( $data ) || is_object( $data ) ){
print '<script>console.table(' . json_encode( $data ) . ');</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
print '<script>console.log(' . json_encode( $data ) . ');</script>';
}
}
}
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%2f53415354%2fwordpress-acf-get-field-not-loading%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Why are you using console.log inside PHP?
It should be:
<?php echo $trend_one['url']; ?>
<?php echo $trend_two; ?>
<?php echo $trend_three; ?>
https://www.advancedcustomfields.com/resources/link/
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you proposevar_dump
orvar_export
instead?
– cale_b
Nov 21 '18 at 15:48
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as usingecho
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".
– tadadadadi
Nov 21 '18 at 16:16
Agreed on all points. My only point is thatecho array(1,2,3);
will display the wordArray
on the screen, and you might want to suggestvar_dump
instead :)
– cale_b
Nov 21 '18 at 16:19
add a comment |
Why are you using console.log inside PHP?
It should be:
<?php echo $trend_one['url']; ?>
<?php echo $trend_two; ?>
<?php echo $trend_three; ?>
https://www.advancedcustomfields.com/resources/link/
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you proposevar_dump
orvar_export
instead?
– cale_b
Nov 21 '18 at 15:48
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as usingecho
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".
– tadadadadi
Nov 21 '18 at 16:16
Agreed on all points. My only point is thatecho array(1,2,3);
will display the wordArray
on the screen, and you might want to suggestvar_dump
instead :)
– cale_b
Nov 21 '18 at 16:19
add a comment |
Why are you using console.log inside PHP?
It should be:
<?php echo $trend_one['url']; ?>
<?php echo $trend_two; ?>
<?php echo $trend_three; ?>
https://www.advancedcustomfields.com/resources/link/
Why are you using console.log inside PHP?
It should be:
<?php echo $trend_one['url']; ?>
<?php echo $trend_two; ?>
<?php echo $trend_three; ?>
https://www.advancedcustomfields.com/resources/link/
answered Nov 21 '18 at 15:45
tadadadadi
134
134
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you proposevar_dump
orvar_export
instead?
– cale_b
Nov 21 '18 at 15:48
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as usingecho
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".
– tadadadadi
Nov 21 '18 at 16:16
Agreed on all points. My only point is thatecho array(1,2,3);
will display the wordArray
on the screen, and you might want to suggestvar_dump
instead :)
– cale_b
Nov 21 '18 at 16:19
add a comment |
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you proposevar_dump
orvar_export
instead?
– cale_b
Nov 21 '18 at 15:48
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as usingecho
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".
– tadadadadi
Nov 21 '18 at 16:16
Agreed on all points. My only point is thatecho array(1,2,3);
will display the wordArray
on the screen, and you might want to suggestvar_dump
instead :)
– cale_b
Nov 21 '18 at 16:19
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you propose
var_dump
or var_export
instead?– cale_b
Nov 21 '18 at 15:48
Thanks for wanting to help! Note that echo won't handle some variable types (arrays, objects, booleans) - may I suggest that you propose
var_dump
or var_export
instead?– cale_b
Nov 21 '18 at 15:48
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as using
echo
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".– tadadadadi
Nov 21 '18 at 16:16
@cale_b Hi, thanks. I wasn't sure if the OP is asking about the correct way to output the value, or the correct way to debug. Since console.log is not PHP, I thought the answer might be as simple as using
echo
. Shouldn't we get a PHP error, if we use console.log? - which is why I was a bit confused, how the code returns "null" or "empty data".– tadadadadi
Nov 21 '18 at 16:16
Agreed on all points. My only point is that
echo array(1,2,3);
will display the word Array
on the screen, and you might want to suggest var_dump
instead :)– cale_b
Nov 21 '18 at 16:19
Agreed on all points. My only point is that
echo array(1,2,3);
will display the word Array
on the screen, and you might want to suggest var_dump
instead :)– cale_b
Nov 21 '18 at 16:19
add a comment |
Here's that code you wanted for the console_log in PHP
function console_log( $data ) {
if ( empty( $data ) ){
print '<script>console.log("Empty Data");</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
if ( is_array( $data ) || is_object( $data ) ){
print '<script>console.table(' . json_encode( $data ) . ');</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
print '<script>console.log(' . json_encode( $data ) . ');</script>';
}
}
}
add a comment |
Here's that code you wanted for the console_log in PHP
function console_log( $data ) {
if ( empty( $data ) ){
print '<script>console.log("Empty Data");</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
if ( is_array( $data ) || is_object( $data ) ){
print '<script>console.table(' . json_encode( $data ) . ');</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
print '<script>console.log(' . json_encode( $data ) . ');</script>';
}
}
}
add a comment |
Here's that code you wanted for the console_log in PHP
function console_log( $data ) {
if ( empty( $data ) ){
print '<script>console.log("Empty Data");</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
if ( is_array( $data ) || is_object( $data ) ){
print '<script>console.table(' . json_encode( $data ) . ');</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
print '<script>console.log(' . json_encode( $data ) . ');</script>';
}
}
}
Here's that code you wanted for the console_log in PHP
function console_log( $data ) {
if ( empty( $data ) ){
print '<script>console.log("Empty Data");</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
if ( is_array( $data ) || is_object( $data ) ){
print '<script>console.table(' . json_encode( $data ) . ');</script>';
print '<script>console.log(' . json_encode( $data ) . ');</script>';
} else {
print '<script>console.log(' . json_encode( $data ) . ');</script>';
}
}
}
answered Nov 21 '18 at 15:59
Marsel Gray
64
64
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53415354%2fwordpress-acf-get-field-not-loading%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
console.log
is a javascript function. Not sure where you picked upconsole_log
- but if it's a function you've written / added, you need to share that code. There's no need, really, though - In PHP, you'd usevar_dump($trend_one)
(orvar_export
). Note you may get recommendations to useecho
orprint_r
, but those are limited - echo won't work for objects / arrays, and print_r will only work for objects / arrays, whereasvar_dump
orvar_export
will handle any variable contents.– cale_b
Nov 21 '18 at 15:47
Please read how to create an Minimal, Complete, and Verifiable example - while your code is part of the equation, there's much more to the story, and if we don't know where you've put this code specifically, that could be the problem.
get_field
will only work in the right context (when the WPpost
has been set up), so knowing where in your template, which template, etc. matters.– cale_b
Nov 21 '18 at 15:52
There are cases when the field name won’t work - in these cases you have to use field ID (that’s a long, unique code for the field, the ACF documentation tells you how to get it). And the other thing: try to set the second parameter of the get_field() function - that’s the post ID of the WP object whose ACF field you’re trying to get.
– muka.gergely
Nov 21 '18 at 18:24