Run PHP function from HTML hyperlink
-
Hi all,
I’m trying to run a function to alter the post_type in my wordpress query string (or to add a new post type altogether). The function I’m using comes from here: https://www.addedbytes.com/blog/code/php-querystring-functions/
Based on the examples in this thread, I’ve written a sidebar that looks like this:
<?php //add query to URL function add_querystring_var($url, $key, $value) { $url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&'); $url = substr($url, 0, -1); if (strpos($url, '?') === false) { return ($url . '?' . $key . '=' . $value); } else { return ($url . '&' . $key . '=' . $value); } } ?> <?php if (isset($_GET['run'])) $linkchoice=$_GET['run']; else $linkchoice=''; switch($linkchoice){ case 'showAll' : add_querystring_var($url, 'post_type', ''); break; case 'showAnimation' : add_querystring_var($url, 'post_type', 'animation'); break; case 'showPeople' : add_querystring_var($url, 'post_type', 'people'); break; case 'showStudios' : add_querystring_var($url, 'post_type', 'studios'); break; case 'showResources' : add_querystring_var($url, 'post_type', 'resources'); break; case 'showWiki' : add_querystring_var($url, 'post_type', 'wiki'); break; default : ; } ?> <h2>Show... </h2> <a href="?run=showAll">All</a> <a href="?run=showAnimation">Animation</a> <a href="?run=showPeople">People</a> <a href="?run=showStudios">Studios</a> <a href="?run=showResources">Resources</a> <a href="?run=showWiki">Wiki</a><br /><br />
When I click any of these links, they take me to “mywebsite.com/?run=showX” – which in turn directs me to my front page. I think this means that the function is not running, and I’m wondering if anyone has any suggestions as to why. I’ve tried testing without the function, and with just echo functions generating text to indicate that a function has ran – and these haven’t done anything either. I still am directed to the url indicated in the link and nothing beyond that seems to happen. Have I set the function to run incorrectly?
Thanks in advance for any help.
- The topic ‘Run PHP function from HTML hyperlink’ is closed to new replies.