How to remove header from particular single page - wordpress

I'm a newbie at WordPress and php.
I want to remove the header from one or two pages only.
I saw this http://wordpress.org/support/topic/how-to-remove-footer-from-individual-pages
so I did this in my header.php file found under my theme folder
<?php if( !is_page('18') ) :?> <!-- this is what I added -->
<header class="banner">
...rest of html...
</header>
<?php endif;?> <!-- this is what I added -->
Just for the sake of seeing if changes to this file were taking effect I also did <?php if( false ) :?> which was supposed to remove it from all pages but this didn't work either.
Though I don't know the difference, I saw some different syntax and so also tried <?php if(false) { ?> and <?php } ?>
I am wondering if I have to do something else to have the changes take effect.
In case it matters, I am using the roots starter theme http://roots.io/starter-theme/
and WordPress 3.9.1

You should be editing the file /roots/templates/header-top-navbar.php. This get decided at base.php:
if (current_theme_supports('bootstrap-top-navbar')) {
get_template_part('templates/header-top-navbar');
} else {
get_template_part('templates/header');
}
Just in case, add your code to both. I tested your code with header-top-navbar.php and works ok.

you are giving wrong condition it should be if(is_page('18'))
{// do nothing } else {...rest of html...}

Related

What's the best/most common approach to multiple sections front page in WordPress

I'm new to WordPress and I'm thinking about developing some premium themes. I see that a real trend these days is these themes with multiple sections separated in horizontal blocks in the first page. Stuff like this:
<section class="about-us row">
<h1> About us</h1>
<p>Some lorem here </p>
</section>
<section class="features row">
<h1> Features</h1>
<div class="col-1-3">
<h2>Responsive and shit</h2>
<p>Some lorem here </p>
</div>
<div class="col-1-3">
<h2>Free support</h2>
......
</section>
<section class="testimonials">
........
</section>
I'd like to know what's the best or most common approach devs are taking to provide this feature for their end-users.
I see that some of the best selling themes are using page builders managing it as shortcodes, but I'm not planning to use any page builder, at least not in my first theme, I noted that it's quite easy to get a messy code when using them, so I want to start simple.
So guys, can you help me? would the answer be using just shortcodes?
Thank you
Step 1:
I would suggest breaking the layout into sections using the get_template_part() function in your front-page template. The benefit of this is you can simply call for whatever part of the layout you need in any page template like so: get_template_part('testimonials');. You can even use them in the header and footer if you need to.
In your case i'm assuming there are 3 template parts: about us, features, and testimonials. You will need to create 3 PHP files that contain all of the code for each of those 3 parts. The PHP files will need to be located in your template's root folder. The PHP file can obviously utilize PHP however you need it to, but the main idea is that your HTML code for that section or "template part" will be placed in it's own PHP file. If you need to pull posts from wordpress, or perform database queries to generate the content for that section, you can do so individually for each template part in it's own self-contained PHP file. For the purposes of this example, let's just assume that you've called the PHP files for your template parts about_us_part.php, features_part.php, and testimonials_part.php.
After you create your 3 PHP files, making sure they are placed in your template root, you simply place the following lines of code wherever you want that particular section or "template part" to appear in your Wordpress page template. Like so:
<?php get_template_part( 'about_us_part' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part' ); // Testimonials (testimonials_part.php) ?>
Basically, get_template_part( '{slug}' ); searches for a filename in your template root matching {slug}.php. So you can name it whatever you want, but if there is no matching PHP file found, obviously nothing will show up. There is one other option for get_template_part() that allows you to specify a name for the template section. However it is optional and not required, you can use it like so:
<?php get_template_part( 'about_us_part', 'about-us' ); // About Us (about_us_part.php) ?>
<?php get_template_part( 'features_part', 'features' ); // Features (features_part.php) ?>
<?php get_template_part( 'testimonials_part', 'testimonials' ); // Testimonials (testimonials_part.php) ?>
You can read more about get_template_part() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/get_template_part
Step 2:
Now say you wanted to allow the user to display these template parts using shortcodes, you'd need to give them that ability in your functions.php file. For instance, say we wanted to create 3 shortcodes for each of the 3 template parts above. You can do it pretty easily using the Wordpress Shortcode API. You'd add the following code to your functions.php file:
[about_us]
function themeprefix_about_us_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'about_us_part' ); //Get about_us_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'about_us', 'themeprefix_about_us_shortcode' );
Once that function is in your functions.php file, along with the matching add_shortcode() function users can call out the About Us section by using the shortcode [about_us]. The two parts of the add_shortcode() function are the shortcode name, and the function that generates the content for the shortcode. Like so: add_shortcode( '{shortcode name}', '{shortcode function}' );
You'd need to create 2 more for your other 2 shortcodes:
[features]
function themeprefix_features_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'features_part' ); //Get features_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'features', 'themeprefix_features_shortcode' );
[testimonials]
function themeprefix_testimonials_shortcode( $attr ) {
ob_start(); // Start output buffer
get_template_part( 'testimonials_part' ); //Get testimonials_part.php
return ob_get_clean(); //Clear output buffer
}
add_shortcode( 'testimonials', 'themeprefix_testimonials_shortcode' );
Note: I placed "themeprefix" on the front of each function. I'd reccomend replacing that with your theme name, or whatever prefix you might be using on the front of your theme's function names. However the function name can be whatever you want it to be, just be sure to update your add_shortcode() to the new function name.
You can read more about add_shortcode() in the Wordpress codex here:
http://codex.wordpress.org/Function_Reference/add_shortcode
Also, I reccomend reading the Shortcode API page in the codex to learn how to add parameters to your shortcodes:
http://codex.wordpress.org/Shortcode_API

