Last active
October 11, 2015 22:58
-
-
Save ammist/3932906 to your computer and use it in GitHub Desktop.
WordPress: Attach a category of posts to a page or other post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Replace the output HTML with whatever your post formatting should look like (at line 53 or thereabouts)