- This topic is empty.
-
AuthorPosts
-
December 8, 2014 at 1:39 pm #190335
clint_wcbi
ParticipantHello 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') && (ot_get_option('town1_variation') == 'tv_2') || is_page('$town2') && (ot_get_option('town2_variation') == 'tv_2') || is_page('$town3') && (ot_get_option('town3_variation') == 'tv_2') || is_page('$town4') && (ot_get_option('town4_variation') == 'tv_2') || is_page('$town5') && (ot_get_option('town5_variation') == 'tv_2') || is_page('$town6') && (ot_get_option('town6_variation') == 'tv_2') || is_page('$town7') && (ot_get_option('town7_variation') == 'tv_2') || is_page('$town8') && (ot_get_option('town8_variation') == 'tv_2') || is_page('$town9') && (ot_get_option('town9_variation') == 'tv_2') || is_page('$town10') && (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
December 8, 2014 at 3:28 pm #190343clint_wcbi
ParticipantUPDATE: All I needed to do was remove the ” around town#s and it worked.
December 9, 2014 at 10:50 am #190441clint_wcbi
ParticipantHey BenWalker,
Yes, if you could show me your approach with arrays it would be greatly appreciated.
Thanks.
December 9, 2014 at 12:06 pm #190448Anonymous
InactiveTurns out arrays aren’t needed, but iteration does reduce a lot of your complexity.
The following is (I think) functionally identical:
December 9, 2014 at 12:20 pm #190449clint_wcbi
ParticipantMuch MUCH cleaner & functionally identical. THANK YOU. The only question I have is, why did you start with “$variation = false;”?
December 9, 2014 at 4:40 pm #190464Anonymous
InactiveIf 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.
-
AuthorPosts
- The forum ‘Back End’ is closed to new topics and replies.