admin管理员组

文章数量:1429151

I'm new to php and I know some basic stuff about echoing and stuff but I would really like to know how to do the following thing:

<div id="<?php echo get_the_ID(); ?>" class="project-wrapper">

I now want to store the div id in a php variable so that I can echo it elsewhere later. Is that possible? :)

Thanks!

(I use wordpress, if that is relevant!)

I'm new to php and I know some basic stuff about echoing and stuff but I would really like to know how to do the following thing:

<div id="<?php echo get_the_ID(); ?>" class="project-wrapper">

I now want to store the div id in a php variable so that I can echo it elsewhere later. Is that possible? :)

Thanks!

(I use wordpress, if that is relevant!)

Share Improve this question asked Sep 9, 2015 at 9:11 user2432661user2432661 1
  • 1 you can use session for that i think – guradio Commented Sep 9, 2015 at 9:15
Add a ment  | 

4 Answers 4

Reset to default 3

Yes you can store it in a varibale and reuse it.

<?php
$divId=get_the_ID();
?>
<div id="<?php echo $divId; ?>" class="project-wrapper">

And you can use the variable $divId anywhere in the page.

And if you want to use it in any other pages then you have to put it in any session variable and use that .

<?php
    session_start();
    $_SESSION["divId"]=get_the_ID();
    ?>
    <div id="<?php echo $_SESSION["divId"]; ?>" class="project-wrapper">

You can use the variable $_SESSION["divId"] anywhere in your website.

You need to store your data in the session, here : $_SESSION['myId'] = get_the_ID();

then when you need it, just

echo $_SESSION['myId']

please note that to use session, you must have a session already started by using before any line of code. It is probably already running for wordpress. To start it manually if it is not the case : session_start();

If you want to Use the Id in current template you can use local variable, if you want to use it in other template file use Session Variable, ans if you are going to do some jQuery/JavaScript manipulation if would Suggest use a static id instead of dynamic id

Eg. <div id="static-id"> instead of <div id="dynamic-<?php echo get_the_ID(); ?>">

as you have to write different event for each id in the dom for same element.

and if you are using just id="<?php echo get_the_ID(); ?>" i.e id of the post/page you can call it any where in the template using <?php echo get_the_ID(); ?> it will give the same value

You can definitely do this. See the code below.

            <?php
            // Creation of a php variable and assigning a value to it.
            $var1="division_id1";
            ?>
            <html>
                <head>
                    <title>Demo</title>
                </head>
                <body>
                <div id="<?php echo $var1; ?>">
                    Division 1 : Here the id is "division_id1"
                </div>
                </body> 
            </html> 

In this the ID is changed to the variable content that you have assigned.. Good Luck..

本文标签: javascriptStoring div id in a php variableStack Overflow