singular.php doesn't show the content - php

I'm trying to develop my first wordpress themes, but now I have a quite weird problem. My singular.php page seems to not work. whatever I put in the page doesn't show up. as if the wordpress core doesn't recognize the file. while I used the same content for another theme for the same website and it worked properly.
It doesn't even show my header and footer. and when I inspect the page it's completely empty!
I tried setting the index.php file, but the same result was there :( nothing to show.
the content of the page is:
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php get_header();?>
</head>
<body>
<?php get_template_part('template-parts/header/header-bar');?>
<?php
if (have_posts()):
while (have_posts()) : the_post();
the_content();
endwhile;
else:
echo '<p>Sorry, no posts matched your criteria.</p>';
endif;
?>
<?php get_footer();?>
</body>
</html>
UPDATE
for some reason, this content works in page.php, I don't know why singular.php and index.php don't work. according to wordpress Template Hierarchy they should show the content in case of existence.

Related

Why won't my html render in my header.php file?

I have a barebone header.php file that looks like this so far
<!doctype html>
<html>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<header class="testB">
<p>header section</p>
</header>
The header loads into the index.php file perfectly fine, however it doesn't render my <header> tag with the text. I just checked out a couple videos and tutorials to see if there's something else I need to do and they all seem to dive right into customizing everything and have it render. The only thing present is the title of my website which wordpress injects on its own. Any clue as to why this won't just work like it seems to in the tutorials?
Here's my index.php and footer.php just so you guys can see the overall setup.
index.php
<?php get_header(); ?>
<p class="test">body</p>
<?php get_footer(); ?>
footer.php
<footer>
<p>footer</p>
<?php wp_footer(); ?>
</footer>
</body>
</html>
Now that I posted this is just hit me that WP strips the <p>footer</p> out of the footer.php as well while injecting it's "powered by wordpress" snippet. I haven't enabled any WP theme support in the functions.php file yet as I haven't seen anyone doing anything like that before diving right into customizing their html.
UPDATE
I added the wp_body_open() function as suggested below by #amerinediary and my code still isn't rendering in the browser.
You're missing the wp_body_open() function.
Fire the wp_body_open action.
Which is getting triggered after the opening body tag, via the do_action( 'wp_body_open' ) hook.
Source # https://developer.wordpress.org/reference/functions/wp_body_open/
Following are default blank template part boiler plate
//header.php
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' );?>">
<?php wp_header(); ?>
</head>
<body <?php body_class(); ?>>
<?php wp_body_open(); ?>
//index.php
<?php get_header(); ?>
<?php get_footer(); ?>
//footer.php
<?php wp_footer(); ?>
</body>
</html>
Additionally <?php wp_footer(); ?> shouldn't be included inside a container as Wordpress is using it to render <script>'s tags.

wordpress get_header() on a page template getting the head content inside of the body tag

I am having trouble with my Wordpress template page. When calling get_header() in the template page, the content of the head is appearing inside the body tag and causing trouble. I couldn't find a solution for this.
This is what I mean :
page inspect element
page template :
<?php
/*
* Template Name: sub services
*/
get_header();
?>
<h1>sub page</h1>
<?php
get_footer();
?>
header.php :
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo('charset'); ?>">
<?php wp_title();?>
<link rel="icon" type="image/png" href="/favicon.png">
<link href="https://fonts.googleapis.com/css2?family=Lemonada:wght#600&display=swap" rel="stylesheet">
<script src="https://kit.fontawesome.com/90928eb6b7.js" crossorigin="anonymous"></script>
<?php wp_head(); ?>
</head>
<body id="theme-body" <?php body_class(); ?>>
<header>
<h1>Main Header</h1>
</header>
and I have added a page in wordpress admin and linked it to the template.
wordpress add page
Everything works fine except that the content of the head is in the body tag. when calling get_header(). In the template.php I have checked my error log and its fine. I dont know what is causing this problem or what I'm doing something wrong.
Your header.php file contains the full body (including the closing </body>) and even the closing </html> tag. Your page template then appends to that some more HTML and a footer - that's invalid HTML and can't really be intended, is it?
Browser will probably try to fix those errors by adding missing or omitting superfluous tags, but in this case none of that will come up with the intended result - it will rather lead to a result like the one you are seeing.
The header.php file can contain the opening <body> tag, but not the closing </body>. But it should contain the Wordpress loop, and inside that the_content() to load the actual page content, and also the other WP functions to fetch heading, featured image etc.
After that, you usually fetch the footer using get_footer();, which might contain the closing </body> tag, if that's not in the page template.
So, the biggest problem in your code are the closing </body> and </html> tags and the resulting invalid HTML.

WordPress custom theme going to 404.php

I created a custom theme with (for now) nothing special in it. My functions.php only contains an action hook to wp_enqueue_scripts and I have the following files:
header.php
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="profile" href="http://gmpg.org/xfn/11">
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
index.php
<?php
get_header(); ?>
INDEX
<?php get_footer(); ?>
footer.php
<?php wp_footer(); ?>
</body>
</html>
archive.php
<?php get_header(); ?>
ARCHIVE
<?php get_footer(); ?>
404.php
<?php get_header(); ?>
404
<?php get_footer(); ?>
functions.php
<?php
# WP Basics
//include_once(get_stylesheet_directory() . "/functions/menus.php" );
include_once(get_stylesheet_directory() . "/functions/scripts.php" );
Now I have the following issue, when I go to the front page it shows the correct page, but when I got to any other page, archive, or what so ever it ends in the 404.php.
I did check the following:
Disable all plugins (no result)
Enable default theme (twenty-seventeen, everything is fine)
Permalinks are on "/%postname%/" when I set it to "plain" everything works
Check if there is a .htaccess somewhere where it doesn't belong (isn't)
Check if filenames are correct (they are)
So needless to say I'm really stuck, I have made several custom theme's and never had this issue before. Does anyone have any idea?
Kind regards,
Augus
You are missing style.css file in your theme as in your question you haven't mention style.css.
And style.css is most required file to create the theme in wordpress so please add style.css in your theme and it will work for sure.
Alright so i fixed it, thanks for all the help.
I added a rewrite rule which wasn't correct, then i deleted it again. But somehow this rewrite didn't go away and was only active for my theme.
I was under the assumption that when i saved the permalinks again the rewrite rules were flushed, but this wasn't the case. So i added the following snippet to my functions.php and it worked.
add_action( 'init', 'shoe_flush_rewrite_rules' );
function shoe_flush_rewrite_rules() {
flush_rewrite_rules();
}
Remember to delete this snippet when everything works again, cause it doesn't need to be run everytime.

