Displaying varying Wordpress content using get_template_part - php

I'm working on a Wordpress landing page template and the way I want it to work is that each content-xxxxxx contains a different block of text/images etc. Then on the Front Page I can call them like;
<?php get_header(); ?>
<div class="container">
<?php get_template_part( 'content', 'featuredcontent'); ?>
<?php get_template_part( 'content', 'textblock'); ?>
</div>
<?php get_footer(); ?>
Which is great and works fine, but I want to be able to choose the order of the blocks from within WordPress without having to edit the front-page.php file, does anyone have any idea or code that can help with this?
Thanks

Related

WP shortcode outputs shortcode as text

I'm trying to use shortcode in post to display gallery but on website it puts out shortcode itself as text.I'm using
<?php the_content();?>
in php file.
Same shortcode works well with
<?php echo do_shortcode('[shortcode here]')?>
but in this case I need it to be shortcode in post editor.
No need to write PHP code and just put [shortcode here] in the WordPress Post Editor.
If you want to use short code in wordpress post editor then no need to write php code.
And if you want to user the_content() in any wordpress .php file then you need to write and follow code pattern.
for example (example of single.php)-
<?php
while (have_posts()) : the_post();
the_title();
the_content();
endwhile;
?>
Lets consider that you are on the checkout page, in the dashboard > Pages > Checkout Page you enter this [woocommerce_checkout] and
In page-checkout.php file of your wordpress you can write your custom code to make it work.
Here is an example of my custom code :
<?php
if (have_posts()) : while (have_posts()) :
the_post(); ?>
<div class="container">
<div class="checkout-page my-4">
<?php the_content(); ?>
</div>
</div>
<?php endwhile; endif; ?>
Sorry for bad English :-P
Try adding following code in your active themes functions.php
add_filter( 'the_content', 'do_shortcode' );

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(); ?>

How to make custom WordPress theme show pages, NOT posts

I have recently created a custom WordPress theme, following a guide I found online.
However, all of the guides I came across explain how to set up a blogging site and do not explain how to set up a static website page. I am guessing that it is something to do with this code in my index.php:
.
>. get_template_part( 'content', get_post_format() );
>. endwhile; endif;
>. ?>
When I remove this, it removes the blog layout. However, I do not know what to amend to make a static page layout.
Can anyone help please?
get_post_format() Returns the post format of a post. This will usually be called in the the loop, but can be used anywhere if a post ID is provided. and its usually found on the posts pages/blogs.. therefore Its used in a theme when you want to display blog posts.
As you you might know that wp have different posts types. let's just take two of them.
Post (Post Type: 'post')
Page (Post Type: 'page')
The get_post_format() is used to get the format of a post,(blog Post) if you open your wp dashboard and start a new post or editing existing post you will see different posts formats types
As you can see from the image above, the red highlited part is the post format, that is what you are requesting when u use get_post_format() wp treats that template as it should display posts, but not pages.
If you want the content of the page you then need to use the_content()
Therefore this
get_template_part( 'content', get_post_format() );
endwhile; endif;
becomes :
get_template_part( 'content', the_content() );
endwhile; endif;
alternatively :
<div class="YourContainer">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<div class="YourContentDiv">
<?php the_content();?>
</div>
<?php endwhile; ?>
<?php else:
echo "no content found";
?>
<?php endif; ?>
</div>
Hope this helps, Goodluck
If you look at the page template hierarchy you will see which template is used in which circumstances:
https://developer.wordpress.org/themes/basics/template-hierarchy/
For example if you create a front-page.php template it will take precedence of other templates.
If you set a static home page in settings then the template used will depend on the hierarchy first a custom named selectable template if this isn't set then a page-$slug.php template e.g. home-page.php then if that doesn't exist then a page-$id.php e.g 15-page.php if this doesn't exist then page.php will be used then singular.php then index.php

Custom WordPress template loads only header & footer

I have a weird problem. I'm trying to make custom template for WordPress.
index.php works perfectly fine - everything what should load, loads with no problems.
index.php:
<?php get_header(); ?>
<?php if ( is_home() &&function_exists('get_template_part')) get_template_part( 'content', get_post_format() );?>
<?php get_footer(); ?>
But when I create a new page, like promotions.php and put custom code, It does not show up on the page. I can see only header & footer section. It looks like I didn't put any code at all.
You can make custom page templates like so
<?php
/**
* Template name: custom-name
*/
get_header();
if (is_home()) {
get_template_part('content', get_post_format());
}
get_footer(); ?>
And then inside WordPress you can edit the page. On the right side below publish you can see default template. Choose your template and hit save.
Edit:
See the image below for further instructions.
Use flush_rewrite_rules() function one time in functions.php.
All should works after that.

Creating a custom template page in wordpress causing other features to stop working

I'm new to wordpress and i'm trying to create a custom template. I'm using the default twentyfifteen theme. I went to wp-content/themes/twentyfifteen and copied page.php to a new file called page_with-contact.php and added this comment on the top :
/*
Template Name: Page with contact
*/
I made no other changes to the template.
I then went to the admin site and changed one of the pages to "page with contact".
When I open the page I see that the affix on the left menu is not working and the responsive menu is not working either.
I followed a pretty simple tutorial here so I'm just wondering what am I doing wrong.
EDIT
Following #masiorama's answer and the comments below is crated a child theme, moved the template file to the child theme and renamed it to page-with-contact.php, This is the content of the template
<?php
/*
Template Name: Page with contact
*/
get_header(); ?>
<div id="primary" class="content-area">
<main id="main" class="site-main my-content-page" role="main">
<?php
// Start the loop.
while ( have_posts() ) : the_post();
// Include the page content template.
get_template_part( 'content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
?>
<div class="hentry entry-content contact-form">
<?php
echo do_shortcode('[contact-form-7 id="19" title="contact form 1"]');
?>
</div>
<?php
// End the loop.
endwhile;
?>
</main><!-- .site-main -->
</div><!-- .content-area -->
<?php
get_sidebar();
get_footer();
?>
Now I have several problems :
As you can see the sidebar, heading and footer are all included but the affix and responsive menu are still not working.
The contact form (which previously worked fine) is now not showing at all.
I'm unable to enqueue the parent's rtl.css file.
Appreciate any further guidance.
As far as I know your page file should be named:
page-with-contact.php
and not:
page_with-contact.php
Be sure that it contains at least some wordpress function calls like:
<?php get_header(); ?>
<!-- stuff -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Check this for more details: http://codex.wordpress.org/Page_Templates
Looks like you are calling a template within a template. 'get_template_part( 'content', 'page' );' should be the_content();. Good chance that's whats messing you around.
Need this WordPress hooke to call in your template
get_header();
get_sidebar();
get_footer();

Categories