As the title says, I'm currently working on a theme and i've found a bit of a problem.
Let's say I make a page called test and give it the custom page template of test-page-template
so far that works fine if I go view the page but the problem comes when I set the homepage to use my page as the static homepage; what happens is instead of using the test-page-template it uses front-page.php.
The obvious work around would be to edit front-page.php to be the same as test-page-template but that's a bad solution if a client wanted to select a different page as the static front page. Any ideas?
This is happening because of the WordPress template hierarchy. Front-page.php takes priority as compared to page template, when it comes to the front page of the blog. Here is the heirarchy graph:-
I'm not sure of the complete requirements, but I would suggest you to use index.php and page templates in that scenario.
Related
I have just encountered a small but weird problem in WordPress. I have made page called 'nyheter' (translates to news in Norwegian), where I have made a custom page having a 'page-nyheter.php' in my template folder where I want to display all the posts on the website.
Please keep in mind that I don't want to display any posts on my front page.
I do also have a custom static front page that is just 'front-page.php' which works fine navigating to. However, choosing my front page as the homepage, and the 'nyheter' page as the posts page in the settings, ends up showing the homepage on the 'nyheter' page as well. All of the other pages works fine, and if I just remove the 'nyheter' page from the custom posts page, it works fine again.
What exactly am I doing wrong here? Could someone please help me?
The Posts page will use index.php for it's template, and the Static Front Page will use front-page.php
Is the Nyheter page powered by posts? Why not use index.php since is most suitable for posts than a page template.
https://wphierarchy.com/
If you are a developer please enable wp_debug to true. Then check your errors.
I got my own created template in Wordpress. I got two pages : index.php and ab.php. My question is what should be a proper link code that links these two pages in menu? Because I always get 404 page or index.php page or blank. I try'ed everything including saving permalinks.
Here are a few of my examples:
http://example.com/?p=ab
http://example.com/?p=ab.php
<a href ="ab.php">
etc.
My guess is that this is your first WP theme.
If that is the case I should tell that the hierarchy from pure php websites won't work. Check this https://developer.wordpress.org/themes/basics/template-hierarchy/ WP uses the wp-admin part for creating new pages.
If you are switching from php to WP, I think I can help a bit by giving few pointers.
If there is an issue with your project I can't quite get what is happening but if you call http://example.com/ab.php the php inside will run however it's not cool to reference pages that way in WP.
I'm trying to figure out how a certain WordPress sets things up. I'd like to have a special page where I could make WP calls and interact with the theme, without affecting anything else.
I just making test.php and putting it into my theme's folder, but that doesn't work.
#Eliran provides one possible option, but you could also add a page in the back-end of WP, just make sure it has the slug 'test', and change your 'test.php' filename to 'page-test.php'. If you're worried about the public seeing this, set the page visibility in the admin to 'private'.
Edit:
to move your understanding along a little further also, you should review the way that WordPress determines what file to grab to render a particular URL. This can be pretty confusing to start with, so be patient if you're not familiar with it, but it's at the heart of designing WP themes. I'll link to the examples, and if you scroll down a little there's a diagram that, along with the text, will help you see how WP is 'thinking'.
http://codex.wordpress.org/Template_Hierarchy#Examples
You can see here: Page Templates
all you need to do is create a page named page-{custom-name}.php and add it to the theme folder.
and inside this php file add:
/*
Template Name: My Custom Page
*/
and than to use this page you need to go to the wp-admin, add/edit a page and chose it:
inside the php file everything you do is classic wordpress.
all this is giving you is a custom page tamplate.
Put it in your root folder. When you go to look at it, you'd look at www.mywebsite.com/test.php
It may be other ways to do this, but I rather use the rewrite API and custom query vars, to create custom routes.
A previous answer on the subject can be found here
The basic idea is to add a new url rule, catch the query var with the parse_request filter and maybe do a die or redirect to prevent the default wordpress template from loading.
I prefer this over theme templates, because with templates you need to create a page for each new url, and if that page gets acidentally deleted, that functionality would stop working.
What Pages are Not:
Pages are not Posts, nor are they excerpted from larger works of fiction. They do not cycle through your blog's main page. WordPress Plugins are available to change the defaults if necessary.
Pages cannot be associated with Categories and cannot be assigned Tags. The organizational structure for Pages comes only from their hierarchical interrelationships, and not from Tags or Categories.
Pages are not files. They are stored in your database just like Posts are.
Although you can put Template Tags and PHP code into a Page Template file, you cannot put these into the Page or Post content without a WordPress Plugin like Exec-PHP which Read overwrites the code filtering process.
Pages are not included in your site's feed.
Pages and Posts may attract attention in different ways from humans or search engines.
Pages (or a specific post) can be set as a static front page if desired with a separate Page set for the latest blog posts, typically named "blog."
More About Pages.
In WordPress to add a new page you have to log in to the admin/backend and from the pages menu you can add a new page. In this case, you can select templaes for your page and also you can create a custom page template for that page.
You may read Createing a new page in WordPress. and custom Page template in WordPress.
I'm having a bit of trouble with Wordpress, I want my blog posts to show on the news page, so I selected that page in the settings tab.
The blog posts show but I want to change the page template, I created a new template which works on all other pages but when I select it from the page template list within the 'news' page it doesn't change but instead just stays the same.
There is no blog.php file, I've even tried changing the standard page.php expecting it to be falling back onto that but it is not! I've tried changing the archives page too but nothing seems to change it!
Any ideas?
It won't be page.php which shows a single page view and you're right, there is no blog.php. Wordpress uses a template hierarchy for displaying lists of blog posts.
As you can see from this diagram - Wordpress template hierarchy
For archives like your News blog page you are looking at category.php, archive.php and then index.php (depending on your theme and which template files you've got).
Rather handily your problem is exactly the same as the working example used as a demonstration in the Codex link I've given above, so this should help explain things more.
Currently there is a bug in WordPress if you have a Posts Page set under:
Settings->Reading->A Static Page->Posts Page
If there are posts, than the page (e.g. with a slug called news) displays the post, and uses index.php from the theme.
But, with this configuration, if there are no posts, then it calls the theme's 404 page. This is definitely a bug, and has been submitted (Ticket #10822). It should be letting index.php show whatever it wants upon !have_posts(), but right now the page doesn't seem to be called at all.
My question is: is there a workaround for this bug without modifying core wp files? I'd be open to a plugin, theme changes, a custom page template, htaccess changes, etc.
I just performed a test for it. It isn't actually serving a 404, it's using index.php but since no posts are found so it falls outside of the have_posts(). Are you sure it's not a plugin or theme causing your issue? I tested this with the default theme on a base install of WordPress.
Most theme files have as part of their WP loop a catch for no posts, i.e.:
<?php else : ?>
Sorry, but you are looking for something that isn't here.
<?php endif; ?>
That might be what you're seeing. You can use a custom page template without that text (leave the loop) for yoiur posts page. And try Reveal Template | coffee2code.com to find out what template file you're looking at.