This worked perfectly in a previous version of wordpress, different site tho. I have a custom field called flash on several wp pages but it keeps defaulting to "printed if results are empty". Any ideas on why this wouldn't work?
<?php
if ( function_exists('get_post_custom_values') )
{
$mykey_values = get_post_custom_values('flash');
if(count($mykey_values) > 0)
{
foreach ( $mykey_values as $key => $value )
{
echo "$value";
}
}
else
{
//printed if results are empty
echo("mainPage.swf");
}
}
else
{
//printed if function doesn't exist
echo("mainPage.swf");
}
?>
Hi not really sure why that is not working (as long as you are sure you do have custom fields with 'flash' values in the post). Anyhow, try the following, I am sure the following will work.
<?php
$custom_fields = get_post_custom();
$my_custom_field = $custom_fields['flash'];
foreach ( $my_custom_field as $key => $value )
echo $key . " => " . $value . "<br />";
?>
Related
<?php if( $fields )
{
foreach( $fields as $field)
{
$value = get_field( $field['name'] );
if ($value) {
echo '<dl>';
echo '<dt>' . $field['label'] . '</dt>';
echo '<dd>' . $field['value'] . '</dd>';
echo '</dl>';
}
}
}
?>
This is what I have. If I do a var_dump on acf_get_fields, it apparently sets the value to NULL. I could have known, as it's written here:
https://www.advancedcustomfields.com/resources/get_field_object/
The problem: I have to first get all fields in a specific field_group, hence I am using acf_get_fields.
I thought with using $field['value'] I could get it to work, but apparently this does not work.
Can someone help me to retrieve the values in the foreach per field? Surely there must be a way?
PS:
<?php
$fields = get_fields();
if( $fields ): ?>
<ul>
<?php foreach( $fields as $name => $value ): ?>
<li><b><?php echo $name; ?></b> <?php echo $value; ?></li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
This gives me ALL fields. I just need a specific list of fields from a specific field group. That is the problem. Anybody knows how I can solve it?
<?php
$fields = get_field('fieldname');
foreach( $fields as $field)
{
echo '<dl>';
echo '<dd>' . $field . '</dd>';
echo '</dl>';
}
?>
Think the value is always NULL because you're getting it only the group fields, not the fields from a post. Have made a few examples maybe this will help you out.
$post_id = 123;
$group_id = 1234;
// Get post fields
$post_fields = get_fields($post_id);
foreach ($post_fields as $key => $value) {
if ($key == 'my_custom_field') {
echo $value;
}
}
// Get group fields
$group_fields = acf_get_fields($group_id);
foreach ($group_fields as $key => $group_field) {
if ($key == 'my_custom_field') {
echo $value;
}
}
// Get field objects
$field_objects = get_field_objects($post_id);
foreach ($field_objects as $key => $field_object) {
if ($key == 'my_custom_field') {
echo $field_object['value'];
}
if ($field_object['name'] == 'my_custom_field') {
echo $field_object['value'];
}
// only show fields from specific group
if ($field_object['parent'] == $group_id) {
echo $field_object['value'];
}
}
foreach(get_field('color') as $singleColorCP2) {
$new_fields = get_fields($singleColorCP2->ID);
foreach($new_fields as $name => $value)
echo $value;
}
this is my code and it is running it prints like,
firstfield_valueasecondfield_valuea<br>
firstfield_valuebsecondfield_valueb<br>
firstfield_valuecsecondfield_valuec<br>
I'm getting crazy how to print just:
firstfield_valuea
firstfield_valueb
firstfield_valuec
I'm interested only in one column.
("name" prints the correct name of the field, but still with the same problem, in couple with the second field, I can't divide)
foreach(get_field('color') as $singleColorCP2) {
$new_fields = get_fields($singleColorCP2->ID);
foreach($new_fields as $name => $value)
if($name == 'field_name'){
$val[] = $value;
}
}
echo $val[0];
echo $val[1];
echo $val[2];
I have found this solution in the end...with if. That's not what I would have liked but it works at least and it hasnt much code.
I'm a PHP beginner working with Wordpress, and I'm trying to retrieve data from custom fields (using Custom Field Suite) from a post to be displayed on a different page.
I have tried a few different ways, and found two methods that work. But as I'm a beginner, I wonder if these methods are 'proper'?
This is one solution I found, but it's not exactly elegant:
// Method 1
$current = CFS()->get( 'get_current' ); //retrieves the post ID from a custom field on the page
$custom_fields = get_post_custom($current);
$my_custom_field = $custom_fields['current_title'];
$my_custom_field2 = $custom_fields['current_artist'];
foreach ( $my_custom_field as $key) {
}
foreach ( $my_custom_field2 as $key2) {
}
echo '<h2>'.$key.'</h2>';
echo '<h1>'.$key2.'</h1>';
I tried to re-write it like this, but nothing is displayed - not sure what's wrong with this loop:
// Method 2
$current = CFS()->get( 'get_current' );
$custom_fields = get_post_custom($current);
foreach ( $custom_fields as $key) {
echo '<h2>'.$key['current_title'].'</h2>';
echo '<h1>'.$key['current_artist'].'</h1>';
}
As method 2 wasn't working, I tried something else and found that this also works (I added the loop here based on this answer: https://stackoverflow.com/a/19918170/5483154):
// Method 3
<?php while ( have_posts() ) : the_post();
$current = CFS()->get( 'get_current' );
$currentitle = get_post_meta($current, 'current_title', true);
$artistname = get_post_meta($current, 'current_artist', true);
echo '<h2>'.$currentitle.'</h2>';
echo '<h1>'.$artistname.'</h1>';
endwhile;
wp_reset_query();
?>
Method 3 seems best to me. Is this a good way of approaching this problem? And if anyone would be kind enough to explain what's wrong with method 2, I would also appreciate it. Thanks
Looking at the Custom Field Suite documentation here: http://customfieldsuite.com/api/get.html
It looks like you should be using the 'get' method:
CFS()->get( $field_name, $post_id, $options );
Eg.
$current = CFS()->get( 'get_current' );
echo '<h2>' . CFS()->get( 'current_title', $current ) . '</h2>';
echo '<h1>' . CFS()->get( 'current_artist', $current ) . '</h1>';
Ps. Don't forget to watch the order of your heading tags (<h1> should come before <h2>)
In the case of your "method 2": what if you try rewriting it something like this?
// Method 2
$current = CFS()->get('get_current');
$custom_fields = get_post_custom($current);
foreach ($custom_fields as $field) {
foreach ($field as $key => $value) {
echo $key . ': ' . $value;
}
}
I'm using Wordpress custom post field for my products in Woocommerce.
I've created a custom field called 'phone_model' and it will contain strings like 'iPhone 6 Plus' etc.
In my child theme functions.php I've added this function:
function wc_phone_model(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
foreach ( $phone_model as $key => $value ) {
echo $value;
}
}
}
It's working as expected. It will show the given value in the custom post field.
In my Woocommerce title loop title.php, I've added this line:
<div class="phone_model_wrap"><span class="phone_model"><?php wc_phone_model() ?></span></div>
How do I make this show only if there is something to show from the custom post field?
Thanks in advance guys!
Cheers Kenn
I know there is a stigma against echoing html, but in this case, I can't think of a better way. I'd recommend something like this:
function wc_phone_model(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
echo '<div class="phone_model_wrap"><span class="phone_model">';
foreach ( $phone_model as $key => $value ) {
echo $value;
}
echo '</span></div>';
}
}
Then in your title.php, its just a matter of calling the function.
<?php wc_phone_model() ?>
If someone has a clean solution without echoing html though, I'd be interested in seeing it!
#kunruh
I have faced another problem with your solution.
I have made a new function to be used elsewhere with another class etc.
function wc_phone_model_single_product(){
$custom_fields = get_post_custom($post_id);
$phone_model = $custom_fields['phone_model'];
if (is_array($phone_model)) {
echo '<div class="phone_model_wrap"><span class="phone_model_single_product">Til ';
foreach ( $phone_model as $key => $value ) {
echo $value;
}
echo '</span></div>';
}
}
The "Til" will be echoed even if there is nothing in $phone_model.
How do I avoid this?
Cheers Kenn
I'm trying to get meta tags from a site, the facebook ogs. I used a class I found online.
$metaData = MetaData::fetch('http://www.example.com');
foreach ($metaData as $key=>$value){
if (!is_array($value)){
echo "$key=>$value";
}
}
The each loop returns the following
Content-Type=>text/html; charset=utf-8description=>
og:image=>
og:type=>website
og:site_name=>Site Name
og:url=>www.example.com
How do I get values from this results by keys like "og:url" ?
foreach($metaData as $key => $value) {
if (strpos($key, 'og:') !== 0) {
continue;
}
echo $key . " => " . $value . "\n";
}
Is that you want to do?
http://pageno7.com/enrich-your-social-sharing-using-meta-tags/
best ways to learn about the meta tags and also the social sharings