Converting line, echo within an echo in Wordpress - php

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' ) );
}

Related

Add Html code with get post meta

I am confused with a simple issue, Actually, I am using Wordpress Custom fields using post meta
echo get_post_meta( $post->ID, '_text_field', true );
Above meta code generate a value entered in a custom field.
But, I want to add html code around it.
If i add paragraph to it like this <p> <?php echo get_post_meta( $post->ID, '_text_field', true ); ?> </p> then it is fine! but when field is empty then it shows a empty <p></p>
That's why i am looking to add a html in meta code so that if field is empty then no html code will be included in DOM.
And if field have text then it shows text with a html markup.
i am not sure how to do this!
Any idea?
Use the wpautop() function to automatically add paragraphs instead.
Example:
echo wpautop( get_post_meta( $post->ID, '_text_field', true ) );
You should probably be assigning this meta field to a variable first and checking the value is set before you attempt to output.
E.g.
$text_field = get_post_meta( $post->ID, '_text_field', true );
if ( ! empty( $text_field ) ) {
echo wpautop( $text_field );
}

get_option intermittently fail WordPress

This is my first question.
I have a custom taxonomy set up in a theme inside a functions.php file.
I've added some extra meta fields for the custom taxonomy (category), also set through the functions.php file.
I am using the update_option() function.
Here is the part that is saving the options to the DB:
<?php
// save extra category extra fields hook
add_action ( 'edited_artists', 'save_extra_category_fileds');
// save extra category extra fields callback function
function save_extra_category_fileds( $term_id ) {
if ( isset( $_POST['Cat_meta'] ) ) {
$t_id = $term_id;
$cat_meta = get_option( "category_$t_id");
$cat_keys = array_keys($_POST['Cat_meta']);
foreach ($cat_keys as $key){
if (isset($_POST['Cat_meta'][$key])){
$cat_meta[$key] = $_POST['Cat_meta'][$key];
}
}
//save the option array
update_option( "category_$t_id", $cat_meta );
}
}
?>
In my template file I am calling them like this:
<?php
$terms = wp_get_post_terms( $post->ID, 'artists');
foreach ($terms as $term){
$term_id = $term->term_id;
$term_name = $term->name;
$term_taxonomy_id = $term->term_taxonomy_id;
$term_slug = $term->slug;
//do you term meta stuff here
//print_r($term);
}
?>
This is where I use them (among other things), and it is of course inside the LOOP:
<div class="single-sculpture-artist-info">
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
<img src="<?php echo $category_meta['artists_photo'] ?>" alt="<?php echo $term_name; ?>">
</a>
<h3>
<?php echo $term_name; ?>
</h3>
<p><?php echo $category_meta['artists_city_province'] ?></p>
<p><?php echo $category_meta['artists_bio_excerpt'] ?></p>
</div>
All of this code works perfectly.
I started adding the content, but then it suddenly started to fail. I think it started when I tried to use one of the category (taxonomy) names I used while developing this whole system (my guess is that it was cached somewhere or something), but then I tried using it with different name, and adding some other which were not there before, and it fails as well. My best guess is that somehow the options table is overloaded with data (limit or something).
Is that even possible? I dont have a lot, 56 working posts in that taxonomy, and 34 categories(taxonomy terms).
I tried my best to get my head around it but couldn`t find what was the problem.
When I insert like 2 or 3 more posts, it starts messing around. So, this:
<a href="<?php echo get_site_url(); ?>/artists/<?php echo $term_slug; ?>">
outputs correct link, but, this:
<img src="<?php echo $category_meta['artists_photo'] ?>"
doesnt. It outputs data from some other category (from the same CPT). I can provide additional info upon request.
Not answering to your question but do you know there’s a big change happening with WP 4.2+ where they’re going to be splitting taxonomy terms, so that taxonomy terms don’t share the same term id if their slugs match.
Please take a look at the links below for some details on how this can be addressed
https://make.wordpress.org/core/2015/02/16/taxonomy-term-splitting-in-4-2-a-developer-guide/
https://developer.wordpress.org/plugins/taxonomy/working-with-split-terms-in-wp-4-2/
Through the help of my network I found the solution to the problem. The line where I am calling:
<?php
$category_meta = get_option( "category_$term_taxonomy_id");
?>
I switched to this:
<?php
$category_meta = get_option( "category_$term_id");
?>
This seems to have resolved the problem. However, here are some other solutions that might be more relevant in the similiar situation:
https://en.bainternet.info/tax-meta-class-faq/
also, be sure to read the things Joe Wilson posted above. Thanks again.

WordPress echo post meta array value inside input

I'm sending array meta box data using update_post_meta like below. However I cannot seem to output the post meta array value into an empty input. The meta is being stored correctly.
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}
My input field looks like this:
<input type="date" class="widefat" name="vp-date" id="vp-date" value="<?php echo $date_info['vp-date']; ?>" />
I also get a notice which traces back to the if get_post_meta function above. It says:
Trying to get property of non-object in
Any help would be great.
Thanks
That error message means that the $post is $post->ID is not an object. I don't know what script you are in but try putting
global $post;
above the if.
Making it
global $post;
if( get_post_meta( $post->ID, 'date-meta', true ) ) {
$date_info = get_post_meta( $post->ID, 'date-meta', true );
}

get_the_id vs. post->ID vs. the_id / get_post_meta

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()

Add Genesis PHP line to post_info

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');

Categories