Forum Replies Created
-
AuthorPosts
-
masonifyMember
Using your example above and the OPs example you could hook your custom html into an area of WP by doing the following.
You would still need to know how to close php tags or echo html to do so though.
add_action( 'genesis_entry_content', 'cab_my_page_function' ); function cab_my_page_function() { $myVariable = 'Hello World'; echo '<h1>' . $myVariable . '</h1>'; }
This would display the Header "Hello World" in genesis_entry_content area as defined by WP, which I believe is right before each post's content area, below the post header meta and title.
the first variable in add_action which is 'genesis_entry_content' defines the hook that you want to insert your code into defined by the second variable which calls the function that displays the html in this case.
masonifyMemberAs this question is in and of itself one of the most basic things you can learn in php, my advice would be to google learning php or something similar and read one of the tens of thousands of results that would probably pop up.
masonifyMemberThe instructions are correct for the question.
? How to add html markup into a php script.
How and where to display the html markup in the site is a separate issue, and outside the scope of the question.
You may be correct, they may want to also place it somewhere specific, but that is an assumption, and not what was asked.
placing the content could be done with careful placement of the html in the php files, CSS positioning and using hooks in WP. Usually placing html in wordpress is defined through a combination of all of these things.
That being said, explaining hooks to this user would not have explained his example.
Your example above with Hello World at the top of the page is correctly placing the html within the php script, it's just not explicitly place anywhere specific and so appears at the top of the page as per where you inserted the echo statement.
masonifyMemberFor your example, the last three examples are best to work with and learn, because you can easily read the code, and move the html and php elements around, as each element exists on a separate line.
Notice how much easier it is to read also.
To add a single variable in lots of html you might:
Lots of html up here <html stuff> <a href="<?php echo $variableThatHoldsLink; ?>">My Account</a> <more html stuff> Lots of html down here
So we opened php briefly to insert a variable.
If you are inserting small portions of html into php like you stated, it's best to use the last three examples in above post, and never close php off.
masonifyMemberHello Tmgale,
I'll answer you below.
There are a few ways to get a few of the things you mentioned done, and your example is kinda jumping from one to another, but here we go.
In your first example:
<?php /** * Template Name: Practice */ <h1> hello, world </h1> genesis();
You could achieve the html inserted by doing the following (as mentioned above)
<?php /** * Template Name: Practice */ ?> <h1> hello, world </h1> <?php genesis();
So all we did above is add the php close tag before the html we want to add, and re-open it after the html.
That being said, we could not add an argument or variable inside the H1 tags without doing more.. You couldn't just add a variable there because we used the php close tag before the html, so the PHP server side processor won't look for scripts outside of the php tags, only between them... like an html tag of h1 will only apply Header to things between the tags.
So if we wanted the H1 tag to be a php variable using the same system as above, we would do this:
<?php /** * Template Name: Practice */ ?> <h1><?php echo "hello, world"; ?></h1> <?php genesis();
so notice now, that we closed php processing after the template name comment, put in the html element H1, opened php again to echo the variable or whatever php you want here (in this case it's not a variable, but the exact text you had in the original example, except echoed from php), then closed php again to add the closing H1 tag, then opened php again to call the genesis function.
To produce the same results again, except using a variable we would:
<?php /** * Template Name: Practice */ $myVariable = "hello, world"; ?> <h1><?php echo $myVariable; ?></h1> <?php genesis();
All of these examples so far produce the same result, notice in the last, we set a variable to hello world first, then called it within the php tags inside the H1 tags.
That's one way to add html elements, and a nice way to do so if you are adding a whole page of html within your .php file.
If you only need to add H1 header tags around a variable, closing php out is messy and not best practice. You are better to echo small portions of html like so:
<?php /** * Template Name: Practice */ echo '<h1>'; echo 'hello, world'; echo '</h1>'; genesis();
OR google string operators to understand that the following is also the same:
<?php /** * Template Name: Practice */ echo '<h1>' . 'hello, world' . '</h1>'; genesis();
or
<?php /** * Template Name: Practice */ $myVariable = 'hello, world'; echo '<h1>' . $myVariable . '</h1>'; genesis();
To finish, the problem with your last example is your echo statement needs to be followed with the string you want to echo enclosed in quotes (single or double), you can google the difference, and why and when to use either, but for now you can use either. You also forgot to end the php line with the semi-colon which tells the php processor to process that line as a single line of code. Both will produce errors.
Hope that helps
-
AuthorPosts