advanced custom field not return back result - php

i am using advanced custom filed and i made custom author field (it could be Publisher Or Brand etc) now this author's name is not printing on product (Book) page . in custom field its for author's name slug is 'names'
add_action( 'woocommerce_after_single_product_summary', "ACF_product_content", 10 );
function ACF_product_content(){
echo '<h2> ACF Content </h2>';
if (function_exists('the_field')){
echo '<p>Woohoo, the_field function exists! </p>';
//the_field('authors');
echo '<pre>';
print_r(get_the_ID());
echo '</pre>';
echo '<pre>';
print_r(get_field('authors'));
echo '</pre>';
die;
}
}
for this i got the result
Check this report screenshot
. now problem is to show the Authors name which is ['post_title'] in this array.
i tried so many solutions but not working its not showing the result.
i used to show this result by this code
echo the_field('names');
'names' is the field name in 'Authors' custom field.

try this code for ACF
<?php
echo get_field('your custom filed slug name',get_the_ID());
?>
fetch the post title
<?php echo get_the_title(); ?>
for display author name for below function
<?php echo get_the_author(); ?>

You may use one of the methods below . You have to strictly set $post otherwise get_the_ID() funtion return false.
global = $post;
$custom_key = 'author';
$post_id = get_the_ID();
echo $customer_key_value = get_post_meta( $post_id, $custom_key , true );
OR
global = $post;
$custom_key = 'author';
$post_id = get_the_ID();
$custom_values = get_post_custom_values( $custom_key , $post_id );
foreach ( $custom_values as $key => $value ) {
echo "$key => $value <br />";
}

Related

How to add an if statement to a php function that returns error when empty

This is probably a stupid question but I'm quite new to php so any help would be appreciated. I'm using this code to show members in my wordpress based website the latest posts they've visited. It works fine except when they haven't visited any posts, then it returns an error. (Warning: array_unique() expects parameter 1 to be array, string given in and Invalid argument supplied for foreach() in ) I know I need to add a if statement to echo "You haven't seen any posts yet" but I'm lost at where to add it. I hope someone can help me out and thanks very much in advance!
/*
* Plugin Name: WPSE_63266_Recently_Viewed
*/
function wpse_63266_update_recently_viewed(){
/**
* If is admin or isn't single, then return.
* To get only singular video posts use; if(!is_singular('videos')) return;
*/
if(is_admin() || !is_single()) return;
global $post;
// Get the current post id.
$current_post_id = get_the_ID();
if(is_user_logged_in()){
// Store recently viewed post ids in user meta.
$recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
if( '' == $recenty_viewed ){
$recenty_viewed = array();
}
// Prepend id to the beginning of recently viewed id array.(http://php.net/manual/en/function.array-unshift.php)
array_unshift($recenty_viewed, $current_post_id);
// Keep the recently viewed items at 5. (http://www.php.net/manual/en/function.array-slice.php)
$recenty_viewed = array_slice($recenty_viewed, 0, 10); // Extract a slice of the array
// Update the user meta with new value.
update_user_meta(get_current_user_id(), 'recently_viewed', $recenty_viewed);
} else {
/**
* For non-logged in users you can use the same procedure as above
* using get_option() and update_option()
*/
}
}
add_action('wp_footer', 'wpse_63266_update_recently_viewed');
function wpse_63266_show_recently_viewed(){
$recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
$evets=array_unique($recenty_viewed);
$hadi = get_the_terms( $evets, 'video-genres' );
$hadi2 = get_the_terms( $evets, 'video-type' );
$hadi3 = get_the_terms( $evets, 'video-category' );
foreach($evets as $evet) {
echo '<div class="soncular"><div class="soncol1"><img src="'.get_the_post_thumbnail_url($evet, 'progression-studios-video-index').'"</div>';
echo '<div class="soncol2"><b><strong>'.get_the_title($evet).'</strong></b>';
echo '<br><span>'.$hadi->name.'</span>';
echo '<br><span>'.$hadi3->name.'</span>';
echo '<br><span>'.$hadi2->name.'</span></div></div>';
}
}
add_action('wpse_63266_recently_viewed', 'wpse_63266_show_recently_viewed');
UPDATE:
Following CBroe's advice, I've updated the function as below. Now the empty array shows no error bu the echoed code, but the actual array does not show the last viewed posts. It always turns up empty.
add_action('wp_footer', 'wpse_63266_update_recently_viewed');
function wpse_63266_show_recently_viewed(){
if(is_array($recenty_viewed)){
$recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
$evets=array_unique($recenty_viewed);
$hadi = get_the_terms( $evets, 'video-genres' );
$hadi2 = get_the_terms( $evets, 'video-type' );
$hadi3 = get_the_terms( $evets, 'video-category' );
foreach($evets as $evet) {
echo '<div class="soncular"><div class="soncol1"><img src="'.get_the_post_thumbnail_url($evet, 'progression-studios-video-index').'"</div>';
echo '<div class="soncol2"><b><strong>'.get_the_title($evet).'</strong></b>';
echo '<br><span>'.$hadi->name.'</span>';
echo '<br><span>'.$hadi3->name.'</span>';
echo '<br><span>'.$hadi2->name.'</span></div></div>';
}}
else {
echo'hmm';
}
}
The true value at the end means it will return a single value, not an array
$recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', true);
See the docs for get_user_meta
Change to
$recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', false);
I think you also then need to call for the terms individually for each recent post
function wpse_63266_show_recently_viewed(){
$recenty_viewed = get_user_meta(get_current_user_id(), 'recently_viewed', false);
$evets=array_unique($recenty_viewed);
foreach($evets as $evet) {
$hadi = get_the_terms( $evet, 'video-genres' );
$hadi2 = get_the_terms( $evet, 'video-type' );
$hadi3 = get_the_terms( $evet, 'video-category' );
echo '<div class="soncular"><div class="soncol1"><img src="'.get_the_post_thumbnail_url($evet, 'progression-studios-video-index').'"</div>';
echo '<div class="soncol2"><b><strong>'.get_the_title($evet).'</strong></b>';
echo '<br><span>'.$hadi->name.'</span>';
echo '<br><span>'.$hadi3->name.'</span>';
echo '<br><span>'.$hadi2->name.'</span></div></div>';
}
}

