Wordpress front-page.php template - php

I have my front page set to a static page and am trying to build my custom template. How do I actually show the selected front page in front-page.php? I have googled and googled but can't seem to figure out how to do it.
The front-page.php actually loads like it should, but I can't seem to find documentation on exactly how to show the page that is assigned as the static home page. Any suggestions?
I have tried
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
but that didn't seem to work...

Your static page uses a page template (usually page.php for the default template)
You can create a new one for the homepage if you wish. see : Creating_Your_Own_Page_Templates copy page.php to homepage.php and change the template name
Example template (homepage.php) :
<?php
/*
Template Name: Homepage
*/
//the content of page.php and now you can do what you want.
?>

$id = 0; /* The id of your page */
$page = get_page($id);
echo apply_filters('the_content', $page->post_content);
If its a static page, I should not use a loop.

First, take a look to topic to show something only on home page. a related question is Wordpress Post Thumbnail Issue (Only 1 thumbnail on frontpage). Also, it can be useful how to create a static front page in wordpress.

I was missing something obvious. The loop I was using I had copied out of wordpress template. It actually called another template file. What I should have used was:
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages(array('before' => '<div class="page-links">' . __('Pages:', 'twentytwelve'), 'after' => '</div>')); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link(__('Edit', 'twentytwelve'), '<span class="edit-link">', '</span>'); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php endwhile;?>

Related

get_template_part - Wordpress not working

I am trying to place the content of the about page inside a div on the header , the template part is located on folder template-parts/content-about.php
on the header the code is:
<div id="slideOut">
<div class="slideout-content">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'about' );
endwhile; // End of the loop.
?>
<?php include 'content-about.php'; ?>
</div><!-- .slideout-content -->
</div>
And the content-about.php looks like this:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="entry-content">
<?php
the_content();
wp_link_pages(
array(
'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'about' ),
'after' => '</div>',
)
);
?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
The issue is that is showing on the div the same content as the current page for example if I'm located on home page it shows only the content of the home page inside the div when it should always show the content of the about page.
why is not working?
Thanks
get_template_part() is working as expected. Your content-about.php is included normally.
However, it prints the content of the current post/page because you're calling the_content().
You could replace the_content() with get_the_content(), passing the post id of your about page as the third parameter.
Note that:
An important difference from the_content() is that get_the_content() does not pass the content through the the_content filter. This means that get_the_content() will not auto-embed videos or expand shortcodes, among other things.
So, you might want to use apply_filters() like this:
<?php
echo apply_filters( 'the_content', get_the_content( null, false, $about_page_id ) );
// where $about_page_id is the post id of your about page
You don't even need the get_template_part for this if you don't want.
You can do this:
<div id="slideOut">
<div class="slideout-content">
<?php echo get_the_content('','','your-about-page-id-here'); ?>
</div><!-- .slideout-content -->
</div>
get_template_part() is a way to include files with markup that may rely on the loop, but keep things separated. You don't necessarily need it here.
You can also use:
echo get_post_field('post_content', your-post-id-here );

WordPress : using page template to fetch posts in wp page

i want to display posts in wp page. to do that this is what i did i created a template named mypage-page.php and copied code from page.php to mypage-page.php
this is my mypage-page.php
<main id="main" class="site-main" role="main">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title() ;?></h2>
<?php the_post_thumbnail(); ?>
<?php the_excerpt(); ?>
<?php endwhile; else: ?>
<p>Sorry, no posts to list</p>
<?php endif; ?>
</main><!-- .site-main -->
// this has header and footer as well .
now i created a page mytest using this template ,what i expect it should list posts but it does not, please help me to understand where i'm wrong .
i'm just a beginner
If your page is a Custom Page template then you have initialise a custom query.
WordPress's default query won't work in a custom page template.
You can do that query using WP_Query Class :
https://codex.wordpress.org/Class_Reference/WP_Query
Or
get_posts() :
https://codex.wordpress.org/Template_Tags/get_posts
I'd suggest the earlier one.
you can also use this, query_posts

trying to understand page.php in Wordpress "application" theme

