I know this question is already asked a lot, but every time I read an answer on those questions it looks like I've implemented it like I should've. My problem is as follows:
I have added the following statement to my index.php to load a different header (saved in header-frontpage.php) for my static home page (called page-home.php).
if(is_front_page())
{
get_header('frontpage');
}
else
{
get_header();
}
I have also set page-home as my static front page in the wordpress settings->read.
Shouldn't this result in my homepage loading the header-frontpage.php? Now it just retreives my header.php for my frontpage as well. A live preview can be found at www.koencuijpers.com (wrong loading of header.php) and www.koencuijpers.com/spot-map (right loading of header.php).
My complete code for my index.php is:
<?php
/**
* The main template file.
*
* This is the most generic template file in a WordPress theme
* and one of the two required files for a theme (the other being style.css).
* It is used to display a page when nothing more specific matches a query.
* E.g., it puts together the home page when no home.php file exists.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Spot_Utrecht
*/
if(is_front_page())
{
get_header('frontpage');
}
else
{
get_header();
}
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
if ( have_posts() ) :
if ( is_home() && ! is_front_page() ) : ?>
<header>
<h1 class="page-title screen-reader-text"><?php single_post_title(); ?></h1>
</header>
<?php
endif;
/* Start the Loop */
while ( have_posts() ) : the_post();
/*
* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
*/
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
the_posts_navigation();
else :
get_template_part( 'template-parts/content', 'none' );
endif; ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_sidebar();
get_footer();
?>
If you've set your homepage as a static page, then WordPress will be following its template hierarchy to decide what template to use:
https://developer.wordpress.org/themes/basics/template-hierarchy/#front-page-display
This means WordPress will not be using index.php as the template for your front page, so whatever changes you make there will have no effect.
Therefore you need to include your if statement in the appropriate template (probably front-page.php, depending on your theme setup).
Related
I've got a weird issue, possibly because I've customized some PHP on my Wordpress site.
I've got a template called homepage.php that displays all the posts from today's date (taken directly from the Wordpress Codex, filed under WP_Query).
About 7 or 8PM every day, all the posts disappear from that page. Why? And how do I fix it?
I thought at a certain point that it might be a post expiration issue, but I never set any expiration - I don't even think that's an included feature in Wordpress, is it?
Many thanks.
Full code included below just in case there's some clue here.
<?php
/**
* Template Name: Home Page
* The template for displaying all of today's posts on home page.
*
* This is the template that displays only today's posts.
* Please note that this is the WordPress construct of pages and that
* other "pages" on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fifteen
* #since Twenty Fifteen 1.0
*/
get_header(); ?>
<?php
$today = getdate();
$query = new WP_Query( 'year=' . $today['year'] . '&monthnum=' . $today['mon'] . '&day=' . $today['mday'] );
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<?php
// Start the loop.
if ($query->have_posts() )
{
echo'<ul>';
while ( $query->have_posts() ){
$query->the_post();
echo '<li>' . get_template_part('content', get_post_format() ) . '</li>';
}
echo '</ul>';
/* Restore Post Data */
wp_reset_postdata();
//else : // no posts found.
}
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php get_footer(); ?>
What is best practice for making a function call on a wordpress page?
For instance if you want to call my_special_function(); on the home page, where is the proper place to put the function call (ie home-page.php, /template-parts/content-page.php etc.).
<?php if( function_exists( my_special_function ) ) {
my_special_function();
} ?>
FYI Im using Underscores theme.
I've seen a few comments regarding shortcode. Would something like the below WP page template with the shortcode inserted be a best practice over just calling the function on that page?
So if I wanted the function to be called in the page template below I would just insert the shortcode wherever I want it on that page template or just is just calling the function is sufficient or best practice
<?php
/**
* Template Name: Home Page
*
* The template for displaying the home page.
*
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package Rollins_Ridge
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
// insert Shortcode
<?php echo do_shortcode('[special_function_shortcode]') ;?>
// or just call the function
my_special_function();
// or something else im not aware of
code here;
<?php
while ( have_posts() ) : the_post();
get_template_part( 'template-parts/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop ?>
</main><!-- #main -->
</div><!-- #primary -->
<?php
get_footer();
Don't edit themes core files.
the proper way is using functions.php file in child theme directory.
you can add your function in this file and create shortocde using wordpress hook
like
my_special_function(){
//Your content goes here.
echo "This is proper way";
}
add_shortcode('special_function_shortcode','my_special_function');
and use following shortocde anywhere on site.
in wordpress page use [special_function_shortcode]
and in php use
<?php echo do_shortcode('[special_function_shortcode]') ;?>
Not sure whats wrong. I have looked and guess its php thats doing it. Ive seen this posted but cant find a fix.
its hard to post on here' * The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
<div id="main-content" class="main-content">
<?php
if ( is_front_page() && twentyfourteen_has_featured_posts() ) {
// Include the featured content template.
get_template_part( 'featured-content' );
}
?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
// Start the Loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
endwhile;
?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
<?php
get_sidebar();
get_footer();
Close out the php tag on the last line of the initial block of commented out code. It should look like this:
<?php
/**
* The template for displaying all pages
*
* This is the template that displays all pages by default.
* Please note that this is the WordPress construct of pages and that
* other 'pages' on your WordPress site will use a different template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/ ?>
<div id="main-content" class="main-content">
I am building a personal website to host my university work, personal projects and photos etc.
The menu is a hierarchical structure made up of pages and links. Take my university pages for example. What I would like to achieve is to display posts that are related to the module code which is the page's slug.
I've used the following link http://codex.wordpress.org/Page_Templates#A_Page_of_Posts and managed to get it working but I have hard coded the module code into the template, meaning for each module I will have to have a separate template and the only thing that will be different from one file to the next is 5 characters which isn't great for code re-use.
What I am asking, is, is there a way to get the slug from the page I'm looking at and use that for the WP_Query arguments.
If you go to http://michaelnorris.co.uk/ and look at the menu structure. Navigate to University -> Year Three -> Individual Project, you will notice the url is http://michaelnorris.co.uk/uni/three/ci301 where ci301 is the module code for the Individual Project. I want to have this system on each of the module pages so that I can tag posts and they are displayed in the relevant module.
Ok, I actually found the answer myself, but for others looking to do the same. Below is a solution.
Solution found here on the Wordpress.org Codex http://codex.wordpress.org/Page_Templates#A_Page_of_Posts
Name the file pageofposts.php and edit the Page within the Wordpress Dashboard and set the Template (in the dropdown) to 'Page of Posts'. Bingo!
<?php
/*
Template Name: Page Of Posts
*/
/* This example is for a child theme of Twenty Thirteen:
* You'll need to adapt it the HTML structure of your own theme.
*/
get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php
/* The loop: the_post retrieves the content
* of the new Page you created to list the posts,
* e.g., an intro describing the posts shown listed on this Page..
*/
global $post;
$slug = get_post( $post )->post_name;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display content of page
get_template_part( 'content', get_post_format() );
wp_reset_postdata();
endwhile;
endif;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
// Change these category SLUGS to suit your use. category_name is comma separated.
'tag' => $slug,
'paged' => $paged
);
$list_of_posts = new WP_Query( $args );
?>
<?php if ( $list_of_posts->have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( $list_of_posts->have_posts() ) : $list_of_posts->the_post(); ?>
<?php // Display content of posts ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
I am having some problems trying to get a custom page to display on my site. What I want is to create a page where the client can edit the content in Wordpress. When I add the page using the Wordpress "add new page" it doesn't show, but if I create a page in .php with the same content, it works fine. This is what I did:
First I created a blank .php template called lefthome.php using the following code:
<?php
/*
Template Name: Left Template
*/
?>
<?php get_template_part( 'loop', 'page' ); ?>
I then went into Wordpress, clicked on pages > add new. I gave my page a title and added the content I wanted to appear on the page. Then from the template option in the page attributes I choose the template I had earlier created (Left Template) and clicked update.
I wanted this template to appear on the homepage, so I tried to add it to the homepage template I had created using the following code:
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php include (TEMPLATEPATH . '/lefthome.php'); ?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>
The header, slider.php and footer all show fine. The contents of the lefthome.php do not appear. I do not know how to get it to show. The only way I have got it to show is to paste the contents into the lefthome.php template. I don't want to do this though, as the client will not be able to edit the contents themselves.
I hope someone can help me.
Thanks
You won't be able to display the content of a page just by calling directly its template file. You need to query the page itself. In that matter it won't matter what template you've already assigned to it anymore.
<?php
/*
Template Name: Home
*/
get_header(); ?>
<?php include (TEMPLATEPATH . '/slider.php'); ?>
<div id="content">
<?php
global $post;
$page_id =5; // 5 is the id of the page we want to present with Left Template.
$post = get_post( $page_id );
setup_postdata( $post );
include locate_template( 'lefthome.php' );
wp_reset_postdata(); // we are done with that. reset the query so the rest don't mess up
?>
</div>
<?php get_template_part( 'loop', 'page' ); ?>
<?php get_footer(); ?>