I've built a custom page.php template. Very simple, essentially:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php get_footer(); ?>
I've created a few pages, and if I visit their url, I just get the same page title.
I read up in the documentation, and it says to use the_title() and such only when in "the loop".
So presumably, I'm just being shown the first page in the "array".
Is there any way I get get the contents of a single page based on the url?
Edit: In fact, should I even need to do this? Refering to example templates, it looks like I'm doing everything right?
In my sidebar I was using a custom query.
This was called before trying to access the main page content, without resetting.
When doing a custom query you must reset after you've finished your loop like so:
$originalPost = $post;
$sidePosts = get_posts($queryArgs);
foreach($sidePosts as $post) {
setup_postdata($post);
// echo it out like a normal post.
}
$post = $originalPost;
or if you are using query_posts() (which you shouldn't in a sidebar):
wp_reset_query();
Which will take your post back to it's previous value.
For a custom page template please use the following to get everything correct
<?php
/* Template name: My custom template */
get_header();
if ( have_posts() ) while ( have_posts() ) : the_post();
the_title();
the_content()
endwhile;
get_sidebar();
get_footer();
?>
Related
Using WordPress 3.7.1 I am not able to load and render anything on my Custom Page Template.
I have a Custome Page Template as regPage.php and it has been coded as:
<?php
/*
Template Name: Regular Page
*/
<?php get_header(); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php the_content( ); ?>
<?php else : ?>
<h2>Page Not Found</h2>
<?php endif; ?>
<?php get_footer();
?>
While the Regular Page template is available on Page Attribute options I generate and publish the page (Lets' say Test) but when I check the Page(Test) it only shows a while empty page not even loading the Head , Style and Scripts tags from the header.
Here is an example of what happening :Sample Link
I test the header.php and footer.php they work fine on default template options but not working with my custom page template!Can you please let me know why this is happening and how I can fix it?
Two things. First a syntax error. You're opening <?php while <?php is open so close it first or do neither. They must balance.
<?php
/*
Template Name: Regular Page
*/
?>
<?php get_header(); ?>
or
<?php
/*
Template Name: Regular Page
*/
get_header(); ?>
Second problem is you're looping through posts but your call to output their contents is outside the loop.
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_content( ); ?>
<?php endwhile; ?>
...
Though I imagine the_post() would still be set to the last iteration. It's the syntax error that caused a white screen. Turn debugging on.
How can I "tell" Wordpress to choose a different single post template if the user making the request happens to be a logged in subscriber?
This is what I want to do.
Our company distributes products.
The general public can check out the individual products (single posts) and have access to some information (public pricing, etc.).
The plan is to manually add our resellers as subscribers in WP admin, let them log in as subscribers in our website, and then they would be able to see a similar single post, but with some more information thrown in (reseller pricing, volume discounts, links to service manuals, stuff like that).
Is this easy to accomplish?
Thanks!
Are you comfortable with PHP? What you'd have to do is find the template directory and edit (or create) single.php and use get_template_part to display the correct single.php template:
<?php get_header(); ?>
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php if ( is_user_logged_in() ) :
<?php get_template_part( 'single' ); ?>
<?php else: ?>
<?php get_template_part( 'single', 'guest' ); ?>
<?php endif; ?>
<?php
// If comments are open or we have at least one comment, load up the comment template
if ( comments_open() || '0' != get_comments_number() )
comments_template();
?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
P.S. I copied that single.php page from https://github.com/Automattic/_s/blob/master/single.php
I would handle this with the built-in WordPress function is_user_logged_in().
From a high level, I would actually create a code snippet in my single.php template like so:
if (is_user_logged_in()) {
include 'single_subscriber.php';
}
else {
include 'single_visitor.php';
}
Then you can actually keep single.php clean and simple. But put your actual templates for different users in different files.
Of course, if you have common functionality or logic it would be best to do all of those common things in single.php where possible, and perhaps tweak my example to only add more items using that built-in function.
I tried everything found in the web, but i'm getting errors or that's not what I am searching for...
I have to make a PAGE in wordpress, to show ONLY the posts of the category with a certain ID ( in my case id=8 )
i tryed to edit the loop-xxxx.php .. the template file... everything but I get always a problem
navigation system doesn't work. I mean... getting back to older posts won't work cause the output shows the last posts instead of older one.
The code I'm using in the loop or in the template file is:
<?php
query_posts('cat=8');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
i tried inserting it before the
<?php while ( have_posts() ) : the_post(); ?>
in loop.php
or before the call of loop inside the index.php
please help me :\
One solution is to use a custom WP_Query. In the custom page's TEMPLATE file, where ID is the id of the targeted category:
<?php $tmp_query = new WP_Query('cat=ID');
while ( $tmp_query->have_posts() ) : $tmp_query->the_post();
the_content();
endwhile;
wp_reset_postdata();
?>
Check this.
<?php query_posts($query_string . '&cat=8'); ?>
<?php if (have_posts()) : ?>
<optional> You can write here: "You are in category X". </optional>
<?php while (have_posts()) : the_post(); ?>
Good luck.
I have create separated page says "service.php" with following code
<?php /* Template Name: services */ ?>
......//code
......
So the above page now act as separate static page.
now i want to assign particular post for this service page in wordpress.
Is it possible to do that?
Kindly advice on this.
If there are no other querys on the page...
Simple...
<?php query_posts('p=5'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<!-- Do stuff here -->
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
Where it says P=5 - that's the post id number.
I have a permalink structure of /%catergory%/%postname%/.
When I go to blah.com/categoryname I want all posts in that specific category to be listed.
When I go to blah.com/categoryname/post-name I want just the specific post to be displayed.
I have made a category specific template (category-5.php) and have got as far as...
// Display all post titles in category loop
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<?php endif; ?>
// Display specific post in category loop
<?php if ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="storycontent">
<?php the_content(); ?>
</div>
<?php endif; ?>
...but obviously I only want the first loop to display when the url is blah.com/categoryname, and the second loop to display when the url is blah.com/categoryname/post-name.
Any thoughts? thanks
You can't have two loops running on the same page as you do here.
I believe you need to separate out the two things you're trying to do. To have a unique look/feel for the category, create a category-1.php file. To create a unique look/feel for the posts within that category, create a separate 'single' template.
This WP support thread explains how to create the 'single' template: http://wordpress.org/support/topic/266638
There are also a few "post template" plugins that help accomplish the same thing, if you prefer to go that route, for instance:
http://wordpress.org/extend/plugins/post-template/
Full list here: http://wordpress.org/extend/plugins/search.php?q=post+templates
Good luck!