Skip to content

Instantly share code, notes, and snippets.

@ammist
Last active October 11, 2015 22:58
Show Gist options
  • Save ammist/3932906 to your computer and use it in GitHub Desktop.
Save ammist/3932906 to your computer and use it in GitHub Desktop.
WordPress: Attach a category of posts to a page or other post
/* if using this with Genesis, hook this after the loop.
The name of the custom field can be hidden, if you provide a metabox for choosing it.
The simplest thing is to leave it visible and just use the custom field selector.
*/
function tl_attach_category(){
global $post;
$attach_option = get_post_meta($post->ID, 'attach_category', true);
// You might want to do different things for different categories
if ($attach_option){
if ($attach_option == 'events'){
$args=array(
'category_name' => $attach_option,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'caller_get_posts'=> 1
);
} else if ($attach_option == 'past-events'){
$args=array(
'category_name' => $attach_option,
'meta_key' => 'event_start_date',
'orderby' => 'meta_value',
'order' => 'DSC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'caller_get_posts'=> 1
);
} else {
$args=array(
'category_name' => $attach_option,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => 10,
'caller_get_posts'=> 1
);
}
$my_query = null;
global $more; $more = 0;
$my_query = new WP_Query($args);
if( $my_query->have_posts() ) {
while($my_query->have_posts()): $my_query->the_post();
?>
<div class="headline_area">
<h2 class="entry_title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a></h2>
</div>
<div class="entry-content">
<?php
//the_excerpt();
$more = 0;
the_content("Read More");
echo "</div>";
endwhile;
}
wp_reset_query();
}
}
function tl_featured_image(){
if (!is_single() && !is_page()){
return;
}
$post = get_queried_object();
echo '<div id="featured-image-widget" class="widget sidefeature">';
echo get_the_post_thumbnail($post->ID, 'Right Size' );
echo '</div>';
}
add_action('genesis_before_sidebar_widget_area', 'tl_featured_image');
@ammist
Copy link
Author

ammist commented Oct 22, 2012

Replace the output HTML with whatever your post formatting should look like (at line 53 or thereabouts)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment