I have a PHP statement
<?php echo get_post_meta( $post->ID, 'jetpack-post-views', true ); ?>
This i used by a wordpress plugin called "Jetpack post views"
It asked me to place the provided code anywhere on the page to see the post views.
I want it on "genesis_entry_header" with the other post_info
I don't know how to do that, and I look everywhere for a solution before I post this.
Thank you
Try with this,
function wp_87978_jpv(){
global $post;
echo get_post_meta( $post->ID, 'jetpack-post-views', true );
}
add_action('genesis_entry_header', 'wp_87978_jpv');
Related
I am looking for a way to auto tag custom posts in Wordpress without using a plugin.
I have a custom post type 'tech-video' and I want to auto tag it with the video tag every time a post gets published of that type.
I tried this code snippet but it doesn't work:
/* Auto Tag Tech Videos */
add_action('publish_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
I'm not skilled with either PHP or Wordpress hooks so any help is appreciated.
Thank you,
You're close; You just got the hook name wrong.
Whenever a post is saved, the following is run:
do_action( "save_post_{$post->post_type}", $post->ID, $post, true );
To leverage this hook you can run:
add_action('save_post_tech_video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post){
// check the term has not already been added.
$terms = wp_get_post_terms($post->ID, 'video');
$term_names = array_map(function($term){return $term->name;},$terms);
if(!in_array('tech-video',$term_names){
wp_set_post_terms( $post_id, 'tech-video', 'video', true );
}
}
But note: Since the "save_post" hook is run every time the post is saved, you need to check that the term has not already been added.
Note that the signature for wp_set_post_terms is:
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false );
So this assumes that you have a registered taxonomy named "video", and the taxonomy is linked to the "tech_video" post type.
After doing some more digging into the Wordpress Codex I was able to figure out a more elegant solution that works great:
// Call when the post gets created
add_action('save_post_tech-video', 'tag_tech_video', 10, 2);
function tag_tech_video($post_id, $post) {
// If the video tag doesn't exist, add it
if (!has_tag('video', $post->ID)) {
wp_set_post_tags( $post_id, 'video', true );
}
}
Note that I had to change tech_video to 'tech-video' to make it match the name defined by the Custom Post Type (and thus call properly).
I like this method because it's cleaner.
Thanks #andrew for pointing me in the right direction at least!
This code is designed to add a button to specific posts using the get_post_meta function. How do I alter the get_post_meta function to display this button on a specific post? I have already tried changing its $post->ID parameter to '1464', which is the post ID I want to use.
function custom_listify_single_job_listing_actions_after() {
global $post;
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
add_filter( 'listify_single_job_listing_actions_after', 'custom_listify_single_job_listing_actions_after' );
If you only want to run this code on a specific post, you need to add an if statement to check for that post ID.
Your code would need to look similar to this:
if($post->ID == 1464){
$url = get_post_meta( $post->ID, 'your_custom_meta_key', true );
echo 'My Button';
}
This simply wraps the get_post_meta() function and echo statement so that both of these only run on the post you want them to. Any other post will ignore the code.
so I looked through a number of similar topics here and couldn't for the life of me replicate the techniques to this.
I'm working Wordpress and instead of adding the following line:
<a class="button alt live_demo" href="<?php echo get_post_meta( $post->ID, '_live_demo', true ); ?>">Live Demo</a>
..directly into Wordpress template files, I'm trying to hook in via functions.php. So first I added the function to the hook where I want it to go:
add_action('woocommerce_before_add_to_cart_button', 'add_live_demo_link');
Then created the function:
function add_live_demo_link() {
echo '<a class="button alt live_demo" href="<?php echo get_post_meta( $post->ID, '_live_demo', true ); ?>">Live Demo</a>';}
Obviously that code won't work, can anyone help converting that to a working function?
Couple of issues with your code. 1. you are not globally declaring $post so you'll never retrieve any meta. 2. You are echoing an echo, which will probably also not work.
I didn't test this so I may have made a typo, but this is on the right track:
add_action('woocommerce_before_add_to_cart_button', 'add_live_demo_link');
function add_live_demo_link() {
global $post;
$meta = get_post_meta( $post->ID, '_live_demo', true );;
printf( '<a class="button alt live_demo" href="%s">%s</a>', esc_url( $meta ), __( 'Live Demo', 'my-plugin' ) );
}
I think it must be pretty basic question but I am only starting. Can someone have a look at the 3 versions of the same (?) code below and say what the difference is? All of them seem to work fine in the loop I am working on.
Which should be used: $post->ID, $the_ID or get_the_id()?
Is it necessary to have global $post;?
global $post;
$content = get_post_meta( $post->ID, ‘my_custom_field', true );
echo $content;
or
$content = get_post_meta( $the_ID, ‘my_custom_field', true );
echo $content;
or
$content = get_post_meta( get_the_id(), ‘my_custom_field’, true );
echo $content;
Many thanks for your help
If you're inside a WordPress loop, then $post->ID it's the same as using get_the_ID()
You shouldn't need to globalize $post since it's already in the scope of a WordPress loop.
I've never seen code using $the_ID, so I would avoid using that.
The safest choice would be to use get_the_ID()
Simple Wordpress problem - get_post_meta is not retrieving custom field values. Here's the code that is pulling from the custom fields:
<img src="<?php echo FCG_PLUGIN_URL; ?>/scripts/timthumb.php?src=<?php echo get_post_meta($post->ID, 'slider_image', true); ?>&h=250&w=400&zc=1" alt="<?php echo $post_title; ?>" />
In production, this is the HTML I get:
<img alt="Post Title" src="http://***.com/wp-content/plugins/jquery-slider-for-featured-content/scripts/timthumb.php?src=/&h=50&w=80&zc=1">
You can see the src= point in the string is empty - as if there is nothing posting from it. I have isolated and echo'd just the get_post_meta and it's a whitespace. I am 100% sure it's named correctly within the post - is there something glaring I'm missing here?
If you are calling get_post_meta inside the loop then you should call get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true)
Strange things happens when you call get_post_meta inside a loop. In some themes developers hack the $post at the beginning and get_post_meta stops working so this is one of the solution for those particular cases too.
Search for the term "slider_image" in the wp_posts and wp_postmeta tables using phpmyadmin. Then view the row that has it to see if there's anything inside.
Also try changing the name of the custom value as a test and see if that works. I use this exact code to do something similar to you and it works:
<p><img src="<? bloginfo('template_url'); ?>/img/downloadresume.png"></p>
Its because of auto save.
use these lines for preventing auto save and user privileges.
// Bail if we're doing an auto save
if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
// if our current user can't edit this post, bail
if( !current_user_can( 'edit_post' ) ) return;
You could also use get_post_meta( $loop->post->ID, 'yourkey', true ); if you are using $loop = new WP_Query( $args ); or something similar.
Actually, it gave you '/', which is not nothing. I'd say that's what's saved as 'slider_image'. Check the post (or the database directly).
I've written some simple templating functions that enable you to use the meta data (custom data) in your theme. You can write a template function for any meta data key/value pair, and render it in a theme file like so:
<?php the_meta_templates($meta_data_keys) ?>
<?php the_template_for($meta_data_key) ?>
Feel free to check out the basic functions from github and give them a try. You'll need to add them to your themes functions.php file.
<?php get_post_meta(get_the_id(), 'YOURKEY', true) instead of get_post_meta($post->ID, 'YOURKEY', true) ?>
Works for me!
could it be linked to the bug
#18210 (Update_post_meta is case insensitive on meta_key, but get_post_meta is NOT) – WordPress Trac
https://core.trac.wordpress.org/ticket/18210
It would explained the different experiences, depending on db_collation... (forgive me if it total nonsense, I am a newbie .. )
WordPress Database Charset and Collation Configuration | hakre on wordpress
http://hakre.wordpress.com/2010/12/26/wordpress-database-charset-and-collation-configuration/
<?php
// Get custum fields and values
$mykey_values = get_post_custom_values('my_key');
foreach ( $mykey_values as $key => $value ) {
echo "$key => $value ('my_key')<br />";
}
?>