Load different div at home template in wordpress - php

I am working on a custom wordpress homepage.
I want that it is loading in a different div in the header.
The header now loads the #content div for every page. But i want to let it load #contenthome div if the homepage loads.
I'm using this php-code:
<?php if (is_page_template('template-home.php')) { ?>
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
I'm fairly new to php. so i hope you guys can help me :)
Thanks a lot.

Or this:
if(is_home())
{
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
Personally, I prefer writing things in shorthand when it comes to cases like these. Consider also using this to shorten your code:
<div id="<?php echo is_home() ? 'contenthome' : 'content'; ?>">
ADDITIONAL NOTES: If your <body> tag has the Wordpress body_class() added to it, you can pretty much target individual pages with CSS, even when they're using the same template. The homepage, like any other page, will have a unique body class applied that will then allow you to target that particular page.
So if your issue is simply a matter of being able to target any given page regardless of template with CSS, you won't necessarily NEED to apply any unique divs to your page. For example:
<style type="text/css">
#content{display:block;}
.home #content{display:none;}
</style>
This will hide the content div only on the homepage. It's probably not what you want it to do, but you can see how the page itself is being targeted to override the default behavior.
UPDATE: As per your latest question, if you need another particular page to use that conditional, use is_page() like so:
LONG CODE:
if(is_home() || is_page('posts_page_title'))
{
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>
SHORTHAND:
<div id="<?php echo is_home() || is_page('posts_page_title') ? 'contenthome' : 'content'; ?>">
If you prefer, you may also use is_page('posts_page_ID') or is_page('posts_page_slug'). Play around with it and see what works best for you. More information here: http://codex.wordpress.org/Function_Reference/is_page

With reference to this post Try this one....
$pagename = get_query_var('pagename');
if ($pagename = 'template-home.php') {
<div id="contenthome">
<?php } else { ?>
<div id="content">
<? } ?>

it seems like you already created a file in your theme just for the homepage and you called it template-home.php
from this point you have two options:
1) you can just define a constant (or variable) and decide what div to use only when it is defined; if you will use a variable remember that in header.php you are in fact inside a function (get_header) and you will need to use global to have access to it
2) if you foresee any other differences between the structure of the header between the main page and the rest of your wordpress you could use another file for the header. to do this, in your template-home.php file give a parameter to the get_header function, like get_header('home')
if you do this header-home.php will be loaded instead of header.php

Related

How to display php image gallery inside a div?

My website has two div columns: a vertical navigation menu and main content. I used php to navigate different pages of my website to the main div (similar to this php example)...(eg. index.php?pg=about_us --> get content from /page/about.html). But one of the pages I want to display this gallery (http://sye.dk/sfpg/) on the main div.
How to display my gallery correctly in the main div (installed under /pages/gallery/index.php) (eg. width about 700px)? I have the same problem if the navigation menu is pointed to an external website. (let's say google) The size and charset are not displayed correctly while using div. Thank you.
<?php
// ...blah blah blah
$pgname = isset($_GET['pg']) ? trim(strip_tags($_GET['pg'])) : 'index';
//....
?>
// starts html, header and body
<div class="left_col">
<nav id="navigation">
<ul>
<li>Home</li>
<li>News</li>
<li>Gallery</li>
<li>Donate</li>
<li>About Us</li>
</ul>
</nav>
</div>
<section class="main_col clearfix">
<?php
if ($pgname != 'gallery'){
echo file_get_contents('pages/'. $pgname. '.html');
} else {
echo file_get_contents('http://google.com/'); // this doesn't work, and neither work with '/pages/gallery/index.php'
}
?>
</section>
Simplified, the above becomes:
gallery.php:
<?php
$name = 'gallery'; // Fixed for this example.
$html_gallery = 'pages/'. $name . '.html';
?>
<html>
<section>
<?php include $html_gallery ?>
</section>
</html>
pages/gallery.html:
<img src="/images/foo.jpg">
<img src="/images/bar.jpg">
<img src="/images/baz.jpg">
gallery.php would render much like this:
<html>
<section>
<img src="/images/foo.jpg">
<img src="/images/bar.jpg">
<img src="/images/baz.jpg">
</section>
</html>
So as you can see, it is up to you to style the output.
I like your idea a lot... but I think it would be much easier for you to use JavaScript and AJAX for this. Also, this approach will prevent the page from reloading!
EDIT - So, if you say you have both HTML and PHP files to use, an ext parameter (extension) in your events will do the trick. - EDIT
My idea would be to give an onclick event on each li calling a JavaScript function, let's say onclick="getContent(page, ext)". So of course you need to replace page to whatever string you like, let's say gallery; and ext to any extension you need as a string, let's say php.
Sample result:
<li onclick="getContent('news', 'html')" title="News">News</li>
<li onclick="getContent('gallery', 'php')" title="Gallery">Gallery</li>
Now, let's build our JavaScript-AJAX stuff. What we first need to do is create the function and place it right after the <body> tag inside a <script> tag, of course. Then remember to add an id to your main column, in the following example it will be content.
<script type="text/javascript">
function getContent(pageName, ext){
var url = "pages/"+pageName+"."+ext, // gallery.php - news.html
xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function(){
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("content").innerHTML = xhttp.responseText;
}
};
xhttp.open("GET", url, true);
xhttp.send();
}
</script>
So now this function creates the request and gets the data from your URL and then places all the HTML in it inside your section. Of course, make sure that the HTML file contains only what you need inside the section.
Your main column in HTML should look like this:
<section class="main_col clearfix" id="content"></section>
- EDIT -
About the pre-made single file PHP gallery and resizing problem... I saw the demo and I think I know how it works... my advice is to make sure you set a width to your main_col section because the content given by the demo seems to be lots of div's with a class thumbbox which happens to be arranged by CSS display:inline-block so it should just work fine like that.
But the biggest problem I see is that once you load the content on your page, it will not work unless you include(); (PHP) the file or at least the source code for your single page PHP gallery, because you will only load the HTML and I also see that it uses the JavaScript onclick event just like my idea.
What I can say is that to help you solve this thing entirely, I should be able to see how you're implementing this library and many other things. I think you can work it out tho if you include the file like I said (so that the PHP code loads and hopefully prints the necessary JavaScript).
Also, the charset might be solved using PHP utf8_encode(); or utf8_decode();. Use the first one to encode from ISO-8859-1 to UTF8 and the second one for the other way round.

PHP Reload div's content on link click

I'd like to reload a specific div if you click on a link with PHP.
HTML Code:
<nav>
Register
Login
</nav>
<div class="content">
//Reload Content here
<p>That's a test paragraph for the start page</p>
</div>
And now I want to include the page from the href on click into the div.content
How can I catch a link click in php? Is this possible? And how can I include the page into my div.content?
Thanks!
You might want to look into using Jquery and AJAX if you want to achieve this without a page reload... Otherwise you might want to use a PHP GET request as follows:
<nav>
Register
Login
</nav>
<div class="content">
<?php
if (isset($_GET['content'])) {
if (file_exists($_GET['content'] . '.php')) {
require($_GET['content'] . '.php');
}
}
?>
<p>That's a test paragraph for the start page</p>
</div>
PHP code is executed on the server, and cannot reload a <div> widhout rendering the whole page. If you absolutly want to use PHP to do this, you need to make an <iframe> inside the <div class=content> and reload that.
URL parameters is stored in the $_GET variable.
For example: foo.com?page=bar can be retrieved with $_GET['page']
<?php
echo $_GET['page']; // will return "bar"
?>
Example in pure PHP without <iframe>
<nav>
Register
Login
</nav>
<div class="content">
<?php
if(isset($_GET['page']))
{
// Includes the content. # is for supressing errors if the file is not found.
// Can be solved with is_file();
// http://php.net/manual/en/function.is-file.php
#include "/path/to/file/".$_GET['page'].".php";
}
?>
</div>
This is not the most secure way of doing it, because you can manipulate the URL to include other PHP files in your system, but you get the idea.

Add content to PHP file with PHP / edit PHP file with PHP

I'm building my own CMS system and I want to create new pages dynamicly from a template. Just like in wordpress when you add a new Page.
This is the template:
<?php require_once('backend-nav.php');?>
<div id="main">
<div id="main-content" class="xlarge">
<article id="article-wrapper">
// My content needs to go here!
</article>
<?php require_once('backend-sidebar.php')?>
</div>
</div><!-- End main content container -->
<?php require_once('backend-footer.php')?>
<?php } else {
echo '<div class="container">You have to be logged in to view this page:.
'Login'.'</div>';
}
?>
I have made a form to submit the content that I want on the page, and then use the following code to open the templatet php file and save it as a new file on the server with the content for the page
$doc = new DOMDocument();
$doc->loadHTMLFile("new_page.php");
$article = $doc->getElementById('article-wrapper');
$p = $doc->createElement('p');
$addP = $article->appendChild($p);
$content = $doc->createTextNode($page_content);
$addP->appendChild($content);
// the url for the page is also submitted to the form and later added to the menu, which works.
$doc->saveHTMLFile($page_url.'.php');
Since there is no loadPHP function I'd recon I use this one. It also worked for me when adding the link to my main menu, which is also a PHP file.
Now the content gets added to the file, and is saved accordingly but for some reason it fucks up the code in some places like this, some sign are replaced like ? and > etc.:
require_once('backend-header.php');
?>
<?php if (isset($_COOKIE['username'])) { ?>
<?php require_once('backend-nav.php');?>
<html>
<body>
<div id="main">
<div id="main-content" class="xlarge">
<article id="article-wrapper">
<p>test content added in p tags</p></article>
<?php require_once('backend-sidebar.php')?> </div><!-- End main content container -->
</div>
<?php require_once('backend-footer.php')?><?php } else {
echo '<div class="container">You have to be logged in to view this page: ' .
'Login'.'';
}?></body></html>
the PHP ending tag before the html end tag is replaced
I have also tried fread/write to alter the file but I probably are not using things the right way.
Is there a way to add code to php file with php, or a other way to get what I'm trying to do?
Thanks!
DOMDocument only use to read XML and HTML, these have a structure. When you insert PHP code into html file, it is not realy a html anymore. Let see an example below.
The html code:
<a>text</a>
There is a node that named "a" have a content. DOMDocument can understand it well.
But
<a><?php if (false) : ?>true</a><? else: ?>false</a><?php endif ?>
DOMDocument can not understand php and it will read the first < /a> as the closer of < a>. How about the second one, the reader may try to read by fixing it or just ignore it or append something to make it become structured. So, you can not use DOMDocument in this case. You could try to use file_get_contents and replace the content then use file_put_contents to write it back.

Reusable user interface in PHP?

For most of my projects I make an administration interface, which has the same design for every project. The design of the header, the footer, the topbar, the leftmenu, the css, etc. are always the same. It is a pity to create the views every time; so I was thinking: maybe there would be a nice way to put the admin interface in my MVC library, as it is reused by every project?
But for the moment, in every single view I got code like the following:
<?php $this->_include('/includes/doctype.php'); ?>
<head>
<?php $this->_include('/includes/head.php'); ?>
<title>Some title</title>
</head>
<body>
<?php $this->_include('/includes/topbar.php'); ?>
<div id="page">
<?php $this->_include('/includes/header.php'); ?>
<?php $this->_include('/includes/leftmenu.php'); ?>
<div id="content" role="main">
<h1>Some title</h1>
<p>Blah blah blah.</p>
</div><!-- /#content -->
<?php $this->_include('/includes/footer.php'); ?>
</div><!-- /#page -->
</body>
</html>
Would it be a good idea to extract the custom content from the structure of the interface, and put that structure in my library somehow to make it reusable?
After that how will it be possible to customize the title and the actual menus?
I do this all the time. I have a custom header and footer file that are called at the start and end of every page.
<?PHP
Require("includes/header.php");
...
Require("includes/footer.php");
?>
The header provides a database handle, a datetime string and handles logon, priveleges, logging of pageviews etc.
The footer provides a standard HTML page but includes some systematised variables. It also generates the menu dynamically from the driving database then closes the database connection.
This way when I write code, I don't get mixed up in the HTML and any bugs are easy to find.
I like variables akin to:
$display_scripts - adds extra data in the head section.
$display_onload_scripts - adds onload scripts to body section.
$display_style_sheets - option to include link to additional stylesheets
$display_above_menu - will appear above the menubar. NOT recommended.
$display_below_menu - will appear immediately below the menubar.
$display_one_column - page contents when only one column is to be used
$display_left_column - page contents when two columns used. Left pane.
$display_right_column - page contents when two columns used. Right pane.
$display_footer - appears in footer division.
My main code then just has to generate the appropriate variable. Fundamentally, what you need to do is examine the source of a good age you have produced then replace the stuff you want to change with variables.
Here is a schematised version of the file I use (pseudocode) to give you an idea of how I do it.
// Code here generates the menu from database
// Code here genereates popup alert messages from other users
//permanent links to external style sheets go here.
//You can also select skins here.
<?PHP
echo $display_style_sheets;
echo "<title>".$display_page_title."</title>";
?>
<script type="text/javascript" src="JAVASCRIPT GOES HERE.js"></script>
</head>
<body <?PHP echo $display_onload_scripts;?> >
<div id="page_area" >
<div id="banner">
</div>
<?php
echo $display_above_menu;
if(!$hide_menu){echo $display_menu;} //Insert the menu variable here.
echo $display_below_menu;
?>
<div id="content_area">
<div id="inner_content">
<?PHP
if($display_number_of_columns==1)
{
echo "<div id='onecolumn'>".$display_one_column."</div>"; //I only use this one
}
if($display_number_of_columns==2)
{
echo "<div id='leftcolumn'>".$display_left_column."</div>"; //these are left for legacy support from before I got better at CSS.
echo "<div id='rightcolumn'>".$display_right_column."</div>";
}
echo "<div id='footer'>".$display_footer."</div>"; //just in case - I hardly use it.
echo $display_pop_box; //for user alert messages to other users
?>
</div>
</div>
</div>
<div id="logbox"> Automatic Logout statement</div> //this is called by JS to activate timeouts.
</body>
</html>
<?PHP
$mysqlidb->close();
?>
Sorry it's such a lot of code. The layout allows easy adaptation and makes it simple to find the offending variable if things are not going as expected. There are more elegant solutions but this works well for me and is very fast.

In Wordpress how to add a body class to all interior pages except the homepage

I want to add a class to the body tag to all pages EXCEPT the homepage. Right now I have.
<?php body_class('interior'); ?>
But it adds 'interior' to ALL pages including the home page.
What is the best standard way of adding a class to the body tag to all interior pages except the 'home page'?
http://codex.wordpress.org/Function_Reference/is_home
<?php if (!is_home()) body_class('interior'); ?>
Unless you mean http://codex.wordpress.org/Function_Reference/is_front_page
<?php if (!is_front_page()) body_class('interior'); ?>
I think the best solution is write:
<body <?php if (!is_front_page()) body_class('interior'); ?> <?php body_class(); ?>>
In this way you add only class="interior" in all page except the front page, and you can save the class in the front page, otherwise in you use only the fist php code in the tag <body> in home you will not have class.

Categories