This code is very simple so I feel like it could be shortened by relating the php variable to the jquery function parameter. if you have any ideas开发者_StackOverflow中文版, please help. thank you
<script type="text/javascript">
<?php if (is_page($toppageID1)) { ?>
$(document).ready(function () {
Show('1');
});
<?php } elseif (is_page($toppageID2)) { ?>
$(document).ready(function () {
Show('2');
});
<?php } elseif (is_page($toppageID3)) { ?>
$(document).ready(function () {
Show('3');
});
<?php } elseif (is_page($toppageID4)) { ?>
$(document).ready(function () {
Show('4');
});
<?php } elseif (is_page($toppageID5)) { ?>
$(document).ready(function () {
Show('5');
});
<?php } elseif (is_page($toppageID6)) { ?>
$(document).ready(function () {
Show('6');
});
<?php } elseif (is_page($toppageID7)) { ?>
$(document).ready(function () {
Show('7');
});
<?php }; ?>
</script>
Have something like $somevalue
that would help to switch instead of elseif
.
<script>
<?php switch($somevalue){ ?>
$(document).ready(
function (){
<?php case 1: //?>
<?php case 2: //?>
<?php case 3: //?>
<?php case 4: //?>
<?php case 5: //?>
}
)
<?php } ?>
</script>
First off I would change that function of yours is_page
to page
and return a string with the ID. You already have access to those $toppageID variables in PHP so why not use them in the function itself. If you absolutely cannot for scoping or other reasons, pass them all in the function at once. You want a single output not executing the function numerous times.
Example script using the re-factored function page
:
<body>
...
<!-- end of body -->
<script type="text/javascript">
(function(id) {
if(id) {
Show(id);
}
else {
// throw some error or something
}
})(<?php echo page() ?>);
</script>
</body>
If you need to funnel all the variables at once maybe something like this:
})(<?php echo page($toppageID1,$toppageID2,$toppageID3,...) ?>);
SOrry if I was unclear but im using wordpress functions. a switch works great.
<?php $currID = get_the_ID(); ?>
$(document).ready(function () {
});
精彩评论