Community Forums › Forums › Archived Forums › Design Tips and Tricks › Adding HTML markup to page template
- This topic has 10 replies, 4 voices, and was last updated 8 years, 5 months ago by masonify.
-
AuthorPosts
-
November 10, 2015 at 9:22 am #170658TmgaleMember
Hi all,
Just having a play around trying to add html markup into a custom page template.
<?php /** * Template Name: Practice */ <h1> hello, world </h1> genesis();
I recieve the error ' Parse error: syntax error, unexpected '<'
I thought I would be able to add html straight into the template, anyone know why its not working?
Thanks
November 10, 2015 at 9:25 am #170662PorterParticipantYou're adding html within a php tag. You would either want to:
Close the php tag with ?>, add the html, open the PHP tag again with <?php
or
Echo the html (I believe this works, not my forte) using echo('your html here');
November 10, 2015 at 9:33 am #170663carasmoParticipantThere are many examples in the wild:
http://designsbynickthegeek.com/tutorials/creating-page-templates-in-genesis
Hooks and snippets:
http://my.studiopress.com/snippets/
http://wpsites.net/wordpress-themes/new-genesis-2-0-loop-hooks-how-to-use-them/<?php /** * Template Name: Test page */ //* Remove the breadcrumb navigation // remove_action( 'genesis_before_loop', 'genesis_do_breadcrumbs' ); //* Remove the post info function remove_action( 'genesis_entry_header', 'genesis_post_info', 12 ); //* Remove the author box on single posts remove_action( 'genesis_after_entry', 'genesis_do_author_box_single', 8 ); //* Remove the post meta function remove_action( 'genesis_entry_footer', 'genesis_post_meta' ); //Remove featured image remove_action( 'genesis_entry_content', 'featured_post_image', 8 ); // this hooks in to the genesis_entry_content -- it adds content after page text (if any) add_action( 'genesis_entry_content', 'cab_my_page_function', 15 ); function cab_my_page_function() { // custom stuff here }// end function genesis();
November 10, 2015 at 9:34 am #170664TmgaleMemberI tried to use echo but still recieved the same error. I was under the impression that you could input html like such withing a php doc;
<h1><?php $arg ?></h1> or just plain <h1> hello </h1>
Anyway, I readjusted the code as follows but still recieve the error;
<?php /** * Template Name: Practice */ ?> <?php echo <h1> Hello, World </h1> genesis(); ?>
November 10, 2015 at 9:39 am #170667TmgaleMemberCarasmo, I am specifically trying to html markup into a page template. The turtorials you have provided are great, but dont say how you would do this.
It seems when I try to add it into any template I get an error back, sure is strange.
Thanks
November 10, 2015 at 10:55 am #170690masonifyMemberHello 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
November 10, 2015 at 11:12 am #170693masonifyMemberFor 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.
November 10, 2015 at 1:27 pm #170708carasmoParticipantWhen I follow these instructions, I get "Hello world" but it appears before the site:
That's why all the instructions I've seen have a function and a hook.
I think the OP wants to do something like a landing page. Here's some very good instructions on that:
http://www.billerickson.net/full-width-landing-pages-in-genesis/
November 10, 2015 at 1:39 pm #170709masonifyMemberThe 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.
November 10, 2015 at 1:40 pm #170710masonifyMemberAs 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.
November 10, 2015 at 1:47 pm #170711masonifyMemberUsing 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.
-
AuthorPosts
- The forum ‘Design Tips and Tricks’ is closed to new topics and replies.