Using Advanced custom fields Repeater field in WooCommerce custom product Tab

I am using a Repeater of Advanced custom fields for the content of the my additional custom WooCommerce Tab. The repeater is inside a group field.
I manage to display the custom fields that is outside the repeater field.
Here is the code I used in my functions.php:
add_filter( 'woocommerce_product_tabs', 'dl_custom_product_designer_tab' );
function dl_custom_product_designer_tab( $tabs ) {
// ensure ACF is available
if ( !function_exists( 'have_rows' ) )
return;
if ( get_field('designer') ) {
$tabs[] = array(
'title' => 'DESIGNER',
'priority' => 50,
'callback' => 'dl_custom_designer_tab'
);
}
return $tabs;
}
function dl_custom_designer_tab() {
$designer = get_field('designer');
echo '<p>'.$designer['designer_image'].'</p>';
echo '<p>'.$designer['designer_name'].'</p>';
echo '<p>'.$designer['designer_short_description'].'</p>';
// loop through the rows of data
$achievements = get_field('designer_achievements');
if( $achievements ) {
// loop through the rows of data
echo '<ul>';
foreach($achievements as $achievement){
// display a sub field value
echo '<li>'.$achievement['achievement'].'</li>';
}
echo '</ul>';
}
}
Now the problem is the field inside my repeater field: The repeater sub field is not displaying anything.
What I am doing wrong? How can I have the output for the repeater sub fields?
Edit: A screenshot of the ACF settings repeater field.
UPDATE (new functional alternative):
Apparently this doesn't works with "product" post type…** This looks like a bug in this plugin (I have been able to test the same case and to reproduce the issue).
It should be reported to the authors support treads… I have do it on my side.
A temporary solution (as long as this bug is not solved by ACF team)
This is is custom function replacement for ACF repeater dedicated functions:
/**
* Custom function: Get an array of ACF repeater sub-field.
*
* #param string $master_field (the
* #param string $repeater_field
* #param array $sub_fields
* #output formatted html
*/
function repeater_subfield( $group_name, $repeater, $subfield ){
global $post, $product;
$repeater_meta_key = $group_name.'_'.$repeater;
$rows = get_post_meta( $post->ID, $repeater_meta_key, true );
for($i = 0; $i < $rows; $i++){
$subfield_meta_key = $repeater_meta_key.'_'.$i.'_'.$subfield;
$output[] = get_post_meta( $post->ID, $subfield_meta_key, true );
}
if( count($rows) > 0 ) return $output;
else return;
}
Then your tested and functional code should be:
// Add a custom product tab
add_filter( 'woocommerce_product_tabs', 'dl_custom_product_designer_tab' );
function dl_custom_product_designer_tab( $tabs ) {
// ensure ACF is available
if ( !function_exists( 'have_rows' ) )
return;
if ( get_field('designer') ) {
$tabs[] = array(
'title' => 'DESIGNER',
'priority' => 50,
'callback' => 'dl_custom_designer_tab'
);
}
return $tabs;
}
// The custom product tab content
function dl_custom_designer_tab() {
global $post, $product;
$group_name = 'designer';
$designer = get_field( $group_name );
echo '<p>'.$designer['designer_image'].'</p>';
echo '<p>'.$designer['designer_name'].'</p>';
echo '<p>'.$designer['designer_short_description'].'</p>';
$designer_achievements = repeater_subfield( $group_name, 'designer_achievements', 'achievement' );
// check if the repeater field has rows of data
if( count($designer_achievements) > 0 ):
echo '<ul>';
// loop through the rows of data
foreach( $designer_achievements as $achievement ){
// display a sub field value
echo '<li>'.$achievement.'</li>';
}
echo '<ul>';
else:
// "no rows found" optional message
echo '<p><em>No data…</em></p>';
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested and works…
Original answer:
With Advanced Custom Field Pro plugin to get the Repeater sub fields data, you will need to use the documented way, functions and methods have_rows() and get_sub_field(), this way:
function dl_custom_designer_tab() {
global $post;
$designer = get_field('designer');
echo '<p>'.$designer['designer_image'].'</p>';
echo '<p>'.$designer['designer_name'].'</p>';
echo '<p>'.$designer['designer_short_description'].'</p>';
// check if the repeater field has rows of data
if( have_rows('designer_achievements') ):
echo '<ul>';
// loop through the rows of data
while ( have_rows('designer_achievements') ) : the_row();
// display a sub field value
echo '<li>' . get_sub_field('achievement') . '</li>';
endwhile;
echo '<ul>';
else:
// "no rows found" optional message
echo '<p><em>No row founds in repeater…</em></p>';
endif;
}

WooCommerce Add New Form Field On Product Page

I've driven myself crazy looking for this all over the internet on how I would go around sorting this.
So I need to add a new form below the variations on WooCommerce, and get this to post through to the checkout and then into the order notes once payment has been processed. I have got the select fields added to the product view page (front end) using the code below, but now I need to somehow get this to post to the basket and checkout and then into the order in the backend, but not sure where to start with a function doing this.
add_action( 'woocommerce_before_add_to_cart_button', 'espresso_add_flavour_field', 0 );
function espresso_add_flavour_field() {
global $post;
global $product;
// Show this only on the subscription boxes
$product_type = $product->get_type();
if( $product_type == 'variable-subscription' ){
echo "<div class='pick-flavor'>";
//echo get_post_meta( $post->ID, 'Product Code', true );
echo "Please select your favorite flavors we can include in your subscription box*<br>";
$flavors = ( get_terms('pa_do-not-use-sub-flav', array('hide_empty' => false) ) );
echo '<select name="_redeem_in_stores[]" class="chosen-select" multiple="multiple">';
foreach($flavors as $flavor){
echo '<option>' . $flavor->name . '</option>';
}
echo "</select>";
echo "</div>";
}
return true;
}
I have finally found what I've been looking for on this guide:
https://wisdmlabs.com/blog/add-custom-data-woocommerce-order/

Can't Echo Custom Field In Widget

I'm trying to display a custom field in a widget I'm customizing but I can't get the custom field to display. I think it has something to do with the variable I'm using to get the post ID in the loop because when I change it to the standard the_title function, the widget works, so it has something to do with how I'm calling the custom field.
I know the key to the custom field is "wpcf-promo-title" but no matter what I've tried, the custom field (which holds a shortened version of the post title for the sidebar) just won't display. This code results in the thumbnails showing, but not the promo title. You can see this in action at http://www.cantstopshipping.com
Here's my code, including the query and the front end of the widget.
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
if ( ! $number = absint( $instance['number'] ) ) $number = 5;
if( ! $cats = $instance["cats"] ) $cats='';
// array to call recent posts.
$crpw_args=array(
'showposts' => $number,
'category__in'=> $cats,
);
$crp_widget = null;
$crp_widget = new WP_Query($crpw_args);
echo $before_widget;
// Widget title
echo $before_title;
echo $instance["title"];
echo $after_title;
// Post list in widget
echo "<ul>\n";
while ( $crp_widget->have_posts() )
{
$crp_widget->the_post();
?>
<li class="crpw-item">
<p style="float:left">
<?php the_post_thumbnail('sidebar-small'); ?>
</p>
<?php $promotitle = get_post_meta($post->ID, 'wpcf-promo-title', true); ?>
<p style="float:right; width:200px">
<?php echo $promotitle; ?>
</p>
<?php if ( $show_date ) : ?>
<span class="crpw-date"><?php echo "("; ?><?php echo get_the_date(); ?><?php echo ")"; ?></span>
<?php endif; ?>
</li>
<?php
}
wp_reset_query();
echo "<div class=\"fix\"></div>";
echo "</ul>\n";
echo $after_widget;
}
It looks like your global $post is missing.
But you could try get_the_ID() instead of $post->ID.
You should also consider getting rid of extract(), it's now considered a "bad" practice.
Another thing is that you should use wp_reset_postdata() to restore the global $post object. The wp_reset_query() call should be used with the query_posts() call.

