WordPress: don't show 404 when no posts exist - php

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.

Related

How can I use the block editor for a 404 page in WordPress without redirecting from /404

I have a WordPress website and a custom theme, and of course a 404.php file. I have followed this amazing guide on making the 404 page template editable with the block editor.
My components on the 404 page appear correctly when I go to /404 (or for that matter, any invalid link), however, when going to /404, the user is redirected to /404-2 This is the behavior I would like to fix. When the user goes to /abc (an invalid page) then the 404 is shown and they ae not redirected and still on /abc (which is as intended).
I checked the page in WP and the URL slug is set to /404-2 and attempting to change it to 404 just defaults back to 404. It seems that this is due to 404 being a number and default WP behavior. But this just tells me why the problem occurs; I do not know how to override or correct this behavior. Thanks in advance.
Fixed it by hooking onto do_redirect_guess_404_permalink
See https://developer.wordpress.org/reference/hooks/do_redirect_guess_404_permalink/
<?php
function stop_redirect_guess() {
return false;
}
add_filter( 'do_redirect_guess_404_permalink', 'stop_redirect_guess');
?>
As you've found, the page title "404" (or any numeric title) will always be rewritten to "404-2", this ensures the title is distinct/never confused with a numeric post/page id.
A simple solution would be to rename the page to "404 page" and update the php function to get_page_by_title( '404 page' ). This would give your page more SEO friendly url of /404-page and not require trying to change permalinks/rewriting of the .htaccess file.
An alternative to consider is migrating your custom theme to a block theme if using WordPress 5.9 or higher. This would enable you to create a true "404" page template using the Block Editor that can be edited via the Site Editor > Templates. The Site Editor enables you to add new templates, save and export them for easily creating your own templates for your custom theme. Starting with a base theme like Twenty Twenty Two is a good way to see what is possible. It's very different to creating a "classic" php-based theme; though the advantage of block templates and the Site Editor is a great benefit for future-proofing your theme.

Wordpress blog posts have no <a href="url_to_blogpost">

I have encountered a very weird issue after the last update to WP 4.4.1 and it's that the href attribute on all blog posts in the website.com/blog section are missing, so the link looks like this:
Most read articles in the previous year
The articles are normally accessible and have the href attribute from the front page of the blog, where they point to: website.com/texts/name_of_the_blogpost just not from website.com/blog
Did anyone else ever experience such an issue, and could the update of WP be the reason for it? Everything worked normally until the update.
Your theme may use a now deprecated (under 4.4.1) WordPress function. Switch to the default WordPress twentyfifteen or twentysixteen theme for a minute and see if the php function correctly outputs the URLs of the blog posts. If so, it's your theme. Talk to the theme developer or take a look at the functions in theme files, the query loop, etc. No one can help more until you post some code.

front-page.php overrides my custom page template wordpress

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.

Make special pages from WordPress theme?

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.

Adding inline wordpress comment functionality to a non-wordpress page

I have created a wordpress.org blog on my website but I do not use the actual blog page to display the blog posts. Initially I tried to style the wordpress blog to match my site, but since wordpress' theme is too constricted I decided to use "the loop" to gather the_author_posts_link, the_title, the_content, etc and display them on my main page. (reference: http://www.corvidworks.com/articles/wordpress-content-on-other-pages). The problem I am running into is the comment section. No matter what, I cannot seem to find a way to add comments to my front page using php. I know I can get my post content to display by calling the_content(), but I cannot seem to find a similar function for comments. I'm open to any solution that will allow my pseudo-blog to display comments under each post.
Thank you!
Should be <?php comments_template(); ?>. Look in your theme files for that template tag. But it working depends on if you've correctly included the blog header on your non-WP pages.

Categories