Reputation: 538
I've created a post type name "Gallery" and added some post meta boxes to it. For now, let's say there is a Title and a Content for each single gallery.
now, I want to make a use of that data with Shortcode. How Is this possible?
For example -
[gallery id=x]
I know how to take the value of ID (x), but how do I output a post with the ID of that value?
I register this shortcode like so (but of course it's not completed)
function shortcote_gallery( $atts , $content = null ) {
extract( shortcode_atts(
array(
'title' => 'Default Gallery Title'
), $
return "<h3>$title</h3><p>$content</p>";
}
add_shortcode( 'gallery', 'shortcote_gallery' );
I hope I was clear enough. If not, please let me know so I explain myself better. Thanks in advance.
Upvotes: 2
Views: 5010
Reputation: 2198
In your example:
[gallery id=x]
has as parameter the field id
.
So in your shortcode-function, you need to extract that as well.
function shortcote_gallery( $atts , $content = null ) {
extract( shortcode_atts(
array(
'title' => 'Default Gallery Title',
'id' => '',
), $atts));
return "<h3>$title</h3><p>$content</p>";
}
You now can do everything you want in the usual php way. $id
now has the value you entered in your post. So if you want to output something with this ID, just fire a query to the database and fetch the results you need.
If you have something like this:
global $wpdb;
$results = $wpdb->get_results("SELECT * FROM `myTable` WHERE id ='".$id."'");
Here you go and format the output:
$content = "You have selected ".$results[0]->myField." with the id: $id";
return $content;
Upvotes: 1
Reputation: 566
You need of this function? Or I haven't understood your question. https://codex.wordpress.org/Function_Reference/get_post
Enjoy your code!
Upvotes: 2