There's nothing to "generate". All Elementor is doing is giving an example of how to use their shortcode for a specific item. You only need one shortcode client
, that accepts a parameter to determine which client to show:
add_shortcode(
'client',
function( $atts ) {
$atts = shortcode_atts( [ 'id' => null ], $atts, 'client' );
if ( $atts['id'] ) {
$post = get_post( $atts['id'] );
return $post->post_title;
}
}
);
So that basic shortcode can be used to output the post title for the given post ID.
That's all you need for the shortcode to work for all your client posts. There's no need to generate something for each post. However, what you might want do do, and what it sounds like Elementor does, is give a preview of how to use the shortcode for each post, to save the user having to write it themselves.
To do this, you can add a column to the posts list table. Assuming your post type is called client
, you can add the column with manage_client_posts_columns
:
add_filter(
'manage_client_posts_columns',
function( $columns ) {
$columns['shortcode'] = 'Shortcode';
return $columns;
},
100
);
And then you can populate it with manage_client_posts_custom_column
. All you need to do is print your shortcode prefilled with the ID of the current post:
add_action(
'manage_client_posts_custom_column',
function( $column, $post_id ) {
if ( 'shortcode' === $column ) {
echo '[client id="' . $post_id . '"]';
}
},
10,
2
);