Custom Fields/Meta Boxes - Only Display If Value is Present - wordpress

I am using the Custom Metaboxes and Fields for WordPress add-on to create custom fields for custom post types. However, I need a way to display content only if a value exists for a specific custom field.
Currently, I am using this code:
<?php
$url = get_post_meta($post->ID, 'snippet-reference-URL', true);
if ($url) {
echo "<p><a href='$url'>Reference URL</a></p>";
} ?>
However, this displays content if the field is present (which in this case, is always). I need code that will only display content if a specific field has a value.

check this example given on codex page
<?php
$key_1_value = get_post_meta( get_the_ID(), 'key_1', true );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
?>

Related

how to show the data of CPT UI plugin on website?

I have used CPT UI to add some posts with taxonomies. I have filled two post data in CPT UI for practice. Now I want to show these post on a page. What all code I have to write.
You can use Wp_Query along with the post name that you created using CPT Ui plugin to display those posts. Like, e.g. i had created a post named as school then code to display all posts of School type is as following :
$query = new WP_Query( array( 'post_type' => 'school' ) );
while($query->have_posts()):
$query->the_post();
echo $query->ID; // it will print the ID of post
endwhile;
Hope this will clear the things..
In order to pull in the custom fields/post meta, you will need to write some code within the WordPress loop (https://codex.wordpress.org/The_Loop) in your template file.
The Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes each post to be displayed on the current page, and formats it according to how it matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop will be processed on each post.
eg.
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
For all post meta:
$meta = get_post_meta( get_the_ID() );
echo '<pre>';
print_r( $meta );
echo '</pre>';
Or for a single value:
$custom_field_value = get_post_meta( get_the_ID(), 'custom_field_key_name', true );
See below for more information in the WordPress Codex:
https://codex.wordpress.org/Custom_Fields
and
https://developer.wordpress.org/reference/functions/get_post_meta/

Wordpress - Can't output image (from custom field) for previous and next posts

I am working on a portfolio website. Whenever a portfolio piece is clicked, the user is taken to a page that shows details about that piece (i.e. more photos and information). There will also be previous and next navigation links to get to additional pieces. However, I want the previous and next navigation links to be a thumbnail photo of the next piece (custom field for that is thumbnail_photo). This is what I have so far:
<?php
$previous_post = get_previous_post();
$next_post = get_next_post();
$prev_value = get_post_meta( $previous_post->ID, 'materials', $single = true);
$next_value = get_post_meta( $next_post->ID, 'thumbnail_photo', $single = true);
?>
<p><?php echo $prev_value; ?></p>
<p><?php echo $next_value; ?></p>
I used 'materials' in the call for $prev_value since 'materials' is another custom field. I just wanted to see if it was actually working. It outputs the materials just fine, but it only outputs the ID number of the thumbnail_photo. I can't get it to reference the file name so that I can output the actual image.
I am using the Advanced Custom Fields plugin, so each image is stored as an image object. So this is how I would typically output a thumbnail image:
<?php
$image = get_field('thumbnail_photo);
echo $image[sizes]["thumbnail"]; // thumbnail is a reference to wordpress' thumbnail media size
?>
When you configure the ACF input field, be sure you set the return value to be an image object instead of the image ID after change this, or if you already changed, you have to update the post or posts you're trying to get. Because, if you set the field return value as image ID, created the posts and now you wanna change their values you have to modify all the posts because every post_meta is containing the ID of the image.
In the ACF website has a tutorial how you could get the values an turn into images:
With the ID
<?php
wp_get_attachment_image( $next_value, 'thumbnail' );
?>
Another Example With the ID, but this time you have to create the image HTML, the function returns an array with the url, width and height
<?php
$image = wp_get_attachment_image_src( $next_value, 'thumbnail' );
// url = $image[0];
// width = $image[1];
// height = $image[2];
?>
<img src="<?php echo $image[0]; ?>" />
You could do a check for the returned value and then write the image, example:
<?php
if( is_int( $next_value ) ) {
wp_get_attachment_image( $next_value, 'thumbnail');
} elseif ( is_object( $next_value ) ) {
echo $next_value['sizes']['thumbnail'];
} else { ?>
<img src="<?php the_field('field_name'); ?>" alt="" />
<?php } ?>
I didn't test it, but I think it works fine.
I hope this help and sorry for the bad english

get value of image custom field at frontent of wordpress?

Please help me out.I am battling since three days.
How to fetch value of a custom field to display it in post content area?
You can get custom field to display using below code:
get_post_meta($post_id, $key, $single);
Custom field data can be fetched inside template (inside loop) using this functions:
Fetches all fields
<?php the_meta(); ?>
OR for specific value
get_post_meta($post_id, $key, $single);
You can learn more + examples in official wordpress documentation about custom fields.
You can use this through below code:
<?php echo get_post_meta($post_id, $key, true); ?>
Here your arguments must same as you have mention in function.php, while creating your custom field like as in:
<?php add_post_meta($post_id, $key, $single,true); ?>
Thanks.

Fetch custom field data for a specific blog post

I want to output the metadata from a custom field in WordPress post.
On this page if WordPress codex I found the following instruction:
To fetch meta values use the get_post_meta() function:
get_post_meta($post_id, $key, $single);
I am trying to do it this way:
<?php
get_post_meta(1, 'Currently Reading',true);
?>
But nothing gets output in the browser.
What's the proper way to output the content of the custom field?
The easiest way to do this is this:
<?php echo get_post_meta($post->ID, 'your_meta_key', true); ?>
On your post or page editor, you can go to "Screen Options" in the top right corner and check the box to display "Custom Fields". This will allow you to see the meta keys available. Just copy the name of the meta key into your get_post_meta call in the spot above where it says "your_meta_key". Don't change the $post->ID as that is global.
Taken from that page linked
<?php $meta_values = get_post_meta( $post_id, $key, $single ); ?>
so you'd need to access it through the $meta_values return object.
Like so:
<?php
print_r($meta_values);
print 'The ID is: ' . $meta_values->ID;
?>
get_post_meta(1, 'Currently Reading',true); will only get the values, you need to store it somewhere and output it properly. One way to do this is to store the function return values into a variable like so:
<?php $custom = get_post_meta( 1, $key, $single ); ?>
Then you can output it with a print or echo like so:
echo $custom;
Something to note, try using a value $post_id for the first argument. This will grab the current post id.

wordpress woocommerce: how to display product custom attribute on category page

i am using woocommerce theme. created some products with custom attribute Qun
i want to display Qun on ca tegorypage below the product image.
is there any solution for that? have a look at the attached image.
i want to display that custom attribute on category page.
You can use while loop like this
if (have_posts()) {
while (have_posts()) {
the_post();
$key_1_values = get_post_meta( $post->ID, 'Qun' );
// check if the custom field has a value
if( ! empty( $key_1_value ) ) {
echo $key_1_value;
}
}
}
Try this
$result = array_shift(woocommerce_get_product_terms($product->id, 'attr/cate_name', 'names'));

Resources