Goal - have a section of my sidebar that is conditional depending on what page I'm on. All child pages of the selected page should also show the conditional section.
There are at least two ways to do this - 1) have all the conditional hooks and resulting code in one sidebar file and, 2) have multiple sidebar.php documents and call them based off what pages are being visited.
I've pretty much failed at both ways so far... :( I'm working on the second method right now though, because I think it might be more flexible long term. Anyway, in my page.php I replaced:
<?php get_sidebar() ?>
With
<?php
if (is_page(1997) || $post->post_parent) {
get_sidebar('sidebar-test-may12.php')
}
else { get_sidebar()
} ?>
With the hope of testing the method. What I want to happen is that if the page is page id 1997 or any of its child pages please show sidebar-test-may12.php. Otherwise show the regular sidebar. With the new code the whole page goes blank. Any suggestions or solutions for me? Seems like a common problem, but I haven't been able to figure it out yet. Thanks!
You have a few problems with your code example:
1) $post->post_parent is not being checked against anything else, so it will only return true if it is a child page (not necessarily the child of the page you want to target)
2) get_sidebar() is being called incorrectly. If you want to get 'sidebar-test-may12.php' from your theme folder, you need to call get_sidebar('test-may12')
3) You're missing semicolons after your function calls
So your code should look like this:
<?php
if(is_page(1997) || $post->post_parent == 1997) {
get_sidebar('test-may12'); //get sidebar-test-may12.php
}
else{
get_sidebar(); //get sidebar.php
}
?>
Let me know if that helps.
UPDATE: Bear in mind, $post->post_parent does NOT get the top-most ancestor ID of a child page. If you want to grab the top-level ID regardless of depth, consider doing this:
<?php
$ancestors = get_ancestors(get_the_ID(), 'page');
if(is_page(1997) || end($ancestors) == 1997)
get_sidebar('test-may12'); //get sidebar-test-may12.php
else
get_sidebar(); //get sidebar.php
?>
POSSIBLE SOLUTION: Building off your example and my proposed ancestry check, one thing you can do is have your template check to see if a special sidebar exists in your theme based on the parent page's slug. This way, if you decide a particular page needs a special sidebar for it and all of its children/grandchildren/great-grandchildren/etc. you just need to add it into your theme with a name of 'sidebar-{parent_slug}.php'. So:
<?php
$id = get_the_ID();
$ancestors = get_ancestors($id, 'page');
$top_page = $ancestors ? get_page(end($ancestors)) : get_page($id);
if(locate_template('sidebar-'.$top_page->post_name.'.php'))
get_sidebar($top_page->post_name);
else
get_sidebar();
?>
This way, you don't need a ton of conditionals to decide which Sidebar file to load on a general Page template.
Use some error trapping to show what's going on with your php, either toggling an error log in .htaccess of a simple widget: http://wordpress.org/extend/plugins/error-log-dashboard-widget/
I've hardcoded logic for sidebars, but I've also used WordPress › Widget Logic « WordPress Plugins for my own and client sites. It's easy to use with a simplified version of WP conditionals, like is_category and is_page ('101')
For someone else tackling this same problem -- there is a new plugin out that solves this problem very well. It even makes creating the sidebars easy. You can check it out here:
http://wordpress.org/extend/plugins/custom-sidebars/
Related
I'm trying to create a second single.php for my WordPress theme.
The first single is used with get_permalink() but it's impossible to create another single2.php
How is this feasible?
I'd like to use the single2.php the same way as the regular single.php
I've seen some tutorials but every time they associate the single2.php with the creation of WP categories and I know it's doable without it. I just don't know how.
Open single.php in your HTML editor. Delete the contents of single.php and place the following code in it. Substitute the number 3 for the Category ID # you noted.
<?php
$post = $wp_query->post;
if ( in_category('3') ) {
include(TEMPLATEPATH . '/single2.php');
} else {
include(TEMPLATEPATH . '/single1.php');
}
?>
You should have a look at the Wordpress Template Hierarchy, it could give you tips to do that :
You could create a page-{slug}.php or a page-{id}.php file for example, but it's not the nicest way as it's only for a specific slug or specific id.
I'd recommend to create a template file, where you can create your page. This template can be reuse for several pages !
I'm trying to use as few plugins on my WP site as possible. Therefore I created custom fields for posts and pages to create a title tag and meta description.
The code look as follows:
<title>
<?php $title = get_post_meta($post->ID, 'Title', true);
if ($title) { ?>
<?php echo $title; ?> | domain.com
<?php }
else { ?>
<?php wp_title(''); ?> | domain.com
<?php } ?>
</title>
The code works like a charm, unless for one cause. The homepage titletag is similar to the most recent blog posted (my homepage is a blog overview). Obviously I don't want that.
I found that inserting the following code right under <title> works
<?php if(is_home()) { echo "My desired homepage title tag"; } ?>
However, the title tag now contains both the desired title tag AND the title from the custom field. I first thought I could simply solve that by using the logical if, elseif, else contruction and change the following:
if ($title) { ?>
into elseif.
However then my whole site breaks and I get a blank page in return for every URL I try to access.
Then my PHP knowledge stops. I have no idea how to solve the double title. Can someone help me out and point what I'm doing wrong? Why doesn't the conditional statements work
The main reason should be that when you access $post->ID you get the ID of the latest inserted post, not the homepage.
By default wordpress loads the latest post an you have them ready to use in the loop.
To avoid that you have to set a different homepage than "latest posts" in "settings" -> "reading".
Once you do that you'll se the correct title loading up.
Anyway I would advise to use the super useful WordPress SEO plugin by yoast, is the de facto standard for this kind of things and is used by a lot of high traffic wordpress installation.
let me know
I am relatively unfamiliar with Wordpress and I am creating a custom theme for a client. I would like to either display or remove the main menu depending on the page type. I have researched several options like removing the navigation from header.php and referencing it separately and also making the menu conditional which is preferable.
I have a custom page type in my theme called 'landing page' on which I would like the menu to be never be displayed, though it will be on every other page. Ultimately there will be a lot of these and I would rather I didn't have to intervene.
I would rather not duplicate my header.php file but I can only find reference to displaying the menu conditionally like below by page name or ID which seems ridiculous.
<?php
if (is_page('contact')){
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
elseif (is_page('gallery')){
<?php include(TEMPLATEPATH.'/headerB.php'); ?>
}
else {
<?php include(TEMPLATEPATH.'/headerA.php'); ?>
}
?>
Rather than including files as above, I will put the whole thing into my header and just make the navigation conditional. Does anyone know how I should approach this using my custom page type landing page rather than by page name so every page created with that type will never have a menu?
Thanks
Are you talking about a Custom Post Type (CPT) or a page called landing-page?
They are completely different. See http://codex.wordpress.org/Post_Types
In any event, this will work for a custom post type or a page:
if ( !is_singular( 'custom-post-type-name-or-page-slug-here' ) ) {
get_template_part('menu');
}
It says: "If this page is not a single page or a CPT, load the file menu.php from the theme folder."
See also http://codex.wordpress.org/Include_Tags:
The get_template_part() tag includes the file {slug}.php or
{slug}-{name}.php from your current theme's directory, a custom
Include Tags other than header, sidebar, footer.
Is there an easy way to change the WP default template hierarchy?
For instance;
Say I want to change my theme directory structure so that it completely changes from the Template Hierarchy suggested here based upon conditionals:
http://codex.wordpress.org/Template_Hierarchy
if I wanted to make sure that for all page types (is_single() is_home() etc) it always opens one template file which then instigates my own pattern to provide the output?
Thanks very much!
I would do it like this:
Easy to read and works well
single.php:
<?php include_once('template-you-want.php');
home.php:
<?php include_once('template-you-want.php');
If you don't want to have these two files at all do it in index.php:
<?php
// at the very top
if (is_single()){
include_once('template-you-want.php');
die(); // don't continue
}
if (is_home()){
include_once('template-you-want.php');
die(); // don't continue
}
I've inserted the following code to the template loop (in the correct place), but it is not outputting any comments. Why?
<?php
$withcomments = true; // force comments form and comments to show on front page
comments_template( '', true );
?>
I'm trying to display comments for each post on the main-home-page stream of posts.
I'm using the Twenty Ten theme.
Try this before the <?php endwhile; ?> of the loop in loop.php:
<?php
$withcomments = "1";
comments_template();
?>
Try this:
<?php global $withcomments; $withcomments = 1; comments_template(); ?>
There is a much simpler way that doesn't involve editing PHP code. First make sure you can create comments on other pages and if that is working, go back to the home page.
On the top right, click the gear icon to show settings and near the bottom of settings change the "page attributes" "template" from "front page template" to "default template". Save and you will have comments.
However, you may lose other features of the home page (you can always change the template back). For me I didn't lose anything.
Understand that a normal WordPress blog is intended to have comments on the blogs (posts), but not on the home page. By default you shouldn't even have comments on any pages (just posts), but that is easily enabled. That is why normally there aren't any comments allowed, but if you have a one-page site this is a problem.
Also note that there are many different themes and some do allow comments on the home page.