WordPress query_posts() - wordpress

Hello Im new to Wordpress , But I styled some elements, and had the recent posts in them . My question, can I have them for 5 posts repeated after each other ? thanks :)
<div id="main_content">
<h2>Latest Products</h2>
<div class="latest_products">
<div class="group">
<?php query_posts("post_per_page=1"); the_post(); ?>
<h3 class="stick_note"><?php the_title(); ?></h3>
<div class="pro_content">
<div class="product_thumbnail"> <?php the_post_thumbnail('thumbnail'); ?> </div>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</div> <!-- END LATEST PRODUCTS -->
</div> <!-- END Main Content -->

You forgot to implement The Loop (click for detailed info).
A full Wordpress loop is as follows:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
...render your post here, it will keep repeating...
<?php endwhile; else: ?>
<p>Sorry, no posts matched your criteria.</p>
<?php endif; ?>
Since you don't have an actual loop implemented it can only render 1 post.

You can use:
get_archives('postbypost', 5);
OR
query_posts("showposts=5");
This help you? Be more specific in your question.
You can find the answer in documentation of wordpress:
http://codex.wordpress.org/Template_Tags/get_posts
http://codex.wordpress.org/Template_Tags/query_posts
http://codex.wordpress.org/Template_Tags/wp_get_archives
EDIT:
$args = array('numberposts' => 5, 'order'=> 'ASC');
$postslist = get_posts( $args );
EDIT2:
You can do:
$args = array('numberposts' => 5, 'order'=> 'ASC');
$postslist = get_posts( $args );
foreach( $postslist as $post ) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>

Related

How loop through Custom Post Types and get their content in one page

I have some task which try to solve may be not in smartest way, but so far I got only this idea. I have 5 custom post types. In each of this - one post. Want these posts loop and appear on my index.php .Also these posts has the_field() functions from ACF plugin.
The result not what I expected. This is the code in index.php
<?php
$posttypes = array( 'plays','watch','news','shop','contact' );
$args = array(
'post_type' => $posttypes
, 'posts_per_page' => 5 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<div id="owl-demo" class="owl-carousel owl-theme">
<?php foreach($posttypes as $posttype){?>
<?php $the_query->the_post(); ?>
<div class="item">
<?php get_template_part( 'single', $posttype ); ?>
</div>
<?php };?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
</div>
Also I will post one of the post's code, like single_news.php
<?php get_header();
while ( have_posts() ) : the_post() ;?>
<div class="container-fluid "> <!-- play section -->
<div class="row">
<div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
<h2 class="" style="text-transform:uppercase"><?php the_field('name');?></h2>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-2 col-md-2 col-lg-2">
<h2 class="" style="text-transform:uppercase"><?php the_field('news_name');?></h2>
</div>
</div>
</div> <!-- row -->
</div>
Actually the end of this file I couldnt post but it itself correct with closing dives and endwhile.
In args you have post_per_page set to 5 – this means it will only return the 5 latest posts, regardless of Custom Post Type (CPT).
Try '-1' (return everything) for debugging.
`$args = array(
'post_type' => $posttypes,
'posts_per_page' => -1
);`
Do you want it to return 1 latest post from each CPT?
Maybe try this...
<?php while( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<div class="item">
<?php get_template_part( 'single', get_post_type() ); ?>
</div>
<?php endwhile; ?>
Or run query for each posttype...
<?php foreach( $posttypes as $posttype ) : ?>
<?php
$args = array(
'post_type' => $posttype,
'posts_per_page' => 5 );
$the_query = new WP_Query( $args );
?>
<?php while( $the_query->have_posts() ) : ?>
<?php $the_query->the_post(); ?>
<div class="item">
<?php get_template_part( 'single', $posttype ); ?>
</div>
<?php endwhile; ?>
<?php endforeach; ?>
//I think endforeach exists, otherwise use curly braces
Not sure exactly what you want without seeing it visually.

How to fix pagination link to page 2 wordpress?

Hi all I am a new of wordpress I have problame with pagination, I used plugin Wp-pagnavi ,When I click link to page 2 it same as page 1,How to to fix it.
You can see it at
http://westecmedia.com/?page_id=758
And this my code in page-event.php
<?php
/*
* Template Name: Page - Events Page
*/
?>
<?php get_header(); ?>
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; endif; ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please :(
Include paged, documentation: https://codex.wordpress.org/Pagination
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post();
/* Do whatever you want to do for every page... */
endwhile;
wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
Also don't use query_posts to fetch data in WordPress, consider using https://codex.wordpress.org/Class_Reference/WP_Query.
Please ask WordPress related question here: http://wordpress.stackexchange.com
I hope this helps.

How can I get post into my homepage?

I want to get post into my wordpress homepage, I tried to search lot from google but I am not geting any proper solutions, So if any one have idea please help me.
Thanks in advance.
Here is my tried code so far :
<?php
$args = array(
'cat' => '5',
'post_type' => 'post',
'posts_per_page' => 8,
'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
);
query_posts($args);
while (have_posts()) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content,0,300)." . . . ";
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php
endwhile;
?>
</div>
<div id="page-gina">
<?php
//wp_pagenavi();
wp_reset_query(); // Restore global post data
?>
</div>
Two Ways is possible,
i)Create a custom template.
ii)In your page.php check condition.
Ex:
<?php if(is_front_page()){
echo "This Home";//Here Your code
}
?>

