I'm building my own template for Wordpress and I'm bumping into an issue when displaying the content of a post.
I have already built one page template for the home page and it works fine. The loop outputs what I want to display. Now I'm building the template to display an article but the loop doesn't return anything.
Here is the code of the page template:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
This results in displaying the "sorry, no posts matched your criteria" message when i click on one of the links on the homepage. The strange thing is that the page really exists (it has ID=26 as you'll see here below):
<?php
$post = get_post(26);
$title = $post->post_title;
echo $title;
?>
This works and displays the expected title. I have tried get_the_ID(); to get the post ID but it returns an empty variable.
There is probably something missing in my template but I can't figure out what.
Any idea?
Thanks
I have found the issue and it had nothing to do with the template itself.
I discovered what's wrong by using one of the standard Wordpress themes (twentyfifteen) where every post led me to a 404 even if I clicked on a post from the Admin UI. I swapped back permalink structure to the ?p=123 option and there everything working. Definitely a permalink structure problem.
The problem was coming from the polylang plug-in. I'm using a network of wordpress site for which I have made this plug-in available. I did not need this plug-in for this particular site but somehow it needed to be active anyway. So I activated polylang and configured it to have only one language and now it works.
Got new gray hair in the process but if that can help anyone...
Thanks for your help!
Laurent
I don't think, that this template can output any posts like this. For WordPress
/*
Template Name: PAGE
*/
indicates: this is a page-template which you can asign to the content of a page but not to a post.
When you want to show a specific post on such a page you will have to:
Asign the template to the page in WP-backend
Add a new query to the template to show the post
Like:
<?php
/*
Template Name: PAGE
*/
define( 'WP_USE_THEMES', false );
get_header();
?>
// The new Query
$args = array (
'p' => '26',
);
// The Query
$query = new WP_Query( $args );
<div class="wrapper">
<div class="sidebar">
<?PHP get_sidebar(); ?>
</div>
<div class="main">
<div class="section group">
<div class="col col12-12">
<span>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</span>
</div>
</div>
</div>
</div>
<?php get_footer(); ?>
But as this is very complicated I would recommend to:
delete the /* Template Name: PAGE */ from the template
save it as single.php or more specific as single-26.php
link to the desired post in your wp-menu
Related
I am using Understrap to develop a custom theme and when I add a new page template, it does not show up on the page attributes dropdown.
These are the fixes I have tried and they don't work:
1) Activating to another theme and changing back to Understrap.
2) Using a plugin to clear cache due to the caching issue of wordpress 4.9.
3) Bumping Theme Version.
4) Updating Wordpress.
Nothing Seems to work and I am in a fix!.
I am using MAMP for local dev.
folder structure
page templates folder, I've highlighted the files that I have added, none of them show on the dropdown
Thank you for any help that you can provide and please do let me know if you need more info.
EDIT:
<?php
/**
* Template Name : Portfolio Page Template
*
* template for the portfolio masonry style
*
* #package understrap
*/
get_header();
$container = get_theme_mod( 'understrap_container_type' );
?>
<div class="wrapper" id="full-width-page-wrapper">
<div class="<?php echo esc_attr( $container ); ?>" id="content">
<div class="row">
<div class="col-md-12 content-area" id="primary">
<main class="site-main" id="main" role="main">
<?php echo '<div class="card-deck">' ?>
<?php
$args = array( 'post_type' => 'portfolio', 'posts_per_page' => -1);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="card">
<img class="card-img-top" src="<?php get_post_thumbnail_url($post->ID); ?>" alt=" ">
<div class="card-block">
<h4 class="card-title"><?php print get_the_title(); ?></h4>
<p class="card-text"><?php print get_the_excerpt(); ?></p>
</div><!--card-block-->
<div class="card-footer">
<small class="text-muted"><?php get_the_terms($post->ID, array( 'Skills' )); ?></div>
</div>
</div><!--card-->
<?php endwhile; ?>
</div><!--#card-deck-->
</main><!-- #main -->
</div><!-- #primary -->
</div><!-- .row end -->
</div><!-- Container end -->
</div><!-- Wrapper end -->
<?php get_footer(); ?>
This is the template that I want to Include.
EDIT 2:
As #Alexander Suggested I included that code in the functions.php. It Didn't work..I then included a small snippet to display the page template array on the index page and modified the code snippet like so.
if( is_page_template( 'page-templates/page-portfolio.php' )){
echo "template exists";
}
else{
echo "this template does not exist";
}
This is the result:loop and array
It seems like space issue. Can you make only below change in your custom template file:
From
Template Name : Portfolio Page Template
To
Template Name: Portfolio Page Template
I checked the same in my local set up and confirmed above. It will work for you.
On the one hand, You need to adhere to the file structure that is adopted in Wordpress.
So, page templates must been putted in the root folder of theme or child theme.
But also there is always another way, you can include the file in functions.php:
if ( is_page_template( 'page-templates/my-template.php' ) ) {
include_once 'page-templates/my-template.php';
}
OK I have found the problem, it was quite silly, There is space between name and :, well it shouldn't be there. Thank you for all your answers and suggestions.
I installed a plugin called Admin Guide Dashboard Widget. The idea is that it adds a widget to the WordPress Control Panel (the first screen that shows up upon login). On this widget you choose a blog/post category and then every post you write with that category shows up in that widget.
Well it didn't work at all. The widget showed all of my posts. I tried to change the category on the widget, but I just couldn't get it to work so I uninstalled it. Then I realized the the_content() of all my blog posts was gone. I tried to install the plugin again, I tried to change the category I had set for the plugin, nothing worked. I uninstalled. The content part of my posts is still not showing up at all.
Before I installed that plugin I had only created one category, called "Nyheder"
Now I'm not sure if all of my posts were categorized as "Nyheder" or if they were simply uncategorized. I tried unchecking "Nyheder" from my posts but it gets checked again automatically. I don't know if my posts should be uncategorized (if that's even possible) or if the problem lies somewhere else.
The news archive page of the website in question is here. This page actually still shows the content. But if you click on a posts's title to go to its page, you will see that there is no content.
Here is the code on my single.php page, but I really doubt this is the problem.
<?php get_header(); ?>
<div id="pageHead">
</div>
<div id="main2" class="page clearfix">
<div id="content" class="threeFourth clearfix" style="width:100%;"><?php get_sidebar(); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class(); ?>>
<h1 style="display:inline;"><?php the_title(); ?></h1>
<?php
$page = $_SERVER['REQUEST_URI'];
if (substr($page, 1, 14) == "events_listing") {
echo "<h2>" . do_shortcode('[events-listing-date]');
if(do_shortcode('[events-listing-end-date]') != get_the_modified_date('d-m-Y') ) {
echo " til " . do_shortcode('[events-listing-end-date]') . "</h2>";
}
}
?>
<br>
<div class="meta clearfix" style="display:inline-block; padding-top: 10px; font-size: 14px;">
<?php _e('Skrevet af', 'themetrust'); ?> <?php the_author_posts_link(); ?> <?php _e('d.', 'themetrust'); ?> <?php the_time( 'j. F, Y' ) ?><br><br>
</div>
<?php edit_post_link(__('Edit Post', 'themetrust'), '<p>', '</p>'); ?>
<?php the_content(); ?>
</div>
<?php endwhile; ?>
</div>
</div>
It looks like it had nothing to do with the plugin, but instead with another change I had made just before installing it. If you look at the code above, I defined a variable called $page. It turns out if I inserted the_content() before that it worked fine, but not after! So I changed the name of the variable to something else and put the_content() back in its place. Voila! I guess there was a conflict with the variable name $page and it somehow affected the_content().
I'd like my homepage to display my latest posts which are portfolio projects of mine. Beneath these project thumbnails I've got my static content which I'm using the Repeater add-on from Advanced Custom Fields to grab. I cant get it all to work on the same page... either get the blog to work or the ACF stuff to work. Never both because one needs to be a designated static page and one needs to be a posts page. I don't understand how to tie it together and make it appear as one page.
I've experimented with the settings in the reading panel..
I've tried using front-page.php
I've read up on the WP hierarchy etc
I've tried using templates...
I've tried wp_reset_postdata(); which I read about elsewhere on Stack Overflow.
What settings must I use in the reading panel. Do I need to use a template file?
Here's the code I'm working with, I've split the code between templates and different files already, but just for ease of reading its all together here (maybe that's the right way to do it anyway, I wouldn't know..)
<!-- The posts/portfolio items -->
<?php get_header(); ?>
<div>
<?php if(have_posts()) : ?>
<ul>
<?php while ( have_posts() ) : the_post(); ?>
<li>
<!-- Permalink,title and post thumbnail here (omitted) -->
</li>
<?php endwhile; ?>
</ul>
<?php else: ?>
<h2>No Posts found</h2>
<?php endif; ?>
</div>
<!-- Now for the ACF Stuff -->
<?php if(get_field('care_list')): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
Please help a frustrated learner! Thanks in advance.
It looks like you're going to need to add the post id of your 'home page' (the one with the ACF repeater on it) to the get_field() function like so:
<?php $post_id = **post_id_of_your_homepage_here**; ?>
<?php if(get_field('care_list', $post_id)): ?>
<?php while(has_sub_field('care_list')): ?>
<div class="grid_2 what-i-care-about gap">
<div class="important-img-container">
<?php the_sub_field('care_list_image'); ?>
</div>
<h3><?php the_sub_field('care_list_title'); ?></h3>
</div>
<?php endwhile; ?>
This is because the $post_id parameter defaults to the current post being brought up by wordpress, which means ACF is looking for the repeater on the last Portfolio item/post you are displaying. If you set the $post_id parameter to the ID of your homepage, ACF will instead look for the repeater on that page.
Source: http://www.advancedcustomfields.com/resources/functions/get_field/#parameters
If I'm understanding correctly, you have a bunch of posts and you want to display a list of them with title and post thumbnail on your homepage, and then display a custom field you've assigned to the homepage underneath the list of posts?
Step 1: Create a new page template by copying page.php, changing the name to homepage.php and adding this to the top:
<?php
/*
Template Name: Homepage
*/ ?>
Step 2: Crete a Wordpress page called "Homepage" and in the attributes module in the right sidebar of the page creation tool, select "Homepage" as your page template.
Step 3: In your reading settings, change the front page from posts page to "Homepage." Now your homepage is your page called "Homepage."
Step 4: Make something like this the full code on your new page template homepage.php. It will output your posts list followed by your page custom field:
<?php get_header(); ?>
<?php $the_query = new WP_Query( $args );
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_post_thumbnail(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php if(get_field('repeater_field_name')): ?>
<?php while(has_sub_field('repeater_field_name')): ?>
<?php the_sub_field('sub_field_1'); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php get_footer(); ?>
I have a wordpress plugin (broadstreet) which is a company directory and has list of companies. But I want to display in different style to match the rest of the website. Now all the company details are stored in the plugin. Is it possible for me to read the data stored in the plugin and display in a page and publish the same using wordpress? If so how?
Any help is greatly appreciated. Thanks
Yes you can, In this case, what I do is create a custom template page. For that you can go to theme you are refering and make a copy of page.php file and rename it. let's say it is directory-page.php.
Next go to admini panel and create a new menu item and set it is template to derectory-page.
Now you can open it and customise as you expect it.
get_header(); ?>
<div id="primary" class="site-content">
<div id="content" role="main">
<?php while ( have_posts() ) : the_post(); ?>
<?php //get_template_part( 'content', 'page' ); ?>
<?php// comments_template( '', true ); ?>
<?php endwhile; // end of the loop. ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
If you want to remove the blog content and call your directry displaying method.
simply, comment out
<?php //get_template_part( 'content', 'page' ); ?>
<?php// comments_template( '', true ); ?>
<?php echo call_directory_display();?>
Now you have to implement call_directory_display() method in your plugin to show content in derectory-page.
If you have any questions ask in comments.
So i'm quite a beginner, trying to make a custom theme. In a page i want to have a gallery. Uploaded images, made a gallery all fine.
When I view the page it outputs only the shortcode:
[gallery orderby="post_date"]
my page.php file basically has:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo $content->post_content ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
I really don't understand how to get this to show correctly, any pointers would be greatly appreciated. Cheers, Matt
get_page returns the raw page data. There are a few ways to do what you want:
BAD WAY:
<?php $content = get_page( $page_id ) ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<?php echo "<h1>".$content->post_title."</h1><br>" ; ?>
<?php echo do_shortcode($content->post_content); ?>
</div>
</div>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
do_shortcode() renders all registered shortcode that's found within a given string. In this case, your page's content will have all shortcode rendered before being written to the document. I say this is the "bad" way, only because it doesn't follow the usual Wordpress format. Which leads us to the:
BETTER WAY:
<?php if(have_posts()) : while(have_posts()) : the_post(); ?>
<div id='content' class='shadow'>
<div id='innercontent'>
<!---page title-->
<h1><?php the_title(); ?></h1><br>
<?php the_content(); ?>
</div>
</div>
<?php endwhile;endif; ?>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
This is what's called "The Loop". It is pretty much the standard for all Wordpress Themes in retrieving all Post or Page Data, as well as running queries against the database.
I would suggest getting to know it, as well as running Wordpress queries to modify the loop using WP Query. This is getting into a more complex area of Wordpress, but it will help you in the longrun with figuring out how to gather up all of the posts and pages you want to retrieve in your theme that aren't provided by Wordpress' globals.
Good luck.