Iterate Wordpress custom field? - wordpress

I have a custom field in an Apus theme of type multi checkbox and I need to display the selected values.
I am using this code:
<?php $keywords = get_post_meta( get_the_ID(), REALIA_PROPERTY_PREFIX . 'keywords', true ); ?>
<?php if ( ! empty( $keywords ) ) : ?>
<li><span><?php echo esc_html__( 'Keywords:', 'apushome' ); ?></span> <?php echo esc_attr( $keywords ); ?></li>
<?php endif; ?>
What this code is showing is
Keywords: Array
How can I loop the field's values to show them on the page?
Best regards
Americo

you may also use
echo implode(",",$keywords);
maybe this would help if this is indexed array, otherwise use
foreach ($keywords as $val){
echo $val. "\n";
}

Related

Get a Value in the Author Page from a Custom Taxonomy

I have created a Custom Taxonomy called nationality, then I added it to let the user chose his/her nationality through a Custom Account Edit Form using ACF.
I am trying to get the Tax value into the Author.php page but it returns as Array ,
This is my code which I am using:
<?php
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = wp_get_post_terms($post->ID, 'nationality' , $curauth);
if ($terms){
$out = array(
'name'=>'<span>Nationality: </span>'
);
foreach ($terms as $term)
{
$out[] = '<a " ' . ' href="' . esc_url(get_term_link( $term->slug, 'nationality')) .'">' .$term->name .'</a>' ;}
echo join( ' ', $out );
}
?>
I have also tried the following code:
<?php
$nationality = get_field('nationality', $curauth);
$nationality = get_field_object('nationality', $curauth);
echo $nationality['value'];
?>
still giving me an Array.
The Field type is select and Return Value is set to Term Object either Term ID
the error then is
Object of class WP_Term could not be converted to string
any Ideas how to make this Correct.
Thank you!
Ahmad
are you trying to get the custom taxonomy from the current user? try like this:
get_the_terms( get_the_ID(), 'nationality' )
and if you are sure it will always get only one (take into account some people have more than one nationality) just access the first element:
get_the_terms( get_the_ID(), 'nationality' )[0]
I have the right Code here :
$curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
$terms = get_field('nationality', $curauth);
if( $terms ): ?>
<?php foreach( $terms as $term ): ?>
<?php echo $term->name ; ?>
<?php endforeach; ?>
<?php endif; ?>

Wordpress meta value empty no display

I am using a directory theme and I am using the following code:
<?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); echo $listing_contact; ?>
<?php if( empty( $listing_contact) ) : ?>no contact<?php endif; ?>
However, the 'no contact' value is not displaying once the $listing_contact is empty. Can somebody help me out to get this working?
Your code works, even though I would advise you to rewrite it:
<?php $listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true ); ?>
<?php if ( empty( $listing_contact ) ) : ?>
No contact
<?php else : ?>
<?php echo $listing_contact;
<?php endif; ?>
Maybe check that you get the right ID in get_the_ID() or that your actually looking for empty() in your conditional.
The below will check and see if $listing_contact has any value if it doesn't display anything if it does display the contents of $listing_contact.
<?php
$listing_contact = get_post_meta( get_the_ID(), 'listing_contact', true );
if(!($listing_contact == null || $listing_contact == '')){
echo $listing_contact;
}else{
echo "no contact";
}
?>
Would you please try above code?

Reversing sort order of ingested RSS feed in PHP

I have a site where I am 'pulling' local events from a secondary website RSS feed. I have this working however the feed is displaying in reverse order with the local events dated later (i.e. at the end of October versus events dated for today) showing up at the top instead of the bottom.
Here is the code I am using for the feed ingest:
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://sample.com.au/events/feed/'); // specify the source feed
$limit = $feed->get_item_quantity(25); // specify number of items
$semti = array_flip($limit);
$items = $feed->get_items(0, $limit); // create an array of items
}
if ($limit == 0) echo '<div>The feed is unavailable.</div>';
else foreach ($items as $item) : ?>
<p><b><a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<?php echo esc_html( $item->get_title() ); ?></a></b>
<?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
<?php echo sanitize_text_field( $item->get_content() ); ?>
</p>
<?php endforeach; ?>
This works perfectly to get my remote RSS feed and display the title, date of the event and the excerpt, however the order is reverse sorted.
I tried adding filters like "sort and ksort" in the "foreach ($items $items) :" area but this did not work for me. I've racked my brains on this one and am hoping someone can help me out. I appreciate any guidance/help in advance.
Try the appropriately named array_reverse function!
<?php if(function_exists('fetch_feed')) {
include_once(ABSPATH . WPINC . '/feed.php'); // include the required file
$feed = fetch_feed('http://sample.com.au/events/feed/'); // specify the source feed
$limit = $feed->get_item_quantity(25); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
$semti = array_reverse($items); // & flip it
}
if ($limit == 0) echo '<div>The feed is unavailable.</div>';
else foreach ($semti as $item) : ?>
<p><b><a href="<?php echo esc_url( $item->get_permalink() ); ?>" target="_blank">
<?php echo esc_html( $item->get_title() ); ?></a></b>
<?php echo esc_html( $item->get_date('| j F | g:i a') ); ?><br>
<?php echo sanitize_text_field( $item->get_content() ); ?>
</p>
<?php endforeach; ?>
From PHP.net:
array_reverse
Return an array with elements in reverse order
array array_reverse ( array $array [, bool $preserve_keys = false ] )
Takes an input array and returns a new array with the order of the elements reversed.

