How to make custom WordPress theme show pages, NOT posts - php

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

Related

My wordpress custom post displays the content rather than the excerpt

I've made a custom post type called 'news', plus an index page called 'archive-news'. In the archive page the excerpt is replaced with the content of the post. Can't work out how to change it.
I've been using HTML for over a year, familiar with the idea of Wordpress PHP but probably not deep enough yet. Had a look for similar problems. The input area for the excerpt has not been left blank (when you edit a custom post). I've heard that if it doesn't then it defaults to use content instead of excerpt. Cleared the cache many times and given it time to refresh.
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<div class="postExcerpt">
<p><?php the_excerpt(); ?></p>
</div>
<?php endwhile; wp_reset_query(); ?>
should show the excerpt and not the content in the archive-news

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?

Wordpress single.php another get_post function destory the first function

I am trying to deal with two get_post functions, in single.php the first get_post function is the post from the wordpress, But after that I called get_post function to other post also to use both of them in the same page but after I call the first get_post ( the main post ) I get the only second data and cant reach the first data.
My code called to the second function ( The first is from wordpress post):
$main_post = get_field('main_post');
$main_p = get_post($main_post->ID);
Then I am trying to use the variable $post OR the_title() OR any other functions to get the first post and it always returning the info of the $main_p post
for example
get_the_title( get_post()->ID )
returns the $main_p post id and not the main post of the single.php
any soulutions ?
I may be wrong, but it seems to me that you are trying to post a different post format with normal post format?
I, myself use get_post_format() so it can be styled differently or have different options.
in single.php I use
<!-- checking if there are any blogposts to be shown using have_posts check which is a wordpress function-->
<?php if(have_posts()) : ?>
<?php while(have_posts()) : the_post(); ?> <!-- the correct syntax for while loop in wordpress to show all the blogposts -->
<?php get_template_part('content', get_post_format()); ?>
<?php endwhile; ?>
<?php else : //else stament if there aren't any posts (put inside if before endif)?>
<p><?php __('No Posts Found'); ?></p>
<?php endif; ?> <!-- stop checking for blog posts-->
</div><!-- /.blog-main -->
Inside functions.php I activated the post-formats inside wp_theme_setup() function
add_theme_support('post-formats', array('aside', 'gallery'));
In this case i activated the gallery and aside posts
That way I have 2 different post types on one page
like this
image from my theme blog page
Here is also a video tutorial on post formats from Traversy media
https://www.youtube.com/watch?v=CRa7eiqyiCM&list=PLc5p9nvpdwBlrNU0hr1f0kXPRkh0aGo1Q&index=7
The key reason why your post values are being overwritten, the additional get_post() declarations are overriding the default query. Now, the code in your pastebin is a pretty massive dog's breakfast, so a direct solution is a rather large undertaking (e.g. the indentation is all over the place, the code snippets are less than ideal regarding their readability, etc...). However, I can point you in the right direction for the solution.
When I pull content from another page on my WordPress sites, I avoid using get_post() in favour of declaring a fresh new WP_Query() (that's just my preference), following it up with a wp_reset_postdata() declaration.
Here's an example of multiple queries on a single template in the WordPress codex:
https://codex.wordpress.org/Class_Reference/WP_Query#Multiple_Loops
The key here is the wp_reset_postdata(). I'd recommend looking into it's purpose. It'll save you a lot of grief:
https://codex.wordpress.org/Function_Reference/wp_reset_postdata

How to make a page for Custom Post Types in Wordpress 3.0?

I have some custom post types, such as 'review'. I can't seem to find out how to make a section (e.g., www.mysite.com/reviews/) which works like the blog home page, but lists reviews instead of posts (with pagination and everything). I'd like to use a separate template for it too.
Create a page called reviews and then create a new template in your theme folder called page-reviews.php. Add all the necessary elements to the template and include a query_posts in front of your post loop. It should look like this:
<?php query_posts("post_type=reviews"); ?>
<?php if (have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<div class="post" >
<h2><a href="<?php the_permalink() ?>" ><?php the_title(); ?></a></h2>
<?php the_content(); ?>
</div><!-- Post ends -->
<?php endwhile; ?>
<?php else: ?>
<p>Sorry, we could not find what you were looking for</p>
<?php endif; wp_reset_query(); ?>
You need help getting .htaccess to convert your categories into hard URLs. I thought WP did this automatically so you'll want to look and make sure you have set up the directory permissions on WP so it can write to your .htaccess file.
Please read this guide and it will be cleared up.
Duplicate the file in your theme called "single.php" and rename it to "single-reviews.php"
Since "single.php" is used for your regular posts, you can append the name of the custom post type to the end of "single-" to automatically use that.
Now once inside of the file "single-reviews.php" you can adjust the layout to be how ever you want.
If you get a 404 error, or it is not showing you the correct layout, you may need to flush the rewrite rules. You can do this two ways.
Go to the permalinks page in your backend and it will sometimes auto flush them.
The best way to do it is in your "functions.php" file in your theme directory add the following code:
add_action ( 'init', 'flush_rewrite_rules' );
function flush_rewrite_rules()
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
Create a new page called reviews. Create a new page template that calls the custom post type. Assign the page template to the page...

WordPress: How to display only posts that are in a certain category?

I'm pretty new to WordPress but have spent some 50 odd hours studying up on it, trying things out and such and have the feeling I got a pretty good handle on it now..
However the one thing I simply cannot get working is to have a page spit out a list of posts of a certain category.
Here is my example: http://dev.jannisgundermann.com/zoeikin/graphic-design/typographic-posters
I have a post that if I go to it directly works correctly, but does not show up on this page.
The post direct link.
The category id is '3' while the category name is 'typographic-posters'.
I have a custom page template for the typographic-posters page that looks like this:
<?php
/*
Template Name: Typographic Posters
*/
?>
<?php get_header(); ?>
<?php get_sidebar(); ?>
<?php if (in_category('3')): ?>
<div class="post">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<div class="post-description">
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
</div>
<?=get_image('flutter-image');?>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
<?php get_footer(); ?>
Using this code however the page only shows gets the header, sidebar and nothing else..
If someone could help me out that would really help me get a handle on this filtering of wordpress categories.
Thanks for reading,
Jannis
in_category will only work outside of the loop on a single page. I suggest using the query_posts function to solve this problem. You may use query_posts('cat=3') or query_posts('category_name=typographic-posters') to get the posts you are looking for.
Once obtained, just use the normal WordPress loop to access these posts.
The easiest way is to create a file called category-3.php and use the standard code from normal index.php or category.php file. Wordpress will take care of fetching posts only from category with id=3 and it's child categories.
in_category will only work outside of the loop on a single page. I
suggest using the query_posts function to solve this problem. You may
use query_posts('cat=3') or
query_posts('category_name=typographic-posters') to get the posts you
are looking for.
Once obtained, just use the normal WordPress loop to access these
posts.
This worked excellent, but make sure that you go into Settings > Reading and set the posts page to the -- Select -- option or it will override this query and dump all recent posts there regardless of category.
Simply add before the loop:
<?php query_posts="cat=3&showposts=5">
This will force the loop to display 5 posts (showposts=5) from category 3 (cat=3).
I would 2nd Eimantas' suggestion. The Template Hierarchy will use the category-3.php to display posts in that category. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need. Plus the category template will better support pagination of posts.
But if you need to stick with a Page to display those posts, also see the Page of Posts example.
http://codex.wordpress.org/Template_Tags/query_posts
Just so you know where these answers are coming from...there are a lot more interesting functions you can do with query_posts as well.
This plugin could also help you if you want to be able to change the displayed categories without going through the code :
http://wordpress.org/extend/plugins/advanced-category-excluder/
I have filtered post by category Id using the method below:
query_posts('cat=1&showposts=3');
if (have_posts()) : while (have_posts()) :
// if(1) {
//echo the_category_ID();
the_post();
/**
* The default post formatting from the post.php template file will be used.
* If you want to customize the post formatting for your homepage:
*
* - Create a new file: post-homepage.php
* - Copy/Paste the content of post.php to post-homepage.php
* - Edit and customize the post-homepage.php file for your needs.
*
* Learn more about the get_template_part() function: http://codex.wordpress.org/Function_Reference/get_template_part
*/
$is_post_wrap++;
if($is_post_wrap == '1') {
?><div class="post-wrap clearfix"><?php
}
get_template_part('post', 'homepage');
if($is_post_wrap == '3') {
$is_post_wrap = 0;
?></div><?php
}
endwhile;
else :
get_template_part('post', 'noresults');
endif;
thank you for sharing on your thought its a great thought. Usually you can just copy a theme's index.php or category.php to category-3.php and adjust that template for any customization you need

Categories