最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Add a variable in functions.php and call it in single.php

programmeradmin3浏览0评论

I am new to PHP and am building a WordPress theme. I would like to add a variable in the functions.php and use it in single.php as an if statement. At present, my single.php has the following code -

<?php 
  $prevPost = get_previous_post();
  $prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'keepreading-thumb');

  if ( $prevPost ) : ?>
    <?php previous_post_link('%link',"$prevthumbnail"); ?>
    <h3><?php previous_post_link('%link'); ?></h3>
    <p><?php echo excerpt(25); ?></p>
  <?php endif; 
?>

I would like to have the $prevPost and $prevthumbnail variables to be defined in functions.php and would like to use them in an "if statement" in single.php.

P.S - I am a newbie in coding and would appreciate it if you can give me an answer in detail. Thank you...

I am new to PHP and am building a WordPress theme. I would like to add a variable in the functions.php and use it in single.php as an if statement. At present, my single.php has the following code -

<?php 
  $prevPost = get_previous_post();
  $prevthumbnail = get_the_post_thumbnail($prevPost->ID, 'keepreading-thumb');

  if ( $prevPost ) : ?>
    <?php previous_post_link('%link',"$prevthumbnail"); ?>
    <h3><?php previous_post_link('%link'); ?></h3>
    <p><?php echo excerpt(25); ?></p>
  <?php endif; 
?>

I would like to have the $prevPost and $prevthumbnail variables to be defined in functions.php and would like to use them in an "if statement" in single.php.

P.S - I am a newbie in coding and would appreciate it if you can give me an answer in detail. Thank you...

Share Improve this question asked Jun 21, 2014 at 6:32 AjitAjit 111 silver badge2 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 2

either use global variable in functions.php:

    global $prevPost;
    $prevPost = 10;

in single.php :

    global $prevPost;
    if($prevPost)
     .. . . . Your code continues. .

Second method: define a function in functions.php and call that in single.php in functions.php :

    function func_prevPost(){
     $prevPost = 10;
     return $prevPost;
    }

in single.php :

    if(func_prevPost()){
    // your code here
    }

There are different ways to do that! This is easiest way:

Add your variables to a php files (e.g. config.php) and include it whenever you want to use its variables.

include('config.php');

Another easy way is using $_GET or $_POST variables.

$_GET['my_var']='123';

You can also define a global variable in functions.php.

global $my_var;
$my_var='123';

and then add global $my_var; before using it in single.php.

If I understand your question correctly, this code will only be used in single.php. Honestly I will stick with that code in single.php. Don't move it to functions.php and create a global varaible.

Globals might be useful at times, but I would advice you against adding variables to the global scope, unless you are using it itside a class or unless it is a really really necessarry. Globals in classes will only be accesible by that particular class.

Just a tip, if you ever add anything to the global scope, use names that no one will ever think of, never use names that will be easily replicated, as this might lead to a clash later on with plugins, even wordpress.

I was looking for having a common space/ file where i can put name of the website and email address and use that variable across different pages. I found a plugin on wordpress which does exactly the same. https://wordpress.org/plugins/custom-global-variables/

发布评论

评论列表(0)

  1. 暂无评论