SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  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
    733
    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
    733
    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
    733
    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]"); 
    ?>

  7. #7
    SitePoint Member
    Join Date
    Dec 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is the shortcode works like this [this shortcode], it will be converted to whatever the shortcode represents.

    example: [shortcode 1] will display a form
    [shortcode 2] will display a image
    [shortcode 3] will display link

    whatever is in the [ ] must be a shortcode with a function already written. So if I use my example line from the other post: <?php echo do_shortcode('[shortcode content 1]'); ?> [shortcode content 1] would display. The [ ] have to also show on the page so the script knows it's a shortcode.

    I sure hope all that makes sense.

    Thanks,

    Jeff

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    733
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    It doesn't make sense, but that's okay.

    if $n is 1, wouldn't the line echo
    do_shortcode("[content 1]");

    Maybe it's the full quotes "[content 1]"? I don't know.

    You could also wrap these lines in IF statements like
    PHP Code:
    <?php
    $cnt 
    2//Number of shortcodes
    $n mt_rand(1,$cnt);
          
    if(isset(
    $n) && $n==1){
        echo 
    do_shortcode('[content 1]');
    }elseif(isset(
    $n) && $n==2){
        echo 
    do_shortcode('[content 2]'); 
    }else{
        echo 
    do_shortcode('[content 1]');
    }
    ?>

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

    That was it, the quotes. It worked, thank you for the help! This was driving me nuts.

    Thanks again,
    Jeff


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
  •