Home Page Only Footer - wordpress

I have a blog, http://sweatingthebigstuff.com and I would like to add an extra line in the footer which will display only from the homepage. I found this code, but it is not working for me. Do I have the wrong syntax or is there something else I can try to get this to work?
<?php if ( is_home() ) { ?>
text
<?php } ?>
Here is where footer.php is called
<?php include (TEMPLATEPATH . '/sidebar1.php'); ?>
<div class="cleared"></div>
<?php get_footer(); ?>
And here is the footer code:
text 2
Contact | Disclaimer | Privacy StatementCopyright © 2009-2010 Sweating The Big Stuff. All Rights Reserved.
and then some sitemeter crap.

is_home() sets a global var that doesn't seem to reset itself or re-evaluate, wp kinda strange.
Try putting wp_reset_query() at the end start of your if statement code
Actually, it'll be better to call it before as we can ensure the queries are reset
<?php wp_reset_query();
if ( is_home() ) { ?>
text
<?php } ?>
Now that the php is working, ideally you'd want the code above.
text
I just did a view source and I can plainly see the php code, which shouldn't be visible since it is meant to be parsed server side. The following shouldn't be there in the view source.... wrong file being edited?
<p>
<?php if ( is_home() ) { ?>
text
<?php } ?>
<wp_reset_query()>
<br />
<br />
The footer.php file should be located in wp-content/themes/nameofyourtheme folder

is_home() is a method that should return true or false. You need to implement this method somewhere. If blogspot doesn't implement this method for you, you need to do it yourself. For your website I think this function would do what you want:
<?php
function is_home(){
$r = $_SERVER['REQUEST_URI'];
return $r == '/' || $r == '' || $r == '/index.php';
}
if(is_home()) {
?>
text
<?php } ?>
And where you want the footer, put:
<?php include 'footer.php'; ?>
instead of the line:
<?php get_footer(); ?>
I believe your problem is that get_footer() is reading the footer as text, so it isn't executing the PHP beforehand. If you do it this way you can add as much PHP in the header as you want.

There is nothing wrong with your syntax, and when I view your page source, I see " text "
What is the file extension of your footer page? if it is "footer.php" then I shouldn't see the opening and closing terms for php (). php won't run unless the file extension is ".php"
as to a previous answer:
<?php wp_reset_query();?> should go BEFORE <?php if(is_home()){?>text<?php } ?> in this scenario. is_home() depends on a loop being present on the page. Maybe somewhere you used a custom query, or one of your plugins used a custom query that upset the default query vars. Like I said, use the reset statement before your if statement.

You need to modify the code to include a check for is_front_page() like so:
<?php
$ishomepage = ( is_home() || is_front_page() );
switch( $ishomepage )
{
case true :
echo 'Your homepage-only snippet of text goes here';
break;
case false :
default :
// Do nothing... or do something else...
break;
}
?>
Reference: WordPress Codex: is_front_page()
Default WP installations don't have a homepage defined, it uses your index.php and checks for other templates like a home.php template as a starting point (home.php only if your theme has it.) Here's a diagram from their online docs that shows how their hierarchy works: WordPress Template Hierarchy.
By default, WordPress shows your most recent posts on the front page
of your site. But many WordPress users want to have a static front
page or splash page as the front page instead. This "static front
page" look is common for those who wish to not have a "blog" look to
their site, giving it a more CMS (content management system) feel.
If you want to know how to set a static homepage, read this article and follow the instructions: Creating a Static Frontpage. When you set a static front page, is_home() will work as expected.

Related

What is the url for page-test.php?

I create a file page-test.php in my template directory.
page-test.php
<?php echo "Hello"; ?>
What is the url for this page?
But it's not working...
i want to find the url for display the php file "page-test.php"
You can put this line to create the page template in WordPress
<?php /* Template Name: Example Template */ ?>
and then write your code in this PHP file
<?php echo "Hello"; ?>
After that, assign this page template to the admin area,
Reference: https://developer.wordpress.org/themes/template-files-section/page-template-files/#creating-custom-page-templates-for-global-use
I hope it might be helpful to you!

template management with wordpress (underscore boilerplate)

I am using underscore to develop a wordpress theme.
I have a custom post type name project, thus I have, for instance, this url: http://a.site.local/projects/a-beauty/.
I have in my template-parts/ directory the file content-projects.
$ cat template-parts/content-projects.php
<h1>Project</h1>
When I browse http://a.site.local/projects/a-beauty/, I have my title but also the sidebar and the footer (even if they do not appear in my content-project.php nor in index.php).
Where are those widgets coming from / loaded ?
Add conditions to the header, footer and sidebar.
For whole custom post archive:
<?php if( !is_post_type_archive( 'project' ) ) : ?>
// wrap the code you don't want to show on that archive
<?php endif; ?>
For custom post only:
<?php if( !is_singular( 'project' ) ) : ?>
// wrap the code you don't want to show on the post
<?php endif; ?>
If you want to put the code you WANT to show, remove the '!' before condition.
P.S.
You can put the whole content in a condition, but keep the
</div><!-- #page -->
<?php wp_footer(); ?>
</body>
</html>
otherwise the page will break :)
Please share some more code from your project so we can easy to understand the real problem.

