Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Back End Inserting A Variable When Using is_page()

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #190335
    clint_wcbi
    Participant

    Hello I currently am chopping up a theme called Hueman and in the backend of it OptionTree is already built in. This client has hundreds of sites for auto shops and one thing that is not a constant is main & surrounding cities these shops are located by. To accommodate I created 10 fields on the back end where you can type in cities 1-10.

    They have 3 versions of content for the sites they have and in the page.php file I did the following to be able to run the page on the fly without having to physically type in the city in the code (but failed).

    Is there no way to use and include a variable for is_page?

    <?php

    $town1   = ot_get_option('town_1');
    $town2   = ot_get_option('town_2');
    $town3   = ot_get_option('town_3');
    $town4   = ot_get_option('town_4');
    $town5   = ot_get_option('town_5');
    $town6   = ot_get_option('town_6');
    $town7   = ot_get_option('town_7');
    $town8   = ot_get_option('town_8');
    $town9   = ot_get_option('town_9');
    $town10  = ot_get_option('town_10');
    

    if( is_page(‘$town1’) || is_page(‘$town2’) || is_page(‘$town3’) || is_page(‘$town4’) || is_page(‘$town5’) || is_page(‘$town6’) || is_page(‘$town7’) || is_page(‘$town8’) || is_page(‘$town9’) || is_page(‘$town10’) )
    {
    if( is_page($town1) && (ot_get_option(‘town1_variation’) == ‘tv_1’) || is_page(‘$town2’) && (ot_get_option(‘town2_variation’) == ‘tv_1’) || is_page(‘$town3’) && (ot_get_option(‘town3_variation’) == ‘tv_1’) || is_page(‘$town4’) && (ot_get_option(‘town4_variation’) == ‘tv_1’) || is_page(‘$town5’) && (ot_get_option(‘town5_variation’) == ‘tv_1’) || is_page(‘$town6’) && (ot_get_option(‘town6_variation’) == ‘tv_1’) || is_page(‘$town7’) && (ot_get_option(‘town7_variation’) == ‘tv_1’) || is_page(‘$town8’) && (ot_get_option(‘town8_variation’) == ‘tv_1’) || is_page(‘$town9’) && (ot_get_option(‘town9_variation’) == ‘tv_1’) || is_page(‘$town10’) && (ot_get_option(‘town10_variation’) == ‘tv_1’))
    {

        echo
        '<p>'
            echo
            "Content for text variation 1.";
        echo
        '</p>';
    }
    elseif ( is_page('$town1') &amp;&amp; (ot_get_option('town1_variation') == 'tv_2') || is_page('$town2') &amp;&amp; (ot_get_option('town2_variation') == 'tv_2') || is_page('$town3') &amp;&amp; (ot_get_option('town3_variation') == 'tv_2') || is_page('$town4') &amp;&amp; (ot_get_option('town4_variation') == 'tv_2') || is_page('$town5') &amp;&amp; (ot_get_option('town5_variation') == 'tv_2') || is_page('$town6') &amp;&amp; (ot_get_option('town6_variation') == 'tv_2') || is_page('$town7') &amp;&amp; (ot_get_option('town7_variation') == 'tv_2') || is_page('$town8') &amp;&amp; (ot_get_option('town8_variation') == 'tv_2') || is_page('$town9') &amp;&amp; (ot_get_option('town9_variation') == 'tv_2') || is_page('$town10') &amp;&amp; (ot_get_option('town10_variation') == 'tv_2'))
    {
        echo
        '<p>'
            echo
            "Content for text variation 2.";
        echo
        '</p>';
    }
    else 
    {
        echo
        '<p>'
            echo
            "Content for text variation 3.";
        echo
        '</p>';
    }
    

    }

    ?>

    Thanks for any and all help.

    -Clint

    #190343
    clint_wcbi
    Participant

    UPDATE: All I needed to do was remove the ” around town#s and it worked.

    #190441
    clint_wcbi
    Participant

    Hey BenWalker,

    Yes, if you could show me your approach with arrays it would be greatly appreciated.

    http://pastebin.com/TXwYN8FL

    Thanks.

    #190448
    Anonymous
    Inactive

    Turns out arrays aren’t needed, but iteration does reduce a lot of your complexity.

    The following is (I think) functionally identical:

    http://pastebin.com/yYhMyvEq

    #190449
    clint_wcbi
    Participant

    Much MUCH cleaner & functionally identical. THANK YOU. The only question I have is, why did you start with “$variation = false;”?

    #190464
    Anonymous
    Inactive

    If is_page() evaluates to false on all your town options then you don’t want anything to happen. I’m assuming that your variation options will never intentionally be set to false and am using the variable as an indicator for whether we are on a page for one of your towns.

    If the variable were only created within the for loop, my if($variation) statement would produce an error on non-town pages (as $variation wouldn’t exist). Hence setting it to false first and trusting it’ll be changed to something that will evaluate to true if we are on a town page.

    This would normally be handled by a router or a controller, by the way. This sort of coding is messy. A better method would be to find a way to pull the appropriate template in based on the variation – you only really need 3 lines of code (curly braces optional). I’ll leave that solution to your imagination.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Back End’ is closed to new topics and replies.