Wordpress post loop in page - php

I have a custom page template where I also want to display a list of posts.
Normally in a page you can just use the following code to display content
<?php while(have_posts()): the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
But what if before I display the page content, I have a custom query to display a list of posts? How can I after displaying those posts, go back to the original query to display page information?

After your custom query, call wp_reset_postdata() function to get back default wordpress loop.
<?php wp_reset_postdata(); ?>
For more info and example check at http://codex.wordpress.org/Function_Reference/wp_reset_postdata

Related

Home.php using for blog content can't retrieve page title - Wordpress

Hi I want to do the blog part in my wordpress theme, I set up using static front page et blog page to display latest post.
So in my home.php I have :
<?php get_header(); ?>
<section class="header--page">
<div class="header--img">
<?php the_post_thumbnail() ?>
</div>
<div class="container">
<h1><?php the_title() ?></h1>
</div>
</section>
<?php get_footer(); ?>
But instead of getting title from page and featured image from page, It display those from the first posts, any idea ?
I try on my page.php and front-page.php, both are returning correct title and image
So things like the Posts page function a little differently than a standard page. Most likely, your the_title() function is returning the title of the first post to be displayed.
What you'll need to do is this instead:
<h1><?php echo get_the_title(get_option('page_for_posts')) ?></h1>
What needs to happen is you need to grab the ID of the Posts page, and use that ID to find its title.
So you'll want to use get_the_title() instead, because the_title() doesn't allow you to pass an ID of a page or post. get_the_title() accepts an ID as a parameter.
The page's ID that you specified as your Posts Page in the WP Settings > Reading can be grabbed by using get_option('page_for_posts').
So putting it all together, the code snippet above will get the title of the Posts Page, and echo it on-screen. (get_the_title() doesn't automatically echo it's returned value like the_title() does, hence the addition of the echo.

How do you include the Post ID within the_content() field in PHP?

Please bear with me on this question. I understand that it may not be as clear but and suggestions are helpful.
So I created a template page and within that template page I have two tags.
One field is the default:
<?php the_content(); ?>
and the other one is
<?php the_field('test_advertisement_two', 25017); ?>
that I created using ACF (Advanced Custom Fields) which includes the Post id. How can I include the post id in the <?php the_content(); ?> field?
I think I maybe confused how <?php the_content(); ?> works. Is there a way to assign a name to it a name to it?
This is what I am doing:
I am calling the template page (test-template.php) that contains:
<?php the_content(); ?>
<?php the_field('test_advertisement_two', 25017); ?>
from a different location by using
The post content in <?php the_field('test_advertisement_two', 25017); ?> displays but the content from <?php the_content(); ?> does not display. That's why I figure that I either need to include a post Id in <?php the_content(); ?>
The post content in displays but the content from does not display. That's why I figure that I either need to include a post Id in
Essentially.
the_content only functions right within The Loop, one of the most important concepts in WordPress. The the_field call is working because it's being explicitly passed a post ID in the optional second parameter.
You'll need to access it like this:
$page = get_post(25017);
echo $page->post_content;
(or get_page if you're on a page instead of a post)
Hi if you need post content using post id then below is the code for that.
echo get_post_field('post_content', $post_id);

Required Wordpress specific category posts display on static page with newer order at the top

I have two different post categories ("Local" & "International") in WordPress and I have displayed the posts in my static page. Right now both categories post are coming on one page. Can I show only local posts in local.php and International posts in International.php page? Also another change please. Can I show newer posts at the top? Right now older post is at the top.
Following is my code.
<?php
require('wp-blog-header.php');
?>
<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : start_wp(); ?>
<?php echo "<h1>";the_date();echo "</h1>"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
Assuming custom page as a page in yours custom plugin. you will need to load wp-load.php to use the WordPress function/loops to fetch the information.
you will need
require_once("../../../wp-load.php"); // ../../../ according to you file location
// now you can loop using get_posts
$posts = get_posts('numberposts=10&category=CATEGORY_ID&order=DESC&');
// loop
If this is not in WordPress, you have to connect to MySQL and the fetch the information from tables with you SELECT query.
Use
$posts = get_posts('numberposts=10&order=DESC&category=[categoryID]&orderby=post_title');
in local.php respectively International.php. Notice that order=DESC instead of order=ASC, this reverses the post order. And be sure to use the appropriate categoryIDs. You find them in your Wordpress administration area.

Wordpress display page contents and a loop?

I have a custom loop in a page template to display posts by category by give category and tag. It works, but above the loop I need to show the content of the page itself, i.e. the content that's been entered into the Wordpress Dashboard normally.
What do I add to my template to display this content?
I have tried:
$id = $post->ID;
get_page($id);
// then my custom loop
Which does get the current page ID, but no content.
In WordPress, calling
<?php the_content(); ?>
will pull in the content from the WYSIWYG editor on the page that is using that template as long as it is inside the loop.
The most basic example of this might look like this:
<?php while (have_posts()) : the_post();/* Start loop */ ?>
<?php the_content(); ?>
<?php endwhile; /* End loop */ ?>
<!-- Enter your custom loop for displaying posts by category down here -->
More info here: http://codex.wordpress.org/Function_Reference/the_content
In my case, with a modern theme and using Gutenberg blocks, I had to apply filters to the content before the posts loop, as mentioned in this thread here: Proper way to get page content
Following one of the examples, a simple working solution would be:
<?php
$id = 669; // page ID that has been assigned for posts
$post = get_post($id);
$content = apply_filters('the_content', $post->post_content);
echo $content;
?>
<!-- Enter your custom loop for displaying posts by category down here -->
To display the content of the page, using the the_content() function.
Added your custom loop after this function call.

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