get_template_part not working on wordpress - php

I have been working on this for hours and don't know what I'm overlooking or not understanding. I'm trying to use get_template_part to call another php document. When I run the code below there is nothing in the div.content. I don't understand why my partial-banner.php is being called.
front-page.php
<?php get_header(); ?>
<div class="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/partial', 'banner' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php get_footer(); ?>
partial-banner.php
<section class="banner">
<h1>Does this work</h1>
</section>
and here's an image of my file structure.

You could just use a simple PHP include, instead of using WordPress logic
<?php include('template-parts/partial-banner.php'); ?>

Related

how to get full width in my wordpress page template?

I have to create a full-width page template in Wordpress. But I don't understand why, even if I only call my 'sidebar-bas' I also have the 'sidebar-droite' displayed :/
i hope someone could help me
<?php
/*
Template Name: Full Width
*/
?>
<?php get_header();
if(have_posts()): while(have_posts()): the_post();?>
<section id="pages" class="haut avecAside">
<section id="pages" class="haut">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</section>
<?php endwhile;
endif;
get_sidebar( 'sidebar-bas' );
get_footer();?>
Verify that your sidebar-bas template file does not include get_sidebar( 'sidebar-droite' ) in it.

Wordpress Custom Theme: Plugins Won't Load or Display Only Code

This is my first attempt at creating a theme purely from scratch. Before this I just used underscores_me and made most of my changes to style.css and left the majority of PHP alone, because I'm a novice with it.
My issue is that plugins are not working. None of the ones I have installed work. At this point I have been trying plugins that create an event calendar, but I'm assuming that any and all plugins will have issues. In the areas that are meant to display the calendar, there are two things I'm seeing. Plugin-generated pages show nothing at all (aside from the theme visuals) and plugins that are inserted into an admin-created page display plugin-generated code.
I am using WampServer. I have wp_footer(); and wp_head(); in the correct places. My functions.php file was created from the example found at https://scanwp.net/blog/create-a-wordpress-starter-theme-from-scratch/ and the only adjustment I have made to it so far is to remove the fontawesome line of code.
My index.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
<?php get_footer(); ?>
And my page.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
<?= get_post_field('post_content', $post->ID) ?>
<?php get_footer(); ?>
First of all, you have to understand that in your displayed files, there are no functions that recall objects that will later show the page content.
Then, in your index.php file there is an error, besides what was said above, because you're calling the_title () (function) that extrapolates the title of a post or page via the post object, which in this case should be extracted within the while loop contained within the if condition.
Then try editing the files as follows.
index.php
<?php
get_header(); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if( is_singular() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts to display</h1>
<?php endif; ?>
<?php get_footer(); ?>
and page.php
<?php
get_header(); ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
However, within the wordpress codex you will find all the guides well written on any type of function, look no further.
source: https://codex.wordpress.org/

WordPress Blog Not Displaying Posts

I've created a custom template using the twentythirteen template. When I activate the template the blog posts disappear from the front page. I've used the same process to make the new template as I did for my own.
You can see my blog page here, http://www.gnarlydigital.com/blog/ and the new blog that I'm working on is here, http://www.flowdirectpumps.com/news/.
I've tripled checked all the code and both templates are identical. I've uploaded the Gnarly Digital template and it works.
Can anyone see anything obvious?
This is the index.php file
<?php get_header(); ?>
<div class="container pageContent">
<div class="row">
<div class="col-md-12">
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php twentythirteen_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'content', 'none' ); ?>
<?php endif; ?>
</div>
</div>
</div>
<?php get_footer(); ?>

Continuing a WordPress PHP Loop

