Sauter la navigation

[Résolu] How to display information from a custom post in a custom shortcode

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem: I have a custom shortcode that uses the global $post object to set the post ID, but I want to use this shortcode to display information about another post.

Solution: Use a shortcode attribute to pass in the other post's ID.

Relevant Documentation:
https://codex.wordpress.org/Shortcode_API

This support ticket is created Il y a 6 années. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Ce sujet contient 5 réponses, a 2 voix.

Dernière mise à jour par francescoG-3 Il y a 6 années.

Assisté par: Christian Cox.

Auteur
Publications
#1185333

Hi, thanks, now the gallery is displayed fine.

So, now I don't know how to display the correct vlues with my custom shortcodes...

To display the age into the Profile CPT post I use this code, but what snipped permit to display the correct age into the Page?

add_shortcode( 'iam-eta', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
   global $post;
 $timestamp = get_post_meta( $post->ID, 'wpcf-data-di-nascita', true );
$age = floor((time() - $timestamp) / ( 60*60*24* 365 )) ;
return $age;
}

To display the category list in the Profile CPT post I use this code, but what code permit to display the category list in the page?

// Add Shortcode
function attivita_incontramici() {
$terms = get_the_terms($post->ID, 'attivita');
$count = count($terms);
if ( $count > 0 ) {
    foreach ( $terms as $term ) {
        $result .= "<li><span class='logo-serv'>" . $term->name . "</span></li>";
    }
}
return "<ul class='servizi'>" . $result . "</ul>";
}
add_shortcode( 'attivita', 'attivita_incontramici' );
#1185349

Instead of getting post ID from the global $post, you can use shortcode attributes to pass a post ID into the shortcode.
https://codex.wordpress.org/Shortcode_API

Here is an example:

add_shortcode( 'iam-eta', 'wpv_post_age_shortcode');
function wpv_post_age_shortcode( $atts ) {
 $a = shortcode_atts( array(
		'postid' => 0,
	), $atts );
 $timestamp = get_post_meta( $a['postid'], 'wpcf-data-di-nascita', true );
$age = floor((time() - $timestamp) / ( 60*60*24* 365 )) ;
return $age;
}
[iam-eta postid="[wpv-post-id]"]
#1185424

Hi Christian,
the code with your snipped generate an error, and I have a blank page instead of the Crea Profilo Page content.

#1186012

This code is valid, and works for me in my local environment. What is the error?

#1186023

Hi, sorry, it work.

So I had duplicated the shortcode, pasted your code and changed the name of the shortcode but I didn't had change the name of the function and this generate the issue.
I have solved.

#1186029

My issue is resolved now. Thank you!