I'm trying to set up a commercial website using Wordpress and I'm experimenting with different themes. I like "application", but I would like to get rid of the sidebar in "pages" (as opposed to "posts"). So I figured I would edit the template. I believe that is the file "page.php." I'm just learning PHP so some of this is mysterious. My immediate question is how to get rid of the sidebar, but more important than that, I just want to ask some basic questions about the structure of this file.
Here is page.php in the themes/application directory:
<?php get_header(); ?>
<!--content-->
<div id="content">
<div id="left-col">
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<div class="post-head-page">
<?php if ( is_front_page() ) { ?>
<h1><?php the_title(); ?></h1>
<?php } else { ?>
<h1><?php the_title(); ?></h1>
<?php } ?>
</div><!--post-heading end-->
<div class="post-entry">
<?php the_content(); ?>
<div class="clear"></div>
<?php wp_link_pages( array( 'before' => '' . __( 'Pages:', 'application' ), 'after' => '' ) ); ?>
</div><!--post-entry end-->
<?php comments_template( '', true ); ?>
<?php endwhile; ?>
</div> <!--left-col end-->
<?php get_sidebar(); ?>
</div> <!--content end-->
</div>
<!--wrapper end-->
<?php get_footer(); ?>
Basic questions:
Does this represent the structure of both Pages and Posts? I only care about Pages (I won't use posts in my website; it's a commercial website)
I'm told that when I create a custom template, I have to conform to the structure of the pages in my theme. What is the "structure" of this file? How can I tell what that is for my theme?
And if you can see a simple way to remove the sidebar let me know.
This file only represents the structure for pages. Not for posts. To see what templates are used for what content, see this documentation, including a nice diagram.
I believe they mean that you'll need to keep the same html/php structure as in this file. When creating custom page templates, duplicate this file and then start adjusting it to your needs.
Remove the line with get_sidebar() to remove the sidebar.
Hope that helps!

Wordpress "loop" not showing blog posts in my theme

I've made my first theme and it works great for editing updating pages etc, but won't display any posts.
I have put the "loop" in a template page (copied from twentytwelve theme), as I only want the posts to appear on that page. I've set the blog posts to appear on this page (from the settings page), but still nothing will show.
Here is my code for the template page to display blog posts.
Any idea what's wrong?
<?php
/**
* Template Name: blog
*
* Full width page template with no sidebar.
*
* #package Myfirsttheme
* #subpackage Template
*/
get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found. Perhaps searching will help find a related post.', 'twentytwelve' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
<?php endif; // end current_user_can() check ?>
</article><!-- #post-0 -->
<?php endif; // end have_posts() check ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
Posts will always appear in the index.php template, unless you change the "Front page displays" option in Settings=>Reading, see here for example: http://imgur.com/izwa5yw If you have this set to show the blog posts on a page (is in the image), then whatever page that is (blog) has to have the Default Template (in the page edit screen) set to the value you wrote in the Template Name: section of your file (in your case blog), as Tamil said.
Updated: You have to echo get_template_part() or it won't show up. You can use the_content() instead which is preferred. Any variables that start with the_ output themselves. The get_ variables don't output themselves.
<?php echo get_template_part(); ?>
<?php the_content() ?>

Wordpress Single post content not showing up

I've got a wordpress blog theme which shows the content of all posts on the index fine but when I click into one of the posts the content of the post is blank and I can't seem to figure out why. If I'm correct the single.php controls that page.
http://pastebin.com/afLVxMPb = My single.php
an example of what I mean would be http://www.ndesign-studio.com/demo/wordpress/blog/how-about-a-blog-post-with-longer-title but on this site the content of the blog post does show up but on mine it doesn't.
I think the problem is somewhere here...
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages('before=<div class="page-link">' . __( 'Pages:', 'your-theme' ) . '&after=</div>') ?>
</div><!-- .entry-content -->
You should add "the loop" somewhere in your single.php file and call setup_postdata($post) or the_post() so you can access the post data inside that loop.
Read more about the loop here: http://codex.wordpress.org/The_Loop
For example, your single.php file will look something like this (simplified):
........
<div id="content">
<?php if(have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h1 class="entry-title"><?php the_title(); ?></h1>
// etc.. all post info
............
<?php endforeach; ?>
Hope that helps! Good luck.

Categories