Display Gutenberg content in page template PHP file - php

Firts time I trying to create page using WordPress with new editor called Gutenberg. In Wordpress I Always using:
<?php the_content(); ?>
to display content but it is not working with Gutenberg. Is there some new way to display content when I using Gutenberg in PHP page templates files?

You have to call the_content(); inside the WordPress while loop like below.
if ( have_posts() ) {
while ( have_posts() ) : the_post();
the_content();
endwhile;
}

Related

WordPress Custom Theme nth Post

I am just learning custom WordPress theme creation and I can't find answers to some questions.
-- I want to create a blog theme. There will be a carousel. How can I choose (for example) the first 4 posts for this carousel? And after them, the "latest posts" loop will start after 4th post.
I've found only the loop which is shown next;
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile; endif;
?>

WordPress not using category.php file for custom theme

I'm coding a WordPress theme on a staging site and I'm having an issue with having all of my posts display. I've set the static page for the blog to "News" in the setting > Reading option but when I go to the news page, the page is displaying using the index.php file instead of the category.php file that I have created. Template order should have index last if it can't find category or archive is my understanding.
I'm using the same coding on another site and it works correctly. I've put this new theme on that existing site and it works as it should. They are both on the same hosting company. I started a brand new WordPress instance and it's not working on that new instance either.
My template for page.php is working fine instead of defaulting to index.php
Also tried updating permalinks, checked file permissions, checked htaccess file for rewrite conditions.
So what am I missing?
category.php code
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/content-posts', get_post_format() ); ?>
<?php endwhile; else : ?>
<?php get_template_part( 'template-parts/content', 'none' ); ?>
<?php endif; ?>
<?php echo paginate_links(); ?>

How to display the content of a blog post using a custom post template?

I am using my own custom WordPress theme and I am running into trouble displaying the content of blog posts. I can display the title and the date it was published using php but I can't get any of the paragraphs, images, headings, etc. to display on the page. I am using Gutenberg blocks (default) for the content of the blog posts.
I have tried using php functions to grab the content but they don't seem to be working.
<div class="col-md-6 col-md-offset-3">
<p class="date"><span class="glyphicon glyphicon-time">
</span> <?php echo get_the_date();?></p><br />
<p><?php $content = apply_filters('the_content', $post-
>post_content);?></p>
</div>
I am expecting the content of the post to display within the div container but the function is not grabbing the content. Any help would be appreciated!
It sounds like you may be trying to retrieve the post content from outside the loop.
If you look at the post template for a theme e.g. 2017, it is this bit that does the magic. It’s not even necessary to pass a post ID:
<?php
while ( have_posts() ) : the_post();
get_template_part( 'components/page/content', 'page' );
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) :
comments_template();
endif;
endwhile; // End of the loop.
?>
E.g. you should just be able to do:
<?php
while ( have_posts() ) : the_post();
the_content();
endwhile; // End of the loop.
?>
Might be a good idea to start with the code on the link above, or copy the single.php file for the theme you’re using and use that as the basis for your custom post page?

Editable posts WordPress

I'm trying to create my own template in CMS WordPress. I would like to have live editable text on my page http://restaurant.g6.cz/. I want to create something like posts and edit them from the web, because I can't think of anything better. I tried to paste into (page.php) something like
<?php
$my_post = get_post(168);
echo $my_post->post_content;
?>
and tried edit the text with plugin Live Editor but I wasn't successful, so I tried to write
<?php query_posts('cat=5&showposts=1'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; endif; ?>
<?php wp_reset_query(); ?>
this was editable but every post needed it's own category. Does anyone know, how to do this in PHP or better way without plugins? Thank you all very much :)
Example

Adding content via wordpress admin

I'm developing a wordpress theme where I have a set of custom pages and each page has it's own content! For an example, say there is a custom page where the content is already done using HTML but I need to be able to change the content of a single paragraph using the wordpress admin panel.
How do i do this using the wordpress framework? is there any special way of adding custom editable fields to specific content locations in a page?
Just add meta box and get the values from post meta to show in your html
you can generate metabox from this site without any programing skills
https://jeremyhixon.com/tool/wordpress-meta-box-generator/
and get meta field value with the below code
get_post_meta(get_the_ID(), 'meta_key', true);
Use Wordpress loop to do this:
<div class="content">
<p>Your static content goes here</p>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<p>Or here</p>
</div>
This part between if(have_posts()) and endif; will be generated from Wordpress admin panel so you can easily change it with page WYSIWYG editor without any additional plugins.
Don't forget to create your custom pages as page templates and assign to page in admin panel otherwise it won't work as expected.
Use the following code:
<?php
$id=2;
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
Here, $id=2 is your page id which is you get from WordPress Admin panel.

Categories