Hi,
I am trying to create a theme for someone so they can easily update their site. They will need to update testimonials & news themselves.
I have created the category 'news' and 'testimonials' but I need these to display on a certain page, not "<strong>wordpress/category/news/"</strong>.
I am expecting to create a new page template to achieve this, but cant find the actual syntax to only get certains posts within a certain category to display.
Quick response would be great!
Arnav Joy answers:
try this
<?php
/*Template Name: News Template*/
get_header();
$news_category_id = 1; // enter category id for news category here
$showposts = 10; // -1 for all the posts
?>
<div id="primary">
<div id="content" role="main">
<h1><?php echo get_cat_name($news_category_id);?></h1>
<?php wp_reset_query();?>
<?php query_posts('cat='.$news_category_id.'&showposts='.$showposts);?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'theme' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'theme' ) ); ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query();?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_footer(); ?>
save the code as any file.php say news.php and then create a page and choose template "News Template" from the page attributes at right side ans see.
let me know if you need any help in this .
Luis Abarca answers:
Use
<?php
$news_catID = 2; // change this with your news category ID
$posts = get_posts( array('category' => $news_catID) );
foreach( $posts as $post ) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
Dbranes answers:
You can also check the examples here
[[LINK href="http://codex.wordpress.org/Template_Tags/get_posts"]]http://codex.wordpress.org/Template_Tags/get_posts[[/LINK]]
to include in your template files
fx:
<ul>
<?php
global $post;
$args = array( 'numberposts' => 5, 'category' => 10 );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>
</ul>
where '10' is your category_id.
Arthur Araújo answers:
Hi Luke, this is a solution:
1. Create a page in admin with the same slug as your cathegory;
2. Create a php template page, exemple: page-news.php
3. Place this code on the page-news.php:
$cat_ID = get_category_by_slug('news')->term_id;
query_posts( 'cat=' . $cat_ID );
include 'category.php';
And repeat the same steps to testimonials.
Sabby Sam answers:
Hi,
Paste this code in one raw file with the name of template_news and upload in your theme
<?php
/*
Template Name: News
*/
?>
<?php get_header(); ?>
<div id="content">
<h1 class="page-title"><?php the_title(); ?></h1>
<div class="entry">
<?php
query_posts(array('category_name' => 'category-slug', 'posts_per_page' => -1 ));
while (have_posts()) : the_post();
if(has_post_thumbnail()) {
the_post_thumbnail( array(180,200) );
}
echo the_title();
the_content('');
endwhile;
// Reset Query
wp_reset_query();
?>
</div>
</div>
</div>
<?php get_footer(); ?>
Once you paste this code go to add new page and in the page setting you will find the page as news. select it and update it will automatically display all news.
Only thing is that you need to copy your div tags whatever you have in a theme or give me your any theme file code.
In the same you can do this for : testimonials.
Luke Zammit comments:
Thanks everybody for your replies..
Arnav, I used yours first and it worked perfectly.
Thanks very much! I will vote later today.