Reputation: 71
How to create a function in a custom page and use it in single.php
?
I have the custom page mypage.php
<?php
/* Template Name: Form */
...
$myname = add_post_meta( $date, 'My Name', $name, true );
function name(){
if ($myname != ''){
echo ="Hello World! ";
}
}
get_header(); ?>
...
?>
Single page
<? php name(); ?>
Upvotes: 1
Views: 52
Reputation: 26065
To have functions available in all theme template files, you need to put them inside the file /wp-content/themes/your-theme/functions.php
.
Just move your function to it, and call it in any template (single, page, archives, category). Check the documentation: Functions File Explained.
And to make the logic of your code work, you'd need to use get_post_meta
in the function:
function name(){
$myname = get_post_meta('My Name');
if ($myname != ''){
echo "Hello World! ";
}
}
PS: it's a bit strange that you're setting a post meta everytime the mypage.php
template is loaded...
Upvotes: 2