My wordpress custom post displays the content rather than the excerpt - php

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

Related

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 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

How do I display one (featured) post from Wordpress on my static html website?

We have a Wordpress News Blog on our website (http://www.litepanels.com/news/) that is separate from the rest of the static html website. I just want to take the latest story (the featured post) and display it on our home page. The Blog and our website are on the same server. I found I can grab post titles like this:
<?php
require('../news/wp-blog-header.php');
?>
<?php query_posts('showposts=3'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?><br />
<?php endwhile;?>
but I do not know PHP well, how do I grab just the featured post (which has a title, image and text as you can see in the link above)
I am testing it here: http://www.litepanels.com/newwebsite/blog_test2.php
If that works for you, it means you've got all you need except for some way to display your information. For this, Wordpress provides some template tags that can be used within the while loop. the_content is the one you need. Others can be found in the Codex.

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...

Categories