wp_title showing page title but not site title

I couldn't find an answer to this question anywhere and I have a hard time believing I'm the only one who has had this problem.
My title function inside my title tag of header.php, <?php wp_title('|', true, 'right'); ?>, is returning the page title but not the site title.
Example: About Us |
Why would it not be returning the site name??
Looks like wp_title() does not return the site's title. Sorry, hopefully someone will benefit from this answer.
I've never tried combining the entire title inside one function. Instead I use multiple calls:
<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>

Wordpress if else requirements for comments

I am not sure where to start, but I want to add in a symbol or change the css for the comments for registered users. Show a difference between non registered user comments and registered user comments.
How would I go about adding this to my wordpress website?
<div class="commentdetails">
<p class="commentauthor"><?php comment_author_link() ?></p>
<?php if ($comment->comment_approved == '0') : ?>
<em>Your review is pending approval by gsprating staff.</em>
<?php endif; ?>
<p class="commentdate"><?php comment_date('F jS, Y') ?>
IP/Hostname: <small>(coming soon)</small>
<?php edit_comment_link('Edit Comment','',''); ?>
</p>
I want to add make it so that the entire class is a different color if the user is a registered logged in user.
Here's an all php version of Saladin's code using the standard if/else sysntax:
<?php
if ($comment->user_ID) {
echo "<div class='comment_registeredUser'>";
}
else { // The user is not logged in
echo "<div class='commentdetails'>";
}
?>
Putting all the code in php has fixed execution errors for me. Of course, that may have been because I was doing something else wrong.
As comments are displayed in the wp_list_comments() function, you will need to edit the code there. The easiest way to achieve this is to use a simple if/else statement checking whether or not the comment has a user ID associated with it. If it does, that means the comment was made by a registered user.
Of course, as well as this, you will need to create a new CSS class to give the distinction. Here is some example code:
<?php if($comment->user_ID) : ?>
<div class="comment_registeredUser"> <!-- Or whatever you decide to call the CSS class -->
<?php else : ?> <!-- The commenter isn't a registered user -->
<div class="commentdetails">
<?php endif; ?>
// Then include the rest of the code as is
The $comment->user_ID variable will return a true if the comment poster is a registered user and was logged in when they posted the comment. The code above will assign your custom CSS class to the div if it does indeed return true. If not, it will simply apply the standard class and styling.
There is also a really good tutorial for developing themes over at the Wordpress Codex. Definitely worth having a read through if you are unsure on what you need to do to create/edit your WordPress theme.
Edit: Cleaned up the answer and better explained the correct logic.

Adding a static text on top of view when it is not filtered

I want to add a static text (some sort of explanation/welcome text) on the very top of a view (over the filter) in Drupal 6.x with Views "2". I want this text appear only when the view is not filtered (i.e. on initial load of the page).
My problem is that the only place I figure out to make it work partially is in the views-exposed-form--MYVIEW.tpl.php. The problem is that when I place the code in this template, I don't know if the view is filtered or not, so the text appear on every single page! I don't have access to this info in that template so the only place this is available ($rows or $empty variables for example) is in views-view--MYVIEW.tpl.php.
But there I got an another problem. The order in witch it seams the variables are output are not the same as the order in witch they appeared in the file. For example, the $exposed variable content is render always on top, then $admin_links, $header and so forth.
<?php if ($header): ?>
<div class="view-header">
<?php print $header; ?>
</div>
<?php endif; ?>
<?php if (!$rows): ?>
<h3>This static text appear AFTER $exposed !!!</h3>
<?php endif; ?>
<?php if ($exposed): ?>
<div class="view-filters">
<?php print $exposed; ?>
</div>
<?php endif; ?>
<?php if ($attachment_before): ?>
<div class="attachment attachment-before">
<?php print $attachment_before; ?>
</div>
<?php endif; ?>
So even if I place my static content before this code, the filter form always appear on top!
I found the reason why is doing this: the exposed filter form is rendered as a part of the content-top <div></div>, but not the result (and $header, $footer, etc).
So is this by design? Do I miss something? How can I get my static text on the very top of the content-top!?
Well, after some tweaking and lecture on preprocess function in the theming system, I found a solution to my problem. I share it with you and if you find a more elegante approach; let me know!
What I did is this...
1) In the template.php file of the theme, I add two fonctions:
function YOURTHEME_preprocess_views_view__MYVIEW(&$variables) {
if ($variables['rows'] || $variables['empty']) {
$GLOBALS['dont_show_static_text'] = TRUE;
}
}
function YOURTHEME_preprocess_views_exposed_form__MYVIEW(&$variables) {
if ($GLOBALS['dont_show_static_text']) {
$variables['custom_flag1'] = TRUE;
}
}
So, if my views is showing some results ($row) or a blank result ($empty), then I set a flag to be use in the template file....
2) In my views-exposed-form--MYVIEW.tpl.php
...
<?php if (!$custom_flag1): ?>
<h2>Some static text here</h2>
<?php endif; ?>
...
And voilà! My static text is showing up only on the initial load of the view and it shows on TOP of the filter (not under!).
Hope this help someone else!

Resources