How to include a php file with in WordPress header file

Hi there I seem to have run into an issue that I'm not quite sure about. I have a just developed a new WordPress theme for my personal website and I have some include files in the header which aren't being added.
<!DOCTYPE html>
<html lang="<?php language_attributes(); ?>">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" href="<?php echo get_stylesheet_directory_uri(); ?>/favicon.ico" />
<title><?php wp_title(); ?></title>
<!-- Google Analytics Tracking-->
<?php include_once("/inc/google-analytics-tracking.php") ?>
<!-- Drip Tracking -->
<?php include_once("/inc/drip-tracking.php") ?>
<!-- Hotjar Tracking -->
<?php include_once("/inc/hotjar-tracking.php") ?>
<!-- ConvertFlow Tracking -->
<?php include_once("/inc/convertflow-tracking.php") ?>
<!-- Share This -->
<?php include_once("/inc/share-this.php") ?>
<?php wp_head(); ?>
</head>
You can see my site here: https://www.elliottdavidson.com/. I'm not sure why the includes arent working? If anyone could shed any light on this it would be great.
/inc/ - it's an absolute path, if you want to use relative paths you have to write this string as inc/ because / means "root" directory of the linux file system.
I figured this out, in the end, the files don't want to be added into the header.php file but into the functions.php file like so:
// Google Analytics Tracking
require get_template_directory() . '/inc/google-analytics.php';

