I would like to have a shortcode display a value which is a custom field in Wordpress. In this case the variable "prijs".
I have tried lots of solutions on the net and lots more but no luck so far. Can anyone help me ?
Why doesn't this script show anything ? How do i display the custom field "prijs" ?
<?php
function showdetails_shortcode( $attr, $content = null ) {
return <?php $key="prijs"; echo get_post_meta($post->ID, $key, true); ?>
}
add_shortcode('showdetails', 'showdetails_shortcode');
?>
Why doesn't this script show anything?
The provided code shows a couple of syntax errors, of which the most crucial is the re-invocation of <?php within a PHP statement.
If prijs is a good custom field key for the post where this shortcode is placed, then it should work.
function showdetails_shortcode( ) {
$post_id = get_the_ID();
//either output the value or let us know the code is working but the value has not been found
$output = get_post_meta( $post_id, 'prijs', true) ? get_post_meta( $post_id, 'prijs', true) : 'NO CUSTOM VALUE FOUND' ;
return $output;
}
add_shortcode('showdetails', 'showdetails_shortcode');
Responding to a comment, here's a version with two fields and tabular output, keeping in mind that there would be cleaner (more flexible and parsimonious) ways to derive the variables and produce the output for a larger number of fields.
function showdetails_shortcode( ) {
$post_id = get_the_ID();
//extract the field values
$field1 = get_post_meta( $post_id, 'prijs', true) ? get_post_meta( $post_id, 'prijs', true) : 'PRIJS NOT FOUND';
$field2 = get_post_meta( $post_id, 'prijs2', true) ? get_post_meta( $post_id, 'prijs2', true) : 'PRIJS2 NOT FOUND';
//prepare html table output
$output = '<table><tbody>';
$output .= '<tr><td>' . $field1 . '</td></tr>';
$output .= '<tr><td>' . $field2 . '</td></tr>';
$output .= '</tbody></table>';
//return the html
return $output;
}
add_shortcode('showdetails', 'showdetails_shortcode');
Related
I'm facing a problem, while I tried to retrieve number of views for a single post by the following code:
<?php $id = get_the_ID(); ?>
<?php $views = get_post_meta($id, 'post_views_count', true); var_dump($views) ?>
But I got string(0)"" result.
As per my understanding, it means there is no 'post_views_count' column available in my database and so I found looking up my database.
Now, how can I get number of views for a single post?
Best Regards
even i was facing this problem but this code helped me i hope this helps you.
Copy this code in your function.php before the closing tag?>
function gt_get_post_view() {
$count = get_post_meta( get_the_ID(), 'post_views_count', true );
return "$count views";
}
function gt_set_post_view() {
$key = 'post_views_count';
$post_id = get_the_ID();
$count = (int) get_post_meta( $post_id, $key, true );
$count++;
update_post_meta( $post_id, $key, $count );
}
function gt_posts_column_views( $columns ) {
$columns['post_views'] = 'Views';
return $columns;
}
function gt_posts_custom_column_views( $column ) {
if ( $column === 'post_views') {
echo gt_get_post_view();
}
}
add_filter( 'manage_posts_columns', 'gt_posts_column_views' );
add_action( 'manage_posts_custom_column', 'gt_posts_custom_column_views' );
Then copy this code in single.php file in the while loop
<?php gt_set_post_view(); ?>
& now paste this code where you want to show post count
<?= gt_get_post_view(); ?>
I created this code to display the location from ACF that matches part of the URL and it works as expected.
<?php
$myurl= htmlspecialchars($_SERVER["REQUEST_URI"]);
$myexplodes = ( explode ('/', $myurl) );
$posts = get_posts(array(
'post_type' => 'my_vars',
));
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
?>
But when I try to place this code into a function nothing is displayed when I call it.
function local (){
if( $posts ){
foreach( $posts as $post ){
$value = get_field( "location" );
//echo get_field( "location" );
if( $value == $myexplodes[1]) {
echo '<h1>' . $value . ' :this is location</h1>';
}
else {
}
}
}
}
I suspected that it is a scope problem with the vars but I have tried to make the vars global but had no luck.
The first one probably works because $post is a WordPress global variable, so I advise to use another variable name in your foreach.
For the thing that you want to do you should use also the post id in the get_field function call:
$value = get_field( "location", $article->ID );
i am using advanced custom filed and i made custom author field (it could be Publisher Or Brand etc) now this author's name is not printing on product (Book) page . in custom field its for author's name slug is 'names'
add_action( 'woocommerce_after_single_product_summary', "ACF_product_content", 10 );
function ACF_product_content(){
echo '<h2> ACF Content </h2>';
if (function_exists('the_field')){
echo '<p>Woohoo, the_field function exists! </p>';
//the_field('authors');
echo '<pre>';
print_r(get_the_ID());
echo '</pre>';
echo '<pre>';
print_r(get_field('authors'));
echo '</pre>';
die;
}
}
for this i got the result
Check this report screenshot
. now problem is to show the Authors name which is ['post_title'] in this array.
i tried so many solutions but not working its not showing the result.
i used to show this result by this code
echo the_field('names');
'names' is the field name in 'Authors' custom field.
try this code for ACF
<?php
echo get_field('your custom filed slug name',get_the_ID());
?>
fetch the post title
<?php echo get_the_title(); ?>
for display author name for below function
<?php echo get_the_author(); ?>
You may use one of the methods below . You have to strictly set $post otherwise get_the_ID() funtion return false.
global = $post;
$custom_key = 'author';
$post_id = get_the_ID();
echo $customer_key_value = get_post_meta( $post_id, $custom_key , true );
OR
global = $post;
$custom_key = 'author';
$post_id = get_the_ID();
$custom_values = get_post_custom_values( $custom_key , $post_id );
foreach ( $custom_values as $key => $value ) {
echo "$key => $value <br />";
}
This is my first try at php.
Trying to create a custom shortcode in wordpress to call custom metadata and put it inside a page content. It worked fine without the
function wpsl_staff() {
echo '<div class="staff">' .wpsl_get_staff(). '</div>';
function wpsl_get_staff() {
and with the shortcode being
add_shortcode( 'wpsl_staff', 'wpsl_get_staff' );
The output was correct but the output appeared at the top of the page above all other content and when I inspected in the browser it had no div or class id and just appeared as text. How do I wrap this in a div and give id so it sits properly on the content page (It should display between 2 images and under another shortcode) I have managed to give it a class id previously and get it to appear in a div but the output still appears at the top of the page above the images and other shortcode.
<?php
function wpsl_get_staff() {
global $post;
$queried_object = get_queried_object();{
$Manager = get_post_meta( $queried_object->ID, 'wpsl_Manager', true );
if (!empty( $Manager )) return 'Manager : ' .$Manager . "<br>";
else {}
$Assitant = get_post_meta( $queried_object->ID, 'wpsl_Assistant_Manager', true );
if (!empty( $Assistant )) return 'Assistant Manager : ' .$assistant . "<br>";
else {}
$Agent = get_post_meta( $queried_object->ID, 'wpsl_Agent', true );
if (!empty( $Agent )) return 'Agent : ' .$Agent . "<br>";
else {}
}
}
function wpsl_staff() {
echo '<div class="staff">' .wpsl_get_staff(). '</div>';
}
add_shortcode( 'wpsl_staff', 'wpsl_dio_staff' );
Thanks
Toca
Shortcode functions are supposed to return the result, not directly output it.
function wpsl_staff() {
return '<div class="staff">' .wpsl_get_staff(). '</div>';
}
Edit: You can only return one value from a function - so if your wpsl_get_staff is supposed to return data for all three types, then you need to assemble that data into a string variable first, and then return that at the end of the function - something like this:
function wpsl_get_staff() {
global $post;
$queried_object = get_queried_object();
$output = '';
$Manager = get_post_meta( $queried_object->ID, 'wpsl_Manager', true );
if (!empty( $Manager )) {
$output .= 'Manager : ' .$Manager . "<br>";
}
$Assitant = get_post_meta( $queried_object->ID, 'wpsl_Assistant_Manager', true );
if (!empty( $Assistant )) {
$output .= 'Assistant Manager : ' .$assistant . "<br>";
}
$Agent = get_post_meta( $queried_object->ID, 'wpsl_Agent', true );
if (!empty( $Agent )) {
$output .= 'Agent : ' .$Agent . "<br>";
}
return $output;
}
I have a meta value set for a post. The meta is '_test_field_one' and the value is "Cats".
I've made the following function just to test it (it's in a class, yes):
public function get( $post_id, $metakey ) {
echo $metakey; // test_field_one
$metakey = '_' . $metakey;
echo $metakey; // _test_field_one
echo get_post_meta( $post_id, $metakey, true ); // nothing ..
echo get_post_meta( $post_id, '_test_field_one', true ); // Cats
return get_post_meta( $post_id, $metakey, true );
}
In the comments after each echo I've indicated what gets printed on the screen.
Does anyone know what is the reason for the third echo to not work (additionally the function returns nothing).
Apparantly $metakey and '_test_field_one' are unequal but appear equal when echoed. The most likely solution is that $metakey has some trailing spaces. You can remove them using trim($metakey).
Official documentation