Woocommerce - Print Product's Custom Field In Email

In Woocommerce, I need to print a custom field on the Completed Order email.
The template is completely unique in my themes woocommerce/emails folder because the default template doesn't work for my needs.
As such I'm trying to display the following:
Title, Quantity and two custom fields
This needs to be included in the customer-completed-order.php email template but I am not certain what syntax is necessary to print it.
I found something by Googling but it doesn't work:
<?php foreach ( $items as $item_id => $item ) :
$_product = apply_filters( 'woocommerce_order_item_product', $order->get_product_from_item( $item ), $item );
?>
<p>Class: <?php echo get_post_meta($_product->id,'name',true); ?></p>
<p>Date: <?php echo get_post_meta($_product->id,'date_text_field',true); ?></p>
<p>Time: <?php echo get_post_meta($_product->id,'time_text_field',true); ?></p>
<p>Number of Spaces: <?php echo get_post_meta($_product->id,'qty',true); ?></p>
<?php endforeach; ?>
Try adding to action hook,
do_action( 'woocommerce_order_item_meta_start', $item_id, $item, $order );
OR
do_action( 'woocommerce_order_item_meta_end', $item_id, $item, $order );
This could be used in following way:
add_action( 'woocommerce_order_item_name', 'action_custom_order_meta', 10, 3 );
OR
add_action( 'woocommerce_order_item_name', 'action_custon_order_meta', 10, 3 );
Following is the callback handler function.
function action_custom_order_meta($item_name, $item, $false){
$product_id = $item['product_id'];
$product_custom_date = get_post_meta($product_id, 'product_custom_date', true);
$product_custom_time = get_post_meta($product_id, 'product_custom_time', true);
$product_custom_meta = 'Date: ' . $product_custom_date . ' <br> Time: ' . $product_custom_time;
echo $product_custom_meta;
}
Change the custom field names to appropriate. You might also need to wrap the output according to the required markup for your custom email.
Hope this helps.

wordpress simple paypal shopping cart shortcode in custom fields

i”m using wordpress simple paypal shopping cart and i want to parse the shortcode into custom field, i”ve found an example but it”s not working
above the loop i”ve put this
<?php $price = get_post_meta( $post->ID, ‘price’, true ); ?>
and where i want to display the add to cart button i have this
<?php echo print_wp_cart_button_for_product($name, $price); ?>
it”s not working right
the add to cart button apear on every post, when u press it it add product in cart with no name and no price
when i add key=price and value= 25 it add to cart same noname and noprice product :(
btw the shortcode i want to parse it”s looks like this [wp_cart:PRODUCT NAME:price:PRODUCT PRICE:end]
this is how u do it
<?php $price = get_post_meta( $post->ID, 'price', true ); ?>
<?php $name = get_the_title(); ?>
<?php echo print_wp_cart_button_for_product($name, $price); ?>
that code display the add to cart button
Price: <?php echo get_post_meta($post->ID, 'price', true); ?> $
and that”s for the price to be displayed
have fun
To keep things organize the code it will look like this
<?php $price = get_post_meta( $post->ID, 'PRODUCT-PRICE', true ); ?>
Price: $<?php echo get_post_meta($post->ID, 'PRODUCT-PRICE', true); ?>
Product Name: <?php echo get_post_meta($post->ID, 'PRODUCT-NAME', true); ?>
<?php $name = get_the_title(); ?> <?php echo print_wp_cart_button_for_product
($name, $price); ?>

Resources