I am doing theme integration in wordpress.I created home.php (template page) and select from backend(for front end page).
Now i want to show posts on front page.I put following code for display post in front page.But code is not working.Where i am wrong ?
Here is my code
<?php
if ( have_posts() ) : while ( have_posts() ) : the_post();
the_content();
endwhile;
endif;
?>
You can use following code -
<?php
$args = array('posts_per_page' => 3, 'post__not_in' => get_option('sticky_posts'));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post(); ?>
<?php esc_html(the_title()); ?>
<?php the_excerpt(); ?>
<?php
endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php endif; ?>
Related
I have one custom loop in a custom homepage in WordPress that is pulling 4 posts with some banners in middle. All is working fine, however pagination always shows same posts. Is there a way for me to add pagination to this custom loop?
My index.php code:
<?php
query_posts(array(
'post_type' => 'post',
'showposts' => 4,
) );
?>
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 3) : ?>
<!-- banners -->
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<?php else : ?>
<h1> <?php the_title(); ?> </h1>
<?php the_content(); ?>
<!-- banners -->
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<p align="center"><?php posts_nav_link(); ?></p>
Inside your query_post array add this line:
'paged' => ( get_query_var('paged') ) ? get_query_var('paged') : 1,
and in settings -> reading set 4 posts per blog page.
How to get all posts from one category. i tried this code , it's not showing any output.Is it correct or any correction is here? Thanks.
include('wp-config.php');
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result = array(
"id"=>$args['ID'],
"type"=>$args['post_type'],
"url"=>$args['guid']);
endforeach;
wp_reset_postdata();
print($result);
Try below :-
global $wp_query;
$args = ('category=news&posts_per_page=-1');
$myposts = get_posts( $args );
foreach ( $myposts as $post ) : setup_postdata( $post );
$result[] = array(
"id"=>$post->ID, // changed $args to $post
"type"=>$post->post_type,
"url"=>$post->guid);
endforeach;
wp_reset_postdata();
print_r($result);
If you want to display posts from a specific category in the category page, you can use the below given code in your theme's category.php file.
<?php
if(have_posts()) :
while (have_posts()) : the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
?>
If you want to display the same in pages other than category page, just add the following code to the corresponding files.
<?php
$posts = new WP_Query();
$posts->query( "category_name='{enter your category slug here}'&posts_per_page=-1" );
if($posts->have_posts()) :
while ($posts->have_posts()) : $posts->the_post();
?>
<?php the_title();?>
<?php
the_post_thumbnail();
the_content();
?>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
are you just trying to get posts from a category?
This is a handy code from the Codex that I keep around. Use this on any custom category page or anywhere on any page, for that matter, to start the loop. Make sure you put your category slug into the right spot in the code.
query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
if ( have_posts() ) : while ( have_posts() ) : the_post();
// YOUR STUFF LIKE the_title(); or the_content();
endwhile; endif;
This is NOT a fix to your code, but it answers the question you asked. I think your problem may be in the use of $args inside the loop (seems odd), but if you want me to make sure I might need more of the code or a working example I can see.
http://codex.wordpress.org/Function_Reference/query_posts#All_Posts_in_a_Category
AHEM...yeah... I'm an idiot... don't go pasting this around. Use WP_Query!! thanks Pieter.
On your theme directory, search category.php. If it doesn't contain, create a new file category.php and paste this code:
<?php if(have_posts()) : while (have_posts()) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt();?>
<?php endwhile; else :?>
<?php endif;?>
I create category.php for display categories archive in a costumed template.
In a category page link like this: http://www.example.com/category/cat1/
By these codes it's OK and shows last items of cat1
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Some template code
<?php endwhile; ?>
<?php endif; ?>
But when I try to customize query by WP_Query or query_posts instead of contents of cat1 it shows contents of all categories of site
<?php query_posts( 'posts_per_page=30' ); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
// Some template code
<?php endwhile; ?>
<?php endif; ?>
What is reason and solution?
You must define cat in your query.
it's your answer:
<?php
$args = array(
'cat' => get_query_var('cat'),
'posts_per_page' => 30
);
$recent = new WP_Query($args); while($recent->have_posts()) : $recent->the_post();?>
//some template code
<?php endwhile; ?>
I am using a wordpress query to display 3 posts at random from a custom post type. I am using the following code which is working fine:
<?php $my_query = new WP_Query('post_type=my_post_type&orderby=rand&showposts=3'); ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php endwhile; ?>
<?wp_reset_query(); ?>
However, I want to mirror the same query below to show the same items once again. So two wordpress queries on one page, the first query picking 3 random posts and the second query showing the exact same results of the first query. Any help would be appreciated. Thanks
try this out :)
<?php $my_query = new WP_Query('post_type=post&orderby=rand&showposts=3'); ?>
<?php $i=0; ?>
<?php while ($my_query->have_posts()) : $my_query->the_post(); ?>
Do Stuff
<?php
$myPostVar[$i] = array (
'title' => get_the_title(),
'content' => get_the_content()
);
$i++;
?>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
<?php foreach ($myPostVar as $Postvar) : ?>
<h2><?php echo $Postvar['title']; ?></h2>
<p><?php echo $Postvar['content']; ?></p>
<?php endforeach; ?>
Can anybody look at the following codes? After I excluded the category 15, the next_posts_link does not work, nor does the page navigation plugin. When I click the page 2, 3,4, it only display the posts in page 1.
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; else: ?>
<?php next_posts_link() ?>
<?php endif; ?>
How about...
<?php query_posts('cat=-15'); ?>
<?php while (have_posts()) : the_post(); ?>
... ....
<?php endwhile; ?>
<?php next_posts_link() ?>
<?php endif; ?>
??
Try it like this:
<?php query_posts(array( 'cat' => -15, 'paged' => get_query_var('paged') ) ); ?>
This should work.