Pass shortcode variables to template

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite
1












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?







share|improve this question




























    up vote
    2
    down vote

    favorite
    1












    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?







    share|improve this question
























      up vote
      2
      down vote

      favorite
      1









      up vote
      2
      down vote

      favorite
      1






      1





      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?







      share|improve this question














      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?









      share|improve this question













      share|improve this question




      share|improve this question








      edited Aug 11 at 23:50

























      asked Aug 11 at 19:21









      Stickers

      163111




      163111




















          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.






          share|improve this answer






















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "110"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            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






























            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.






            share|improve this answer


























              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.






              share|improve this answer
























                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.






                share|improve this answer














                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.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 12 at 3:23

























                answered Aug 11 at 22:31









                Xhynk

                647416




                647416






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    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













































































                    這個網誌中的熱門文章

                    How to combine Bézier curves to a surface?

                    Carbon dioxide

                    Why am i infinitely getting the same tweet with the Twitter Search API?