admin管理员组文章数量:1323330
I want to generate auto shortcode for custom post type. This is same as the elementor template shortcode.
I have a custom post type named clients and there are many clients. Single client post type is generate with custom template. Now what I want is to generate auto shortcode for every single posts. Like for client1 : [clients name="1"]
for client2: [client name="2"]
. If I create a new client, the shortcode will be generated automatically and will be shown in a field in the list.
I am stuck and without any idea. Please help.
Thank you
I want to generate auto shortcode for custom post type. This is same as the elementor template shortcode.
I have a custom post type named clients and there are many clients. Single client post type is generate with custom template. Now what I want is to generate auto shortcode for every single posts. Like for client1 : [clients name="1"]
for client2: [client name="2"]
. If I create a new client, the shortcode will be generated automatically and will be shown in a field in the list.
I am stuck and without any idea. Please help.
Thank you
Share Improve this question edited Jan 21, 2020 at 12:01 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Jan 21, 2020 at 11:56 Nayan ChowdhuryNayan Chowdhury 34 bronze badges 4- Hello! Welcome to WPSE, can you explain in different words what you mean by "generate auto shortcode"? This doesn't make much sense to me, and what is the list you're referring to? Can you explain what you want without using shortcodes? What are you trying to implement using shortcodes? – Tom J Nowell ♦ Commented Jan 21, 2020 at 12:02
- So here is answer to your question @TomJNowell : 1. I want to show custom post as topic in learndash. That's why I am trying to generate shortcode for each post. I have an idea, the posts could be parsed via post id. But I want that shortcode to be generated automatically. And the list I mention is the list of post (custom). Hope this helps. Would love to hear your ideas – Nayan Chowdhury Commented Jan 22, 2020 at 4:29
- How are the posts being created? – Tom J Nowell ♦ Commented Jan 22, 2020 at 10:46
- Can u tell me how to add builder element into the shortcode. I want all the post content from shortcode. Your example displays post title and I am using Avia builder enfold for sections and I want it to load with shortcode. – Md Sarfaraz Hussain Commented Sep 3, 2020 at 7:58
1 Answer
Reset to default 0There'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
);
本文标签: How to generate auto shortcode for custom post type same as Elementor Template
版权声明:本文标题:How to generate auto shortcode for custom post type same as Elementor Template? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139957a2422537.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论