Dynamic current page highlight issue in wordpress

I'm trying get the code below to always highlight the current page of a theme that I'm working on but it does nothing. The HTML part is as shown below the script. Any suggestions or solution for the fix.
<script type="text/javascript">
var url = window.location.href;
url = url.substr(url.lastIndexOf("/") + 1);
$("#navbar").find("a[href='" + url + "']").addClass("current");
</script>
<div id="navbar">
<ul>
<li>home</li>
<li>services</li>
<li>procedure</li>
<li>about</li>
<li>contact</li>
</ul>
</div>
You need to look into WP Nav Menu.
I'm guessing this is a theme you're developing? If so, Register a Nav Menu in your functions.php file. What this will do is allow you to define your menu in the Wordpress dashboard (Appearance->Menus), and customize it however you like.
The reason why I'm suggesting this is that not only will it allow you to have total control over your Menu in a very intuitive way, but it also defines all the classes you could possibly need for your stylesheet automatically (including current items, and ancestry).
That is the cleanest option you have at your disposal. You can also look into WP List Pages for a similar (albeit, more limited) solution.
Otherwise, you'll have to do something dirty like this:
<?php
$id = get_the_ID();
$class = ' class="current"';
?>
<ul>
<li<?php echo is_home() ? $class : ''; ?>>home</li>
<li<?php echo $id==$serives_ID ? $class : ''; ?>>services</li>
<li<?php echo $id==$procedure_ID ? $class : ''; ?>>procedure</li>
<li<?php echo $id==$about_ID ? $class : ''; ?>>about</li>
<li<?php echo $id==$contact_ID ? $class : ''; ?>>contact</li>
</ul>
Replace $services_ID, $procedure_ID, etc. with their respective Page IDs, and if the current ID matches any one of them, that item will have a class of "current" applied to it, which you can then target in your stylesheet.
The main reason why I suggest a server-side solution over using any kind of jQuery is that this is not a task that is suited specifically for jQuery itself.
While the jQuery library is a powerful tool, Wordpress has all of the built-in functionality you need to detect everything you want to detect in this instance. Let the server do as much work as possible before letting the client's browser take over. Otherwise, you'll be looking at some serious load times.
UPDATE:
Based on your last comment, if you're going to go with the messy way and you don't want to be bothered with finding IDs of your pages, you can also do this:
<?php
$slugs = array('services', 'procedure', 'about-us', 'contact-us');
?>
<ul>
<li<?php echo is_home() ? ' class="current"' : ''; ?>>home</li>
<?php
foreach($slugs as $slug)
{
$page = get_page_by_path($slug);
$class = is_page($slug) ? ' class="current"' : '';
echo '<li'.$class.'>'.str_replace('-us','',$slug).'</li>';
}
?>
</ul>
UPDATE BASED ON LAST COMMENT:
Now that you're using wp_nav_menu, you can target the current menu item with CSS. Here's an example that will turn the background of the current item to red:
<style type="text/css">
.current-menu-item{background:#F00;}
</style>
Apply these rules either in your header, or preferably in your main style.css file. Good luck!
Your substr is using lastindex of / +1. The links shown in the html have / on the end. I'd imagine the two are mutually exclusive as s string ending in / would always return an empty string with that substr.

Drupal multi-site and one theme: how to know which multi-site user is on?

I have drupal multisite installed and atm I have two sites. Both sites uses same theme, but there are few tiny differences between looks of the site (like logo and div/bar is different color). Or well I would that they would have those differences. Now the question is how can I know on theme template that which site is showing up? Is there some paremeter or variable somewhere? Basically so that I could do is simple php if clause (if its this site, show this div and its the other site dnot show it)?
Thanks.
In one of my project I had the similar problem. What I did was that in template.php I created the following function:
function mytheme_firstdomain() {
global $base_url;
if(strpos($base_url,"http://firstsubdomain.mydomain") !== false) {
return true;
}
return false;
}
And then I could call this in the page.tpl.php code. Like
<?php if(mytheme_firstdomain()) { ?>
<div>Only for first domain</div>
<?php } else { ?>
<div>Only for the second domain</div>
<?php } ?>
Otherwise you could look into Block Classes module. That could also help.

Wordpress: Show post if it is in two specific categories

I am trying to get the index-page of a Wordpress-blog show some very specific posts. As far as I understand i need to use a standard loop in order to make sticky posts work, so custom queries is out of the question. (Correct me if this is wrong.)
All posts are put in a main category (Eg. "Make-Up") In addition, posts that should show on the front page gets an additional category "Frontpage".
The current loop outputs all posts, regardless of category. And styles certain categories differently. An example would be the video-category which is only shown by getting an embed code from a custom field in the post.
<?php elseif (in_category('20')) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
I need to remove all posts not in the category "Frontpage" while still being able to control how posts are being shown.
Earlier i used a filter to control the main loop:
function exclude_category($query) {
if ( $query->is_home ) {
$query->set('cat', '20 27');
}
return $query;
}
add_filter('pre_get_posts', 'exclude_category');
However this would cause my geomashup-plugin to break as it probably uses the same loop?
My current proposal for a solution would be to do something like this, plus functioning code:
<?php elseif (the post is in BOTH category 20 and 27)) : ?>
<div class="post element grid_4">
<?php echo get_post_meta($post->ID, 'Embed', true) ?>
</div>
<?php else : ?>
<div style: display: none;></div>
However i am unsure about how make a condition demanding the post to be in two categories, and i realise this is a terribly dirty fix.
Any tips or pointers as to how i could solve this would be greatly appreciated :)
Front page can be seen here: http://parfymelle.brandbase.no
For anyone wondering i solved it by including the geotagged posts-category (the shop locations) in the filter for the main loop, and then using a php if in the index.php to hide posts from that category. Dirty, but works, i guess.

WordPress: Multiple conditional statements not working

I'm trying a simple multiple conditional statement which should work fine with PHP but WordPress says no. What am I missing?
<?php if (is_page('sample1') || is_page('sample2') || is_page('sample3') || is_page('sample4')) { ?>
include this
<?php } else { ?>
include this instead
<?php } ?>
Undefined function?
How are you calling footer.php from your theme?
It has nothing to do with the code you posted. It has everything to do with the path to the footer or where you are trying to load it within the page lifecycle. Also check if the path is right.

Resources