How to fix pagination not found in wordpress?

I am a new of wordpress. I have a problem with pagination when I click to the next post it shows "not found". I installed the plugin wp pagenavi, and I put code in my blog post . It shows the pagination, but I have a problem with the link to the next post. Example when I click to the next post it is show
Something went Wrong!
404
-->
You can see at: http://westecmedia.com/events/
And this is my code in event-page.php:
<div id="content-events">
<div id="head-event"><h3>EVENTS</h3></div>
<div id="main-event">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="part-event">
<div id="entry-thumbnail">
<?php the_post_thumbnail(); ?>
</div>
<div id="event-dess">
<h2><?php the_title(); ?></h2>
<p>
<?php
$content = get_the_content();
$content = strip_tags($content);
echo substr($content, 0, 300);
?>
</p>
<div id="read-more">Read More</div>
</div>
</div>
<div id="line-bottom"></div>
<?php endwhile; else: endif; ?>
<?php wp_pagenavi(); ?>
</div>
</div>
<?php get_footer(); ?>
Help me please ?
Maybe you missed the object here. You can try the code below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'category_name'=>get_the_title(),
'post_status'=> array('publish', 'future')
);
query_posts($args);
instead:
<?php query_posts('category_name='.get_the_title().'&post_status=publish,future');?>

Thesis Framework and Custom Post Types

First time creating/implementing custom post types in Thesis, not first time using custom post types.
I used the Reed Write plugin to create the custom post types. The site is using Thesis 1.8.5.
On the following page (http://www.snyderleadership.com/press-releases/) I have the main content getting dropped in with the contents of the custom post type after it.
I used the custom_functions.php file to create a custom page template and call the db for the contents of the custom post type. Here is my code:
/* CUSTOM PRESS RELEASE TEMPLATE - ADDED by BRETT ATKIN */
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
<div id="press-image">
<?php echo wp_get_attachment_image(get_post_meta($post->ID, 'release_image', true)); ?>
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo get_post_meta($post->ID, 'release_date', true); ?></p>
<p class="press-link"><?php echo get_post_meta($post->ID, 'release_title', true); ?></p>
<p class="press-author"><?php echo get_post_meta($post->ID, 'release_author', true); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
add_action('thesis_hook_custom_template', 'press_releases_page');
It seems like everything is working correctly, just not pulling in the data for the custom post type.
Having done this on other sites (using custom themes), I'm not sure if I did something wrong here or if it is a Thesis issue.
Any help would be great.
Thanks
Brett
Here is the final working code. Thanks to the help from of a local WP Guru friend and maiorano84. We didn't figure out the cause, but we did fine a solution.
function press_releases_page() {
if (is_page('press-releases') || is_page('583')) { ?>
<div id="content">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div class="post_box">
<div class="headline_area"><h1><?php the_title(); ?></h1></div>
<div class="format_text">
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif ?>
<?php
$original_query = $wp_query;
$wp_query = null;
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$wp_query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php $results = get_post_custom(); ?>
<div id="press-wrapper">
<div id="press-image"><?php echo wp_get_attachment_image($results['release_image'][0] ); ?></div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo $results['release_date'][0] ?></p>
<p class="press-link"><?php echo $results['release_title'][0] ?></p>
<p class="press-author"><?php echo $results['release_author'][0] ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_query(); wp_reset_postdata(); ?>
</div><!-- end press-releases div -->
</div>
</div>
</div><!-- end content -->
<?php echo thesis_sidebars(); ?>
<?php } }
remove_action('thesis_hook_custom_template', 'thesis_custom_template_sample');
add_action('thesis_hook_custom_template', 'press_releases_page');
Try changing this:
<div id="press-releases">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="press-wrapper">
To this:
<div id="press-releases">
<?php if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<div id="press-wrapper">
UPDATE:
The only other thing I can see is that you're not resetting your post data, and that the $wp_query variable is actually a Wordpress Global. Try using the reset functions and changing your WP_Query instance name like this:
<?php
wp_reset_query();
wp_reset_postdata();
$args = array (
'post_type' => 'press-release',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query($args);
?>
<div id="press-releases">
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>
UPDATE 2:
Now that you have your wrappers being written to your document, you need to normalize your information. Try to avoid using Metadata instead of normal post attributes. I imagine your metadata 'release_image' is a link to an image somewhere on your server:
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
?>
<div id="press-wrapper">
<div id="press-image">
<img src="<?php echo get_post_meta(get_the_ID(), 'release_image', true); ?>" />
</div><!-- end press-image div -->
<div id="press-information">
<p class="press-date"><?php echo the_date(); ?></p>
<p class="press-link"><?php the_title(); ?></p>
<p class="press-author"><?php the_author(); ?></p>
</div><!-- end press-information div -->
<div style="clear:both;"></div>
</div><!-- end press-wrapper div -->
<?php endwhile; endif; wp_reset_postdata(); ?>

Resources