WordPress.org

Support

Support » How-To and Troubleshooting » PHP Coding to show different elements on different posts

PHP Coding to show different elements on different posts

  • Hi there,

    I’m not too advanced at coding, but I know enough to be dangerous haha. I am trying to write a piece of code that will show the same button but a different embedded link for each room type page (using Hotec theme building a website for a hotel client).

    I know I can use the <? php if(is_page(0000)) string to embed the code, but I need to designate a different link for about 6 different pages.

    Could anyone help me get the code right so this works how I’m imagining?

    Here’s the attempt I currently made, that is not correct, because it is embedding the same link across all singular room type pages:

    <?php elseif(is_singular('room')): ?>
    
            <div class="page-title-wrapper">
                    <h1 class="page-title left"><?php echo $title; ?></h1>
                    <div class="page-title-alt right">
                        <?php
                        // show button in top title
                        if(st_get_setting('show_room_res_btn','y')!='n'){
                             echo '
    <?php if(is_page(2057)) { ?>
    <a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
    }
    ?>
    </div>

    [Please post code & markup between backticks or use the code button. Your posted code may now have been permanently damaged by the forum’s parser.]

    What I’d like to ideally have, is the ability to designate a different link for each different “singular room page”…

    Thanks in advance!

Viewing 14 replies - 1 through 14 (of 14 total)
  • Here is the kind of structure to use multiple ‘if’ statements:

    <?php
             // show button in top title
             if(st_get_setting('show_room_res_btn','y')!='n') :
                if (is_page(2057)) :
                   echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
                elseif (is_page(0001)) :
                   // echo for page 0001
                elseif (is_page(0002)) :
                   // echo for page 0002
                endif;
             endif;
             ?>

    Thank you so much! I tried it, but it doesn’t seem to be working properly. When I add in the below code, it actually removes the entire content of the page, so something must be conflicting. Here is what I have, would you mind taking a look?

    <?php
    // show button in top title
    if(st_get_setting(‘show_room_res_btn’,’y’)!=’n’) :
    if(is_page(2057)) {
    echo ‘‘.st_get_setting(‘room_res_btn_txt’,__(‘Book this room’,’smooththemes’)).’‘;
    }elseif(is_page(1735)) {
    echo ‘‘.st_get_setting(‘room_res_btn_txt’,__(‘Book this room’,’smooththemes’)).’‘;
    }elseif(is_page(1734)) {
    echo ‘‘.st_get_setting(‘room_res_btn_txt’,__(‘Book this room’,’smooththemes’)).’‘;
    }
    endif;
    endif;
    ?>

    Oops, just realized I had brackets in there instead of colons, but I tried both and both are causing the same problem…thanks!

    Here’s what’s currently in the site, and you can see that the book this room button isn’t showing up at all now, but I feel we are getting closer to a solution….

    <?php
                    // show button in top title
     		if(st_get_setting('show_room_res_btn','y')!='n') :
    			if(is_page(2057)) :
    			echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
    		       elseif(is_page(1735)) :
    echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
    		       elseif(is_page(1734)) :
    echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=K1R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
                endif;
             endif;
    ?>
    </div>

    [code fixed by moderator]

    Please repost the code and be SURE to enclose it in backticks.

    Based on what you posted earlier, I think your echo lines need to look something like this:

    echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';

    I think each line will need a different value for the ‘Hotel’, ‘Chain’, and ‘room’ parameters.

    Ahh, ok it may not be doable then. The Hotel & Chain parameters have to remain the same, whereas the Room parameters are what changes for each link.

    Here’s the code I just added, and it’s not breaking the pages anymore but the button isn’t showing up at all. It might be a lost cause!

    <?php
                    // show button in top title
     		if(st_get_setting('show_room_res_btn','y')!='n') :
    			if(is_page(2057)) :
                             // echo for page 2057
                             echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
    		         elseif(is_page(1735)) :
                             // echo for page 1735
                             echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
                             elseif(is_page(1734)) :
                             // echo for page 1734
                             echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=K1R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
                endif;
             endif;
    ?>


    [code fixed by moderator]

    Thanks for getting back so quickly!

    Hmm…it’s strange that when I post the code, the URLs aren’t showing up at all but are embedded in the above “.st” line… not sure if that’s normal or what. Do you think something within this line: ”.st_get_setting(‘room_res_btn_txt’,__(‘Book this room’,’smooththemes’)).”; needs to be changed for each different page?

    Your code is STILL getting messed up because it is not enclosed in backticks or code tags.

    • Put the cursor where you want to insert the code.
    • Click the code button at the top of the entry box.
    • Insert your code.
    • Click the /code button.
    WPyogi

    @wpyogi

    Forum Moderator

    EDIT – cross-posted…

    Please always use the code buttons when posting code on these forums – http://codex.wordpress.org/Forum_Welcome#Posting_Code

    Thanks to both of you, like I mentioned, I’m not super familiar with all this, I know only enough to be dangerous ha! Thanks for moderating.

    I don’t see anything wrong in the code. Try adding one more line to the elseif list to show a default button or message:

    <?php
    // show button in top title
    if(st_get_setting('show_room_res_btn','y')!='n') :
       if(is_page(2057)) :
          // echo for page 2057
          echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
       elseif(is_page(1735)) :
          // echo for page 1735
          echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
       elseif(is_page(1734)) :
          // echo for page 1734
          echo '<a href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=K1R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
       else :
          // echo for none of the above
          echo "Wrong Page: $post->ID";
       endif;
    endif;
    ?>

    That will show whether or not the page number is being picked up correctly.

    Thanks again, I will test this out and see how it performs.

    I finally figured it out! Thank you so much for all your help. It was simply the (is_page) statement that was throwing things off, since each room type is actually a custom post type and not a page. By changing this, it fixed itself.

    <?php
            // show button in top title
            if(st_get_setting('show_room_res_btn','y')!='n') :
    		if(is_single('2057')) :
                    // echo for page 2057
                    echo '<a class="btn book_this_room" href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2RF&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
    		  elseif(is_single('1735')) :
                      // echo for page 1735
                      echo '<a class="btn book_this_room" href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=Q2R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
                      elseif(is_single('1734')) :
                      // echo for page 1734
                      echo '<a class="btn book_this_room" href="https://gc.synxis.com/rez.aspx?Hotel=11618&Chain=8559&room=K1R&start=availresults">'.st_get_setting('room_res_btn_txt',__('Book this room','smooththemes')).'</a>';
                endif;
             endif;
    ?>
Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘PHP Coding to show different elements on different posts’ is closed to new replies.
Skip to toolbar