Appending URL parameter at every request only if parameter already exists to save opt-out option responsive design

I have a responsive Wordpress site: www.2eenheid.de. My client wants an option on a mobile to view the site in fullsize, an opt-out responsive option. Now, I found this solution:
http://css-tricks.com/user-opt-out-responsive-design/
Which uses PHP to remove the meta name="viewport" tag when the URL parameter ?resp=no (URL becomes: www.2eenheid.de/?resp=no) is called. By removing the meta name="viewport" tag the site becomes fullsize on mobile. See code below:
<head>
<title>My cool site huzzah</title>
<?php
if (!$_GET['resp'] == 'no') { ?>
<meta name="viewport" content="width=device-width">
<?php } ?>
</head>
<body class="<?php if (!$_GET['resp'] == 'no') { echo "resp"; } ?>">
This changes the current page to fullsize, but if I click a different link it changes back to responsive, which is logical because the link doesn't contain the parameter ?resp=no anymore.
Now my client has the requirement that if a user wants the fullsize website, it needs to stay fullsize even after clicking a different URL until a user changes it back to responsive.
So my question is how can i save the ?resp=no parameter once clicked for every URL request, until a user changes it back to responsive by e.g. clicking a different link (maybe with a different parameter)?
I've tried googling but I can't find any good solutions. I've seen people suggesting session values but I find that very hard to understand and some people say that thats not good practice because session values are used for logins.
Any help would be highly appreciated.
EDIT 3:
The suggestion below seems good, but I'm quite new to PHP sessions. I changed my code with the suggestion below to this:
<?php ini_set('display_errors', true);
session_start();
if(isset($_REQUEST['resp'])) {
$_SESSION['resp'] = boolval($_REQUEST['resp']);
}
// Check if enabled
$enabled = isset($_SESSION['resp']) && $_SESSION['resp'];
?><!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>" />
<?php if($enabled): ?>
<meta name="viewport" content="width=device-width">
<?php endif; ?>
<title>LALALA</title>
</head>
<body <?php body_class(); ?> id="<?php if(isset($_SESSION['resp']) && $_SESSION['resp']) { echo "resp"; } ?>">
Can anyone help with this?
session_start();
// ...
if(isset($_REQUEST['resp'])) {
$_SESSION['resp'] = boolval($_REQUEST['resp']);
}
And :
<?php if(isset($_SESSION['resp']) && $_SESSION['resp']): ?>
<meta name="viewport" content="width=device-width">
<?php endif; ?>
You could probably use cookies too as said in a comment, but i never looked how it works.
EDIT : An example :
<?php
// If the parameter is in the URL, enable/disable (else leave as it currently is).
if(isset($_REQUEST['enableMode'])) {
$_SESSION['enableMode'] = boolval($_REQUEST['enableMode']);
}
// Check if enabled
$enabled = isset($_SESSION['enableMode']) && $_SESSION['enableMode'];
?>
<?php if($enabled): ?>
<meta name="viewport" content="width=device-width">
<?php endif; ?>
Enable mode
Disable mode
Change page without changing anything
Usings sessions for this is too much, a better mechanish exists, called "output_add_rewrite_var". Using this system, we can say the following:
<?php if(!isset($_GET['resp'])): ?>
<meta name="viewport" content="width=device-width">
<?php else:
output_add_rewrite_var("resp", "1");
endif; ?>
The above code fits perfectly in the view place of a MVC system, since it is a view specific option. The code works by looking if $_GET['resp'] exists or not, and depending on that, if it exists, calling output_add_rewrite_var("resp", "1"); to put itself in other url's, and if it doesn't exists, it adds the meta tag.

Categories