SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Php - rotation of shortcodes

    I have what I think is a simple question but for some reason using any type of ararys won't work, at least as I tried.

    I have two or three short codes I want to rotate using php but I'm not sure how to get only one to show at a time and what options I have for rotating.

    Here are the lines of code

    <?php echo do_shortcode('[content 1]'); ?>
    <?php echo do_shortcode('[content 2]'); ?>

    I just want one of them at a time to show up on the page. Do you know how I can do that?

    Thank you for your help,

    Jeff

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Never worked with shortcode before but for standard arrays you could do something like this. You probably could adapt to shortcode.

    PHP Code:
    <?php
    $do_shortcode 
    = array(
    "content1" => "Hello there.",
    "content2" => "Hi there.",
    "content3" => "Ho there."
    );
    $cnt count($do_shortcode);
    $n mt_rand(1,$cnt);
    $key "content$n";
    ?>

    <html>
    <body>
    <?php echo "{$do_shortcode[$key]}"?>
    </body>
    </html>

  3. #3
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Drummin,

    Thanks for the code. That is what I have used in the past but with shortcodes I cannot get it to work. The php code I showed in the original post works great, but is will show both at the same time. I need only one to show and for them to be rotated.

    I have been going nuts with this, it cannot be that difficult, I must be overlooking something.

    Thanks again for the help, does anyone have any other suggestions?

  4. #4
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Can't you generate the number and add it to the key? Assuming they are numbered "content 1", "content 2" etc.

    PHP Code:
    <?php
    $cnt 
    2//Number of shortcodes
    $n mt_rand(1,$cnt);
    ?>

    <html>
    <body>
    <?php echo do_shortcode("[content $n]"); ?>
    </body>
    </html

  5. #5
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Drummin,

    Can you show me how you would use

    Code:
    <?php echo do_shortcode('[shortcode content 1]'); ?>
    <?php echo do_shortcode('[shortcode content 2]'); ?>
    With your second suggestion. I think you are thinking on the lines of what I thought but I cannot get the two shortcodes to work in the code.

    Could you please write it out the way it would be on the page.

    Thank You,

    Jeff

  6. #6
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Can you replace the two lines you have with this?
    PHP Code:
    <?php
    $cnt 
    2//Number of shortcodes
    $n mt_rand(1,$cnt);
    echo 
    do_shortcode("[content $n]"); 
    ?>


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •