Rick Skeels
Rick Skeels

Reputation: 513

Print a value in wordpress within a page

hello i am trying to print a value from the URL within a page on a WordPress site

i have edited the header.php and entered the following code

<?php
$town = rawurldecode( $_GET['town'] ); ?>

on the page i want to print the value i added

<?php print $town ?>

I have tried a few plugins that ment to allow php in the pages but i get errors

the url looks like this

website.co.uk/page/?town=abc

The results i was trying for was to use the PHP tag when i needed it within the page

for example

Hello i live in <?php print $town ?>, would you like to 
come see the football in <?php print $town ?>?.

Did you know that <?php print $town ?> is not that far?

Upvotes: 1

Views: 317

Answers (2)

Got The Fever Media
Got The Fever Media

Reputation: 760

You could always try to use get_query_var()

Like so:

 $town = get_query_var('town');
 echo $town; 

Upvotes: 1

Joran Den Houting
Joran Den Houting

Reputation: 3163

Change

$town = rawurldecode( $_GET['town'] );

to

if(isset($_GET['town'])) { $town = rawurldecode( $_GET['town'] ); } else { $town = "No town"; }

And change

<?php print $town ?>

to

<?php echo $town; ?>

Maybe this will work, please let us know the result.

Upvotes: 0

Related Questions