Pass shortcode variables to template
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
What I want to achieve
I have a custom page template, and I'd like to display some selected Posts on that Page. I'm developing this feature with shortcode
. For now, I only need to receive the post ids in the page template.
What I have tried
In functions.php
:
function post_link_shortcode($atts)
$atts = shortcode_atts(
array(
'id' => '',
),
$atts,
'featured_posts'
);
add_shortcode('featured_posts', 'post_link_shortcode');
In the admin panel page editor I have added:
Here are the featured posts: [featured_posts id="358,328"]
In the page template:
the_content();
echo do_shortcode("[featured_posts]");
What I expect
It outputs Here are the featured posts: 358,328
on the page along with the normal content. But It doesn't. Any ideas?
shortcode
add a comment |Â
up vote
2
down vote
favorite
What I want to achieve
I have a custom page template, and I'd like to display some selected Posts on that Page. I'm developing this feature with shortcode
. For now, I only need to receive the post ids in the page template.
What I have tried
In functions.php
:
function post_link_shortcode($atts)
$atts = shortcode_atts(
array(
'id' => '',
),
$atts,
'featured_posts'
);
add_shortcode('featured_posts', 'post_link_shortcode');
In the admin panel page editor I have added:
Here are the featured posts: [featured_posts id="358,328"]
In the page template:
the_content();
echo do_shortcode("[featured_posts]");
What I expect
It outputs Here are the featured posts: 358,328
on the page along with the normal content. But It doesn't. Any ideas?
shortcode
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
What I want to achieve
I have a custom page template, and I'd like to display some selected Posts on that Page. I'm developing this feature with shortcode
. For now, I only need to receive the post ids in the page template.
What I have tried
In functions.php
:
function post_link_shortcode($atts)
$atts = shortcode_atts(
array(
'id' => '',
),
$atts,
'featured_posts'
);
add_shortcode('featured_posts', 'post_link_shortcode');
In the admin panel page editor I have added:
Here are the featured posts: [featured_posts id="358,328"]
In the page template:
the_content();
echo do_shortcode("[featured_posts]");
What I expect
It outputs Here are the featured posts: 358,328
on the page along with the normal content. But It doesn't. Any ideas?
shortcode
What I want to achieve
I have a custom page template, and I'd like to display some selected Posts on that Page. I'm developing this feature with shortcode
. For now, I only need to receive the post ids in the page template.
What I have tried
In functions.php
:
function post_link_shortcode($atts)
$atts = shortcode_atts(
array(
'id' => '',
),
$atts,
'featured_posts'
);
add_shortcode('featured_posts', 'post_link_shortcode');
In the admin panel page editor I have added:
Here are the featured posts: [featured_posts id="358,328"]
In the page template:
the_content();
echo do_shortcode("[featured_posts]");
What I expect
It outputs Here are the featured posts: 358,328
on the page along with the normal content. But It doesn't. Any ideas?
shortcode
edited Aug 11 at 23:50
asked Aug 11 at 19:21
Stickers
163111
163111
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
4
down vote
accepted
You can't just stick a shortcode somewhere and expect it to do something. It's basically an buffer that will take whatever code it's told to generate, compile it exactly how you tell it to, and then output it wherever the shortcode is placed.
Take a look at the Shortcode API for more information.
As for your specific example, you don't actually have anything in your shortcode that says "do something with posts 358 and 328".
You'll need to make use of something like WP_Query()
or get_posts()
inside your shortcode. You may also consider using a more unique name for your function and shortcode than post_link_shortcode
and featured_posts
to avoid naming conflicts.
Take this for example:
add_shortcode( 'stickers_featured_posts', 'stickers_featured_posts_function');
function stickers_featured_posts_function( $atts )
extract( shortcode_atts( array(
'ids' => '',
), $atts ) );
// Remove whitespace from IDs
// ex: '123, 321' => '123,321'
$ids = preg_replace('/s+/', '', $ids);
// Turn string of ID's into array
// ex: '123,321' => array(123, 321);
$id_array = explode( ',', $ids );
$args = array(
'post__in' => $id_array
);
$featured_query = new WP_Query( $args );
if( $featured_query->have_posts() )
echo '<ul>';
while( $featured_query->have_posts() )
$featured_query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
wp_reset_postdata();
else
echo 'Post IDs not found';
And the usage would be like [stickers_featured_posts ids="123,321"]
- The final output would be whatever you put in the while
loop, in the case about - a simple list of post titles.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You can't just stick a shortcode somewhere and expect it to do something. It's basically an buffer that will take whatever code it's told to generate, compile it exactly how you tell it to, and then output it wherever the shortcode is placed.
Take a look at the Shortcode API for more information.
As for your specific example, you don't actually have anything in your shortcode that says "do something with posts 358 and 328".
You'll need to make use of something like WP_Query()
or get_posts()
inside your shortcode. You may also consider using a more unique name for your function and shortcode than post_link_shortcode
and featured_posts
to avoid naming conflicts.
Take this for example:
add_shortcode( 'stickers_featured_posts', 'stickers_featured_posts_function');
function stickers_featured_posts_function( $atts )
extract( shortcode_atts( array(
'ids' => '',
), $atts ) );
// Remove whitespace from IDs
// ex: '123, 321' => '123,321'
$ids = preg_replace('/s+/', '', $ids);
// Turn string of ID's into array
// ex: '123,321' => array(123, 321);
$id_array = explode( ',', $ids );
$args = array(
'post__in' => $id_array
);
$featured_query = new WP_Query( $args );
if( $featured_query->have_posts() )
echo '<ul>';
while( $featured_query->have_posts() )
$featured_query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
wp_reset_postdata();
else
echo 'Post IDs not found';
And the usage would be like [stickers_featured_posts ids="123,321"]
- The final output would be whatever you put in the while
loop, in the case about - a simple list of post titles.
add a comment |Â
up vote
4
down vote
accepted
You can't just stick a shortcode somewhere and expect it to do something. It's basically an buffer that will take whatever code it's told to generate, compile it exactly how you tell it to, and then output it wherever the shortcode is placed.
Take a look at the Shortcode API for more information.
As for your specific example, you don't actually have anything in your shortcode that says "do something with posts 358 and 328".
You'll need to make use of something like WP_Query()
or get_posts()
inside your shortcode. You may also consider using a more unique name for your function and shortcode than post_link_shortcode
and featured_posts
to avoid naming conflicts.
Take this for example:
add_shortcode( 'stickers_featured_posts', 'stickers_featured_posts_function');
function stickers_featured_posts_function( $atts )
extract( shortcode_atts( array(
'ids' => '',
), $atts ) );
// Remove whitespace from IDs
// ex: '123, 321' => '123,321'
$ids = preg_replace('/s+/', '', $ids);
// Turn string of ID's into array
// ex: '123,321' => array(123, 321);
$id_array = explode( ',', $ids );
$args = array(
'post__in' => $id_array
);
$featured_query = new WP_Query( $args );
if( $featured_query->have_posts() )
echo '<ul>';
while( $featured_query->have_posts() )
$featured_query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
wp_reset_postdata();
else
echo 'Post IDs not found';
And the usage would be like [stickers_featured_posts ids="123,321"]
- The final output would be whatever you put in the while
loop, in the case about - a simple list of post titles.
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You can't just stick a shortcode somewhere and expect it to do something. It's basically an buffer that will take whatever code it's told to generate, compile it exactly how you tell it to, and then output it wherever the shortcode is placed.
Take a look at the Shortcode API for more information.
As for your specific example, you don't actually have anything in your shortcode that says "do something with posts 358 and 328".
You'll need to make use of something like WP_Query()
or get_posts()
inside your shortcode. You may also consider using a more unique name for your function and shortcode than post_link_shortcode
and featured_posts
to avoid naming conflicts.
Take this for example:
add_shortcode( 'stickers_featured_posts', 'stickers_featured_posts_function');
function stickers_featured_posts_function( $atts )
extract( shortcode_atts( array(
'ids' => '',
), $atts ) );
// Remove whitespace from IDs
// ex: '123, 321' => '123,321'
$ids = preg_replace('/s+/', '', $ids);
// Turn string of ID's into array
// ex: '123,321' => array(123, 321);
$id_array = explode( ',', $ids );
$args = array(
'post__in' => $id_array
);
$featured_query = new WP_Query( $args );
if( $featured_query->have_posts() )
echo '<ul>';
while( $featured_query->have_posts() )
$featured_query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
wp_reset_postdata();
else
echo 'Post IDs not found';
And the usage would be like [stickers_featured_posts ids="123,321"]
- The final output would be whatever you put in the while
loop, in the case about - a simple list of post titles.
You can't just stick a shortcode somewhere and expect it to do something. It's basically an buffer that will take whatever code it's told to generate, compile it exactly how you tell it to, and then output it wherever the shortcode is placed.
Take a look at the Shortcode API for more information.
As for your specific example, you don't actually have anything in your shortcode that says "do something with posts 358 and 328".
You'll need to make use of something like WP_Query()
or get_posts()
inside your shortcode. You may also consider using a more unique name for your function and shortcode than post_link_shortcode
and featured_posts
to avoid naming conflicts.
Take this for example:
add_shortcode( 'stickers_featured_posts', 'stickers_featured_posts_function');
function stickers_featured_posts_function( $atts )
extract( shortcode_atts( array(
'ids' => '',
), $atts ) );
// Remove whitespace from IDs
// ex: '123, 321' => '123,321'
$ids = preg_replace('/s+/', '', $ids);
// Turn string of ID's into array
// ex: '123,321' => array(123, 321);
$id_array = explode( ',', $ids );
$args = array(
'post__in' => $id_array
);
$featured_query = new WP_Query( $args );
if( $featured_query->have_posts() )
echo '<ul>';
while( $featured_query->have_posts() )
$featured_query->the_post();
echo '<li>' . get_the_title() . '</li>';
echo '</ul>';
wp_reset_postdata();
else
echo 'Post IDs not found';
And the usage would be like [stickers_featured_posts ids="123,321"]
- The final output would be whatever you put in the while
loop, in the case about - a simple list of post titles.
edited Aug 12 at 3:23
answered Aug 11 at 22:31
Xhynk
647416
647416
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fwordpress.stackexchange.com%2fquestions%2f311151%2fpass-shortcode-variables-to-template%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password