On my homepage, I have 3 columns showing varying amounts of information from posts in specific categories (first 2 of them child categories, the third column displays posts from various child categories, but one parent category).
I want to determine which posts to display (by category as described) in each, by making them sticky.
Help?
On my homepage, I have 3 columns showing varying amounts of information from posts in specific categories (first 2 of them child categories, the third column displays posts from various child categories, but one parent category).
I want to determine which posts to display (by category as described) in each, by making them sticky.
Help?
Share Improve this question asked Apr 24, 2013 at 21:02 JayxJayx 1391 silver badge5 bronze badges 3 |2 Answers
Reset to default 0OK ... code might still be a bit on the flimsy side, but here's what I came up with (still not posting the old, even crappier, PHP; sorry down-voter ) in case anyone else might have use for it or can improve on it.
<section class="column first">
<h2>What's New</h2>
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'category_name' => 'news',
'post__in' => $sticky,
);
query_posts( $args ); while (have_posts()) : the_post();
{ ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php } endwhile; wp_reset_query(); ?>
</section>
<section class="column middle">
<h2>Country Projects</h2>
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'category_name' => 'projects',
'post__in' => $sticky,
);
query_posts( $args ); while (have_posts()) : the_post();
{ ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php } endwhile; wp_reset_query(); ?>
</section>
<section class="column last">
<h2>Featured Publications</h2>
<?php
$sticky = get_option( 'sticky_posts' );
$args = array(
'category_name' => 'publications', //This is the parent category, no need for using the cat ID this way.
'post__in' => $sticky,
);
query_posts( $args ); while (have_posts()) : the_post();
{ ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php } endwhile; wp_reset_query(); ?>
</section>
Feel free to improve this or comment on how it can be optimised and/or pros and cons to my approach.
As requested, this is a rough example:
<?php
$args = array(
'posts_per_page' => -1,
'category__in' => array( 1, 2, 3 ), // IDs of your categories
'post__in' => get_option( 'sticky_posts' ),
);
$query = new WP_Query( $args ); // do one query only
while ( $query->have_posts() ) {
$query->the_post();
$categories = get_the_category();
$categorized_posts[$categories[0]->name][] = array( get_the_title(), get_the_content() ); // build a new array
}
?>
<section class="column first">
<h2>What's New</h2>
<?php
if ( isset( $categorized_posts['news'] ) ) {
foreach ( $categorized_posts['news'] as $post_stuff ) {
echo '<h3>' . $post_stuff[0] . '</h3>';
echo $post_stuff[1];
}
}
?>
</section>
<section class="column middle">
<h2>Country Projects</h2>
<?php
if ( isset( $categorized_posts['projects'] ) ) {
foreach ( $categorized_posts['projects'] as $post_stuff ) {
echo '<h3>' . $post_stuff[0] . '</h3>';
echo $post_stuff[1];
}
}
?>
</section>
<section class="column last">
<h2>Featured Publications</h2>
<?php
if ( isset( $categorized_posts['publications'] ) ) {
foreach ( $categorized_posts['publications'] as $post_stuff ) {
echo '<h3>' . $post_stuff[0] . '</h3>';
echo $post_stuff[1];
}
}
?>
</section>
posts_per_page
pagination parameter. – montrealist Commented Apr 24, 2013 at 21:11