I usually use this approach:
wrong approach
<?php query_posts( array(
'category_name' => 'news',
'posts_per_page' => 3,
)); ?>
<?php if( have_posts() ): while ( have_posts() ) : the_post(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
With the help of @swissspidy the correct way is this one:
<?php
// the query
$the_query = new WP_Query( array(
'category_name' => 'news',
'posts_per_page' => 3,
));
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
See @codex for more info.
Related Posts:
- add title to widgets of wordpress
- Is there a way to change the default page template selection?
- Can I have two child pages of different parents with the same name?
- Replace Dashes Before Title in Page List
- Proper method to restrict non logged users into certain pages
- WordPress parent select need to be removed
- How to get the specific Page’s current ID and use it in a get_page function?
- Limit number of pages that use a specific template?
- How does wordpress calculate the page depth?
- foreach error on false boolean from get_terms
- Show TwentyEleven header image only on home page
- Show different badge based upon the user role
- Placing google maps into a wordpress page using the wordpress page editor
- Export only pages and posts with mysql dump
- Backend option to include a javascript file on specific pages?
- Putting footer content in a “page” – Doing it wrong?
- How to create wordpress page that shows posts with specific tags?
- Template for child pages / subpages
- Is there a better way to programmatically insert content into a page?
- Custom page template not working
- How to Add a Rewrite Rule for Only One Page?
- Are Pages the right thing to use for making sections of a single-page site?
- Allow contributors to create page (but not publish)
- Visiting Webpage ONLY after another page was visited. Possible?
- add new metabox to page editor with just page parent
- How to open a phpBB forum in the same website as sub-page [closed]
- Display page contain in index.php file
- How to setcookie if is_page(‘page’) ? should I use add_action(‘init’) or there is another action?
- Why do pages in WordPress have a hierarchy but menu’s also have a hierarchy?
- Text Editor Tags
- Column Images Showing Gaps
- I Changed the Menu Order, But the Page Order Didn’t Change on Front Page
- Custom page template with entirely different design. Is it possible in?
- Combining ‘depth’ with ‘include’ in wp_list_pages()
- Page template no longer exists – metaboxes won’t save
- A static page view call every custom page templates
- Wrapping images in tags based on size
- Get the ID of the direct parent page
- External stylesheet per page
- How to add prefix slug in static wordpress page?
- how to avoid the character conversion of WordPress pages?
- How to check if the current page is at a specified path in the URL?
- How to make a page unsearchable in blog search?
- Query specific Pages
- Phantom page can’t get rid of/can’t get at to change
- ‘x’ Converted to html code in example: 5×10
- How can I send an email when someone uses the HTML download Attribute?
- Rewrite to load homepage for a different url
- logout redirect only from admin page
- multiple post into single page
- Later blog post pages give 404 error
- Different css file not working for the page template I created
- How to change the order of the Front Page so it doesn’t appear first
- 404.php only works if I’m logged in. Everybody else redirects to wp-login.php – why?
- redirect index.html inside folder to wordpress page of same name
- Show content if page is a grandchild of top level page
- Does Feedburner send notifications about WordPress pages?
- Using index.php as page template
- Ordering Pages and Increasing Navigation
- Static Website No Titles But Still Nav
- Search results to be only posts & children of page ID
- What’s the section of code (or loop) which retrieves the Page title and description?
- Embedding Password Protected (Private) “Blog Posts” BLOCK that shows Images and Excerpts on Password Protected Page (not the home page)
- How to backup just pages from my site, but not posts with other post_type values
- On the list of Pages, clicking to Edit a particular page redirects to the list of trash
- Template field missing in page attributes
- My pages are using my Index.php and not my page.php
- WordPress custom pages in a folder
- How to delete the scrollbar in my pages?
- How to make a page as default page for a theme without a plugin?
- Return true if parent page id matches
- Not allow add New Page for role ‘editor’
- Pulling images from the media folder category to display inside a page
- How to detect if some page template has been selected
- Why Google map can not show full size in a page?
- What’s the best way to manage sections on a page?
- Display grandchild page content on parent page
- How do you assign custom template for Posts page (under Front page displays setting)
- Category Page Custom Layout [closed]
- How to copy specific page content and create child page?
- How to output different content of page on different places in my template
- Prevent page from displaying
- Adding CSS styles to Admin Area PAGES only (not POSTS or CPT)
- 1st Level Page with No Children
- Previewing Draft shows white page, publishing works fine?
- Changing title bar in browser window of theme “Encounters Lite”
- Loading custom jQuery and HTML in a WordPress page
- Add feed to a custom page
- Directory location of new page
- Grabbing the_content from “about us”
- making a pseudo-static page
- Page specific values in widgets
- Woocommerce – Making product pages child of shop
- Show different number of posts on second page of category
- Use get_posts() with ‘post’ and ‘page’ queries at the same time?
- Website fully loads then immediately crashes in Internet Explorer [closed]
- How to display a page?
- appearance of new page
- How to read a page’s “Shortcodes” from the Template File?
- For some reason my posts are not showing up on my front page [closed]