get value of image custom field at frontent of wordpress? - 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.

Related

how to retrieve custom fields on taxonomy page?

I am using the "advanced custom fields" plugin and need to have it so that a custom field is pulled in for the category pages. I can get these to come in on pages, but the category pages are giving me a lot of trouble... 'video' is the name of the custom field I want to pull in.
This is the code I am currently using:
<?php echo get_field('video', 'clear-creek'.$wp_query->queried_object->term-4); ?>
or just a standard version like this which works on the regualar pages...
<?php the_field('video'); ?>
but it's not working... can someone please help steer me in the right direction?
Thanks!
If you are on a category's archive page, you would use this:
<?php echo get_field('video', 'category_'.get_query_var('cat')) ?>
If you are on a custom taxonomy instead, you would use this:
<?php $queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
echo get_field('video', $taxonomy . '_' . $term_id); ?>
This will dynamically get the taxonomy's slug and ID, and build out your get_field based on that information.

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.

Advanced custom field (wordpress plugin of same name) not showing

I've set up some fields using the advanced custom fields.
I’ve created a custom field and a post that uses that custom field. I’m trying to display it on a page like this:
<?php
$args = array( 'post_type' => 'Portfolio Item' );
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
echo '<p>' . the_title() . '</p>';
echo '' . the_field('portfolio_url') . '';
endwhile;
?>
The title displays no problem, but the custom field does not i.e. the output is just:
The name ‘portfolio_url’ is the ‘Field Name’.
Can anyone help with what I’m doing wrong?
Maybe you should try and send in smaller snippets of code.
Or give an online example.
Basically if you add a the_field('bottom_quote') function on your page it should echo out the current pages' "bottom_quote" field.
If you're not in a WP loop you have to explicitly point to the post you want to get the field from of using an ID:
<?php the_field( 'bottom_quote', $post->ID );
Also note that $post should either be global or in a foreach loop.
I don't think the post_type parameter is allowed to have a space. Check that you're using the correct slug for that first.
I am not to familiar with this specific plugin but you may need to call in the global $variable that I know when using a class like WPAlchemy you need to call $meta
Check here http://codex.wordpress.org/Function_Reference/get_post_meta

In Wordpress, Is there a way to reference a custom post types custom field data?

Inside of the functions.php file, is there a way to get a custom post types custom field data? I'm would like to know what the data is depending on any post id that is passed?
<?php
$id = YOUR_POST_ID;
$meta = get_post_meta($id, 'custom_field_name', true);
echo $meta;
?>

Using wp_query to pull content from a specific post using either title or id

I am trying to pull excerpts and custom fields from specific posts, I have tried using post titles and post id but I am only succeeding in pulling every single post using this query. For example I have this code trying to pull just the title for the post with id 182
<?php $map = new WP_Query();
$map->query('post_id=182'); ?>
<?php while ($map->have_posts()) : $map->the_post(); ?
<?php the_title(); ?>
<?php endwhile; ?>
It pulls the title of every single post using this method and I can not figure out how I am going to have multiple loops like this each pulling content from just one specific post. Can someone please explain where I went wrong?
I've had luck with WP_query('p=182').
If you know the post ID then you can use get_post($post_id); like so
$post_id = 182;
$my_post = get_post($post_id);
$title = $my_post->post_title;
echo $title;
echo $my_post->post_content;
ckeckout codex

Resources