how to loop through posts in both front page and blog page? - php

i want to display some posts in home page according to post type but i can't access posts and post loop returns " Home " post only ,so what is the proper way to make this happen ?
my reading settings
Front page displays : A static page
Front page :home
Posts page :blogs

Keep your reading settings the way they are.
You can create a theme file called front-page.php and use this to control your home page.
Within your front-page.php you can use get_posts to retrieve posts and then loop through them
Example:
<?php get_header(); ?>
<?php
$args = array(
'numberposts' => 10, // number of posts to return
'post_type' => 'your-post-type' // change this to the post type you want to retrieve
);
$posts = get_posts( $args );
if ( $posts ) :
foreach ( $posts as $post ) : setup_postdata( $post ); ?>
<article <?php post_class(); ?>>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
<?php get_footer(); ?>

Related

WordPress loop breaking page, loading blank page

I'm an apprentice WP developer and am trying to wrap up a project for work. I'm trying to loop in the first 6 or so posts on the homepage, but every time I add the loop the page breaks and loads empty (except for the header).
I'm using the HTML5 blank WordPress theme as the basis for the custom theme I'm developing and using ACF to create a custom page builder. The homepage is loading from page.php. Running WP 4.9.5.
<?php $args = array ( 'post_type' => 'post' );
$post_query = new WP_Query($args); ?>
<?php if($post_query->have_posts()): ?>
<?php while($post_query->have_posts() : $post_query->thepost(); ?>
<h2>THIS IS A TEST</h2>
<?php wp_reset_postdata(); endwhile; ?>
<?php else :?>
<p>Whoops. No posts.</p>
<?php endif; ?>
This is your problem: $post_query->thepost();. It should be: $post_query->the_post();. And you're missing a parenthesis after while($post_query->have_posts().
Also, wp_reset_postdata() should be called after the while loop.
Here's the updated code (with a couple of tweaks):
<?php
$args = array (
'post_type' => 'post'
);
$post_query = new WP_Query($args);
if ( $post_query->have_posts() ):
while( $post_query->have_posts() ) : $post_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php
endwhile;
wp_reset_postdata();
?>
<?php
else : // No posts found.
?>
<p>Whoops. No posts.</p>
<?php
endif;

how to do this with wordpress API and php

I am really a newbie in Wordpress and php. I created a theme for my website as follows:
I think my question is clear: in wordpress admin page I have four categories: 1,2,3,4. I want last 2 posts in category 1 to be displayed in section (div) 1 (shown above), last 2 posts in category 2 to be displayed in section (div) 2, and so on.
As I said, I am novice and I faced a tons of functions in Wordpress documentation.
for example I used the code below inside index.php (of my custom theme) in section 2 which outputs: "Sorry, no posts matched your criteria."
<?php
$args = array( 'post' => 'post', 'posts_per_page' => 1,'category_name'=>'تازه ها');
$the_query = new WP_Query( $args );
?>
<?php
if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif;
?>
Please give a piece of code to do this.
This should work:
$posts = get_posts ("cat=1&showposts=2");
if ($posts)
{
foreach ($posts as $post):
setup_postdata($post); ?>
<h2><?php the_title(); ?></h2>
<?php endforeach;
}
Change cat=1 to the id of each of the four categories.

How to add category(posts) in wordpress single post

Hey any one know how to add a specific category posts in wordpress single post.m giving a link of website that is using it http://www.animetv.org/anime/one-piece-dub/ it is a post that contains posts of a particular category.It is demanded as same i.e. with post thumbnails.Thanks.
Just put this code at your single.php file you will get your desired result:
<?php $category = get_the_category(); ?>
<ul>
<?php
$args = array('category' => $category[0]->term_id );
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post ); ?>
<li>
<?php the_post_thumbnail();?><br/>
<?php the_title(); ?>
</li>
<?php endforeach;
wp_reset_postdata();?>
</ul>
And put css for ul li as per your choice like li{float : left;}
If you want to display the posts from a specific category in single.php page, please add the following code to the the file.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page={enter the number of posts to display here}" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<div>
<?php the_post_thumbnail(); ?>
</div>
<div>
<?php the_title(); ?>
</div>
<?php
endwhile;
endif;
wp_reset_postdata();
?>