I'm currently trying to develop a custom Wordpress theme, and on my homepage I need to add a second content block. I am using a plugin to do this, which simply requires me to add the following where I want the content block to be.
<?php the_block('Latest Products')?>
However when I add this it seems to have no effect which I believe is due to the formatting of my php. I'm fairly new to php, so any help is greatly appreciated.
My code is as follows - I've cut out the best part of the HTML. I think it's something to do with that 'endforeach' tag?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php the_block('Latest Products')?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
EDIT
Okay, so apparently it needs to be put outside the loop, however it still won't work. Any ideas?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php endwhile; endif; ?>
<?php the_block('Latest Products')?>
<?php get_footer(); ?>
This mostly depends on what the plugin is actually doing because your code syntax is correct.
If you are using the Multiple Content Blocks plugin and are using the latest Wordpress version 3.5.1 then I believe the plugin may not be compatible. I'd check the version compatibility of the plugin to your Wordpress install as this could be your issue.
EDIT:
The plugin works by applying a filter to the function the_content() so that is why it only works by declaring the_block() before the_content() function is called.
A solution could be to capture the output the_block() and use print it out later, as an example:
<?php
ob_start();
the_block('Latest Products');
$latest_products_contents = ob_get_contents();
ob_end_clean();
?>
<!-- Further down.. -->
<?php echo $latest_products_contents; ?>

Serious php get_template_part() Wordpress issues - easy fix?

I am needing some serious Wordpress expertise from others. I am in the process of building a custom theme, read about the get_template_part() method and decided that would help in cleaning up and organizing my code. I updated my theme to use this method several places and now the site is completely broken...not a single thing will load!! :( I would really appreciate any help! I will copy and paste a few of the php files below. Maybe someone with more experience with using this method can point out the problem. THANKS!
page.php
<?php get_header(); ?>
<div id="content" class="content">
<?php while ( have_posts() ) : the_post(); ?>
<!-- About page -->
<?php if (is_page('about')) ?>
<?php get_template_part( 'page', 'about' ); ?>
<!-- Media & Gallery page -->
<?php if (is_page('media-gallery')) ?>
<?php get_template_part( 'page', 'mediagallery' ); ?>
<?php endwhile; // end of the loop. ?>
</div> <!--/end content -->
<?php get_footer(); ?>
page-about.php
<div id="about">
<div class="text">
<h2 class="about"><?php echo get_the_title(); ?></h2>
<?php the_content(); ?>
</div>
</div>
page-mediagallery.php
<div id="pic-gallery">
<?php include_once 'includes/pwaplusphp/albums.php'; ?>
</div>
index.php
<?php get_header(); ?>
<div class="content">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<br class="clr"/>
<?php endwhile; ?>
<?php content_nav( 'nav-below' ); ?>
<?php else : ?>
<article id="post-0" class="post no-results not-found">
<header class="entry-header">
<h3 class="blog"><?php _e( 'Nothing Found' ); ?></h3>
</header> <!-- .entry-header -->
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found for the requested archive. Perhaps searching will help find a related post.'); ?></p>
</div> <!-- .entry-content -->
</article> <!-- #post-0 -->
<?php endif; ?>
</div> <!--/end content -->
<?php get_footer(); ?>
content-gallery.php
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<div class="post-date"><?php the_time(get_option('date_format')); ?></div>
<h3 class="blog"><?php the_title(); ?></h3>
<div class="post-content"><?php the_content(); ?></div>
</div>
Any thoughts why these may not be working? All of these files are under the root directory of the theme so the get_template_part() method should be finding all of the files. What's weird is no code is being spit out by Wordpress. aka. Since I've started using this method, not a single line of code is spit out in my browser when I inspect the source.
I am using the newest version of Wordpress and Google Chrome for my browser.
I also have this code at the top of each of my .php files, but that shouldn't mess up anything because it's commented as seen below:
<?php
/**
* I put a description of what the file does here.
*
* #package Keynote_WP_Themes
* #subpackage Rhymz_Suhreal
* #copyright Rhymz Suhreal, 2012
* #author Kyle Affolder <myemail#example.com>
*/
?>
I don't have the slightest clue as to what I'm doing wrong. :(
Ideas coding friends?!
Using your page-about.php as an example, you should be calling it like this in the template it's to be place in:
<?php get_template_part( "page-about" ); ?>
The template needs to be in the root of your theme directory so you can use "page-about" without the .php, and not have to indicate the location/directory the file would otherwise be located.
I've run into this a few times before as well - I can't really explain why yet as I'm still digging through the documentation, but it seems you can't call template parts this way when you are inside a post loop ;)
Anytime you are inside the loop, try using this instead:
<?php include(get_query_template('page-about')); ?>

Categories