Undefined variable: post in

I'm debugging my wordpress theme and I keep getting this notification in my functions.php file:
Undefined variable: post in (myfolders)/functions.php on line 961
This is line 961
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));
The code is for a custom tinymce for my custom post type(Portfolio) project description.
What is causing variable to be undefined? Here is the code in it's entirety:
add_action( 'add_meta_boxes', 'mpc_add_description_meta_box' );
// Add the projects Meta Box
function mpc_add_description_meta_box() {
add_meta_box('mpc_add_description_meta_box', 'Project Description', 'mpc_add_description_meta_box_callback', 'portfolio', 'normal', 'high');
}
// the description output
function mpc_add_description_meta_box_callback() {
global $post;
// Noncename needed to verify where the data originated
echo '<input type="hidden" name="mpc_projects_description_noncename" id="mpc_projects_description_noncename" value="'.wp_create_nonce(plugin_basename(__FILE__)).'" />';
// Get the location data if its already been entered
$input = get_post_meta($post->ID, 'mpc_projects_description', true);
// echo '<input type="text" name="mpc_projects_description" value="' . $input . '" " />';
wp_editor($input, 'mpc_projects_description', array('textarea_name' => 'mpc_projects_description', 'editor_css' => '<style>#wp-mpc_projects_description-editor-container{background-color:white;style="width:100%;}</style>'));
echo '<table id="post-status-info" cellspacing="0"><tbody><tr><td id="wp-word-count">Word count: <span class="word-count_2">';
$words = strip_tags($input);
$count = str_word_count($words, 0);
echo $count;
echo '</span></td>
<td class="autosave-info">
<span class="autosave-message"> </span>
</td>
</tr></tbody></table>';
}
//save the data
add_action('save_post', 'mpc_save_description_meta', 1, 2); // save the custom fields
function mpc_save_description_meta($post_id, $post) {
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
if ( !wp_verify_nonce( $_POST['mpc_projects_description_noncename'], plugin_basename(__FILE__) )) {
return $post->ID;
}
// Is the user allowed to edit the post or page?
if ( !current_user_can( 'edit_post', $post->ID ))
return $post->ID;
// OK, we're authenticated: we need to find and save the data
// We'll put it into an array to make it easier to loop though.
$mpc_description_meta['mpc_projects_description'] = $_POST['mpc_projects_description'];
// Add values of $mpc_description_meta as custom fields
foreach ($mpc_description_meta as $key => $value) { // Cycle through the $mpc_description_meta array!
if( $post->post_type == 'revision' ) return; // Don't store custom data twice
$value = implode(',', (array)$value); // If $value is an array, make it a CSV (unlikely)
if(get_post_meta($post->ID, $key, FALSE)) { // If the custom field already has a value
update_post_meta($post->ID, $key, $value);
} else { // If the custom field doesn't have a value
add_post_meta($post->ID, $key, $value);
}
if(!$value) delete_post_meta($post->ID, $key); // Delete if blank
}
}
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));
The post global is available inside the functions but you haven't set it outside.
Change:
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));
To:
global $post;
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));
This could also depend on where you use this code. If you fire the code before the post object is set then you'll get this error.
If it's just in functions.php then change it to:
function wpse_post_test() {
global $post;
echo apply_filters('the_content', get_post_meta($post->ID, 'mpc_projects_description', true));
}
add_action( 'wp', 'wpse_post_test' );
After reviewing your question again it appears you are attempting to output this inside functions.php. What are you trying to achieve?

Categories