Insert variable value inside shortcode - php

I want to use the shortcode below to add a review box to posts in my wordpress template:
<?php echo do_shortcode('[rwp-review id="X"]'); ?>
Where X is the Review ID of the review box for each post.
To get the Review ID, I have the code below but it is not working. $postid gets the current post ID. $box result returns array values for the review box one of which is the Review ID. If I echo $reviewid, I get the Review ID which can be 0, 1, 2, 3 and upwards.
I then tried to use $reviewid in the final shortcode but It is not working. I have very little PHP knowledge so I think I inserted the code the wrong way.
<?php
$postid = get_the_ID();
$box = RWP_API::get_post_reviews_boxes( $postid, false );
$reviewid = $box[0]['review_id'];
?>
<?php echo do_shortcode('[rwp-review id=". $reviewid . "]'); ?>
Can anyone suggest the best approach to this?

Try:
<?php echo do_shortcode('[rwp-review id="'. $reviewid . '"]'); ?>
or
<?php echo do_shortcode("[rwp-review id='{$reviewid}']"); ?>

Related

How to only show category description and title if there's a description available?

Is it possible to only show the code below if there's a category description?
<h2>About <?php $cat = get_the_category(); echo $cat[0]->cat_name; ?></h2>
<?php $catID = get_the_category(); echo category_description ( $catID[0] ); ?>
I have these codes on my single posts in Wordpress but sometimes, I forgot to add descriptions on a new category that I added. So when a user visits the post, they will just see the word About Category and no description at all, making it looks like an incomplete article.
I'm not a developer and I'm also not familiar with PHP. I only added that code on the single.php to show the description. But I want it not to show when there's no description available.
Hope someone can give me the exact code to make it work.
Thanks!
Good practice to assign your values into variables at the top of your script before entering into HTML whenever possible. This will help prevent you making redundant calls and to debug your code better. Once you have your assigned values, you'll want to check if the value is empty. I am assuming the code you presented will always return some kind of value for index 0.
<?php
$cat = get_the_category();
$cat_0 = $cat[0];
$cat_0_name = $cat_0->cat_name;
$cat_0_desc = category_description($cat_0);
?>
<?php if(!empty($cat_0_desc)): ?>
<h2>About <?php echo $cat_0_name; ?></h2>
<?php echo $cat_0_desc; ?>
<?php endif; ?>
I should like to point out that I am choosing to use an alternative syntax for control structures versus the traditional brace control. This will make your code more readable and easily debugged when mixed in with HTML.
If your code is still throwing you errors, I would suggest you check your error logs as something would be happining during the get_the_cateory() call or that it's not returning any values resulting in error with $cat[0].
<?php if ($cat = get_the_category() && count($cat)>0) { ?>
<!-- <?php echo print_r($cat,1); ?> -->
<h2>About <?php echo $cat[0]->cat_name;?></h2>
<?php echo category_description ($cat[0]->cat_id); ?>
<?php } ?>

Using PHP variable inside do_shortcode() for WordPress

first - sorry about the title, not sure exactly how to name what I need.
Here's what I'd like to do:
1) Get the post ID
2) Get the Custom Field from said post id
3) Display shortcode with the custom field's value
Here's what I have:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'id-del-instructor', true);
wp_reset_query();
?>
With this, I am able to get and display the shortcode's value - although I need to store it for later use rather than display it.
Then, I found this to display the shortcode with PHP:
<?php echo do_shortcode("[awsmteam id="XXX"]"); ?>
I have tried combining these two codes but every time it breaks my site.
Basically, where it says XXX, I need the value from the shortcode. It's probably something simple to achieve but I have been looping around and can't get my head around it.
Help? :) Thanks so much!
RESOLVED with this code - thanks #Alon Eitan:
<?php
global $wp_query;
$postid = $wp_query->post->ID;
$meta = get_post_meta($postid, 'id-del-instructor', true);
wp_reset_query();
echo do_shortcode('[awsmteam id="' . $meta . '"]');
?>
If you're doing this inside a page template, you could simply do.
$meta = get_post_meta(get_the_ID(), 'id-del-instructor', true);
echo do_shortcode('[awsmteam id="' . $meta . '"]');
rather than call the global $wp_query and go through those other steps.

How to use shortcode in php code to get value via shortcode for php code?

I'm trying to get postid via shortcode to get parent category of that post.
Here is my code
I tried this code for now but not get any luck, I don't know what is going wrong. Please consider my request I'm very new to PHP and WordPress.
<?php
$category_detail=get_the_category('[field parent-id]');//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
?>
I want to print my post id like this
$category_detail=get_the_category('4');//$post->ID
I can't use PHP code to get post id as my other format is make with shortcodes so please help me in this. Thanks (Sorry for poor English)
Update : I tried this code also but no luck
<?php
$id = do_shortcode('[field parent-id]');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}
?>
The problem is this shortcode is trying to get the parent-id, but the parent ID of what? You need to tell It, so try this (untested)
global $post;
$parent = $post->post_parent;
$id = do_shortcode('[field '.$parent.']');
$category_detail=get_the_category($id);//$post->ID
foreach($category_detail as $cd){
echo $cd->cat_name;
}

Retrieve value of custom meta box page

I have added a custom meta box, once i update the data, it saved successfully. But now i am trying to fetch using this code, but it is not getting content of meta box saved. Please help me out.
<?php
global $post;
$code = get_post_meta( $post->ID, 'caption_code_footer', true );
if($code != ''){
echo $code;
}
else { ?>
I am lorem Ipusm Text
<? } ?>
This code appears okey, so you need debug a bit....It's sound like $post->ID has wrong value... try echo it. echo $post->id; or all the object var_dump($post);
Also Check If you arent in right context. For example in category context you need to use the loop...
I think something error on update your meta box please try this..
$footer=$_POST['caption_code_footer'];
update_post_meta($post_id,'caption_code_footer',$footer);
for echo the value updated..
get_post_meta($post->ID, 'caption_code_footer', true);

Show Post Count For User Wordpress

I am trying to show the amount of posts each users on my WP site has posted.
Currently the code I have is:
<?php
$author_id = the_author_meta('ID');
echo count_user_posts('$author_id');
?>
As you can see I am storing the author ID in $author_id and running that in the count_user_posts() echo.
When I run the to strings separately it works however when, I combine them as above it doesn't.
Any ideas?
Regards,
You should use get_the_author_meta for this kind of purposes.
$author_id = get_the_author_meta('ID');
echo count_user_posts($author_id);
the_author_meta should be used only for echoing stuff.
Try to use it like this :
<?php
$author_id = the_author_meta('ID');
echo count_user_posts($author_id); // remove quotes
?>
Hope it works for you now.
I found you need to add your post, page or your_custom_post_type for it to work successfully.
<?php echo 'Posts made: ' .(count_user_posts(get_the_author_meta('ID'),'your_custom_post_type') ); ?>
// or multiple post types
<?php echo 'Posts made: ' . count_user_posts( get_the_author_meta('ID'),['job', 'featured_job', 'free_job'] ); ?>

Categories