How to add posts in wordpress pages?

I want to show some posts in the homepage of my wordpress site..How will I do it??or is there any plugin that can help me do it?or is there shortcodes that could pull out and display those post in my homepage?
If you want to do it the easy way, you can use a plugin like Display Posts Shortcode.
Or, if you want to do it manually, you can use get_posts().
Here's an example you could use:
<?php
if (is_page()) {
$cat=get_cat_ID($post->post_title); //use page title to get a category ID
$posts = get_posts ("cat=$cat&showposts=5");
if ($posts) {
foreach ($posts as $post):
setup_postdata($post); ?>
<?php the_title(); ?></h2>
<?php endforeach;
}
}
?>
I hope this helps!
The implemetation varies greatly by theme.
Check if your wordpress theme has a file called index.php.
If you have this file for your current theme, this is the file responsible for displaying your home page. And this is where you will have to put the code snippets to display posts.
Presuming that you know a bit of html and PHP you will have to decide the suitable place within index.php to add the code suggested above by Amal Murali.
If you want to show a specific category post on Homepage you can use category slug or category name. Like the below to show and use the wp_pagenavi() plugin to show pagination and present it.
<?php
$paged = (get_query_var( 'paged' )) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => 'CATEGORY NAME ',
'posts_per_page' => 5,
'paged' => $paged,
);
$arr_posts = new WP_Query( $args );
if ( $arr_posts->have_posts() ) :
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
if ( has_post_thumbnail() ) :
the_post_thumbnail();
endif;
?>
<header class="entry-header">
<h1 class="entry-title"><?php the_title(); ?></h1>
</header>
<div class="entry-content">
<?php the_excerpt(); ?>
Read More
</div>
</article>
<?php
endwhile;
wp_pagenavi(
array(
'query' => $arr_posts,
)
);
endif;
?>

Wordpress the_content not working with custom post type

Working to create a custom wordpress theme with custom post types:
I have added custom post types (via the plugin custom post UI) and custom fields (via advanced custom fields). The custom post is correctly displaying in the new template page created (single-custom.php). So the individual custom posts are displaying exactly as I would like.
The problem however is that on the homepage only the title is displaying.
What I've done:
1) I've added the following code to allow new post types to be pulled into the feed on the homepage/blog feed via the functions.php:
add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ( ( is_home() && $query->is_main_query() ) || is_feed() )
$query->set( 'post_type', array( 'post', 'custom' ) );
return $query;
Thanks to Justin Tadlock for this
2) I've created a custom content page content-custom.php and amended the index loop to call that template:
<?php if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php
get_template_part( 'content', 'custom', get_post_format() );
?>
<?php endwhile; ?>
My homepage is still only displaying the titles of my custom posts and not the full content as I hope. The problem has something to do with the call of the_content in the content-custom.php but I can't find the issue as I am still familiarizing myself with wordpress. The relevant code for the content-custom.php is as follows:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
//The title below is displayed correctly
<h1 class="entry-title"><?php the_title(); ?></h1>
</header><!-- .entry-header -->
// The problem begins below. The entry-content div is created but is empty and is not pulling any content from the custom post through the_content call.
<div class="entry-content">
<?php the_content(); ?>
</div><!-- .entry-content -->
I am not familiar with the plugins you mentioned, but check if the custom post type really has a "content" field.
If the "content" is a custom field the you have to get it with
get_post_meta($post->ID, 'field name', true);
I had a similiar problem and it was fixed by adding the following to the template. You can also try adding to your content-custom.php:
<?php wp_reset_postdata(); ?>
Some post-related data is not available to get_posts by default, such as post content through the_content(), or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument:
<?php
$args = array( 'posts_per_page' => 3 );
$lastposts = get_posts( $args );
foreach ( $lastposts as $post ) :
setup_postdata( $post ); ?>
<h2><?php the_title(); ?></h2>
<?php the_content(); ?>
<?php endforeach;
wp_reset_postdata();
?>
See Access all post data

Categories