Derbyshire10 Derbyshire10 - 7 months ago 12
PHP Question

php if function - wordpress category

how can I get on WordPress only one of the weather widgets to show up depending on the category page the user is on.

<?php
if ($current_category == Cheshire)?>{
echo <div id="c_022b685c6534fb12881f4e5a78e6f32a" class="ancho">
<h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
<a href="http://www.forecast.co.uk/northwich.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">
The weather for Cheshire</a></h2>
<div id="w_022b685c6534fb12881f4e5a78e6f32a" class="ancho" style="height:100%"></div></div>
<script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/022b685c6534fb12881f4e5a78e6f32a"></script>
}
if ($current_category == Cumbria)?>{
echo <div id="c_b5e9e66c4515d3c91adb4511543aaab0" class="ancho">
<h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
<a href="http://www.forecast.co.uk/penrith.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">The weather for Cumbria</a></h2><div id="w_b5e9e66c4515d3c91adb4511543aaab0" class="ancho" style="height:100%"></div></div>
<script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/b5e9e66c4515d3c91adb4511543aaab0"></script>
}
if ($current_category == Greater Manchester)?>{
echo <div id="c_a8d03c62bcc860b39aa47992fcd54ae5" class="ancho">
<h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
<a href="http://www.forecast.co.uk/manchester.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">
Weather Greater Manchester</a></h2><div id="w_a8d03c62bcc860b39aa47992fcd54ae5" class="ancho" style="height:100%"></div></div>
<script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/a8d03c62bcc860b39aa47992fcd54ae5"></script>
}
if ($current_category == Lancashire)?>{
echo <div id="c_8fd3a3784f742d28d268d45af5391a20" class="ancho">
<h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
<a href="http://www.forecast.co.uk/preston.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">The weather in Lancashire</a></h2>
<div id="w_8fd3a3784f742d28d268d45af5391a20" class="ancho" style="height:100%"></div></div>
<script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/8fd3a3784f742d28d268d45af5391a20"></script>
}
if ($current_category == Merseyside)?>{
echo <div id="c_42969f258646ac6c3f4e4a53ac5cfd36" class="ancho">
<h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
<a href="http://www.forecast.co.uk/liverpool.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">
The weather for Merseyside</a></h2>
<div id="w_42969f258646ac6c3f4e4a53ac5cfd36" class="ancho" style="height:100%"></div></div>
<script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/42969f258646ac6c3f4e4a53ac5cfd36"></script>
else {
echo ""
}
}
?>


I want it to only load when on them pages no other category page that isn't a place? can anyone help, what have I done wrong?

Answer

It looks like you are not properly formatting your PHP as well as enclosing your code within tags.

Incorrect:

if ($current_category == Cumbria)?> {
    echo <div id="c_b5e9e66c4515d3c91adb4511543aaab0" class="ancho">
    <h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
    <a href="http://www.forecast.co.uk/penrith.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">The weather for Cumbria</a></h2><div id="w_b5e9e66c4515d3c91adb4511543aaab0" class="ancho" style="height:100%"></div></div>
    <script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/b5e9e66c4515d3c91adb4511543aaab0"></script>
}

Correct:

<?php if ($current_category == "Cumbria")?> { ?>
    <div id="c_b5e9e66c4515d3c91adb4511543aaab0" class="ancho">
    <h2 style="color: #000000; margin: 0 0 3px; padding: 2px; font: bold 13px/1.2 Verdana; text-align: center; width=100%">
    <a href="http://www.forecast.co.uk/penrith.html" style="color: #000000; text-decoration: none; font: bold 13px/1.2 Verdana;">The weather for Cumbria</a></h2><div id="w_b5e9e66c4515d3c91adb4511543aaab0" class="ancho" style="height:100%"></div></div>
    <script type="text/javascript" src="http://www.forecast.co.uk/widget/loader/b5e9e66c4515d3c91adb4511543aaab0"></script>
<?php } ?>

Notice the quotes around the city name as well as how I have wrapped the PHP code with the proper tags. This will ensure the PHP code executes properly within the HTML.

Comments