Skip to content

Instantly share code, notes, and snippets.

@billerickson
Created January 19, 2013 14:06
Show Gist options
  • Select an option

  • Save billerickson/4572826 to your computer and use it in GitHub Desktop.

Select an option

Save billerickson/4572826 to your computer and use it in GitHub Desktop.
<?php
/**
* Template Name: Category Listing
*
* @package BE_Genesis_Child
* @since 1.0.0
* @link https://github.com/billerickson/BE-Genesis-Child
* @author Bill Erickson <[email protected]>
* @copyright Copyright (c) 2011, Bill Erickson
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
*
*/
/**
* Category Listing Loop
*
*/
function be_category_listing_loop() {
global $post;
$args = array(
'post_type' => 'my_cpt',
'tax_query' => array(
array(
'taxonomy' => 'cpt_category',
'fields' => 'slug',
'terms' => esc_attr( get_post_meta( $post->ID, 'be_category_listing', true ) ),
)
)
);
genesis_custom_loop( $args );
}
add_action( 'genesis_loop', 'be_category_listing_loop' );
remove_action( 'genesis_loop', 'genesis_do_loop' );
genesis();
@billerickson
Copy link
Copy Markdown
Author

Assumptions:

  • post type is 'my_cpt'
  • taxonomy is 'cpt_category'
  • taxonomy term is stored in 'be_category_listing' post meta
  • you want to use the standard genesis loop for it

Possible Changes:

  • You might want to do some validation of the post meta field to ensure the term exists before querying for it
  • You might want to build your own query instead of letting Genesis do it. In that case, replace genesis_custom_loop() with $loop = new WP_Query( $args ) and do the loop yourself. More info: http://www.billerickson.net/custom-wordpress-queries/

@peripatew
Copy link
Copy Markdown

Wow, thanks for the great response Bill! Below is the template I'm working with. I added the 'meta_value' line in the beginning to "hard code" the value into copies of the original portfolio-template.php, but this isn't very flexible.

It appears that lines 25-26 are most relavent. I'll try adding that in, but thought seeing the actual template I'm working on might be helpful

Question, outside of good coding practice, what would be the benefit of the post meta field validation?

Sorry if I didn't mention this isn't a Genesis flavored theme, it's my first time venturing out of the Genesis community! And I don't know if I'll do that again.

    <?php
    $args = array(
        'post_type' => 'portfolio',
        'orderby' => 'menu_order',
        'order' => 'ASC',
                    'meta_value' => 'ao',
        'posts_per_page' => -1
    );
    $portfolio_query = new WP_Query($args);

    if( $portfolio_query->have_posts() ) : 

        echo '<div id="primary" class="hfeed">';

        while( $portfolio_query->have_posts() ) : $portfolio_query->the_post();

        // styles
        $style = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_style', true);
        $media_pos = 'zilla-' . get_post_meta($post->ID, '_zilla_portfolio_media_position', true);
        $width = ( $media_pos == 'zilla-media-center' ) ? 940 : 600;

        // project url
        $portfolio_url = get_post_meta($post->ID, '_zilla_portfolio_project_url', true);
        if( !empty($portfolio_url) ) 
            $portfolio_button_copy = get_post_meta($post->ID, '_zilla_portfolio_project_url_copy', true);

        // determine which media to display
        $portfolio_display_gallery = get_post_meta($post->ID, '_zilla_portfolio_display_gallery', true);
        $portfolio_display_video = get_post_meta($post->ID, '_zilla_portfolio_display_video', true);
        $portfolio_display_audio = get_post_meta($post->ID, '_zilla_portfolio_display_audio', true);

        // grab everything else
        $custom_bg = get_post_meta($post->ID, '_zilla_portfolio_display_background', true);
        $portfolio_caption = get_post_meta($post->ID, '_zilla_portfolio_caption', true);

    ?>

            <?php zilla_page_before(); ?>
            <!--BEGIN .hentry-->
            <div <?php post_class( $style . ' ' . $media_pos ) ?> id="post-<?php the_ID(); ?>">
            <?php zilla_page_start(); ?>

                <div class="hentry-inner">

                    <!--BEGIN .entry-media -->
                    <div class="entry-media">

                        <?php 
                        if( $portfolio_display_gallery == 'on' ) {
                            $gallery_layout = get_post_meta( $post->ID, '_zilla_gallery_layout', true);
                            $slideshow = ( $gallery_layout == 'slideshow' ) ? true : false;
                            $size = ( $media_pos == 'zilla-media-center' ) ? 'portfolio-full' : 'portfolio-index';
                            zilla_gallery( $post->ID, $size, $slideshow, $slideshow );
                        }

                        if( $portfolio_display_video == 'on' ) {
                            $embed = get_post_meta($post->ID, '_zilla_video_embed_code', true);
                            if( !empty( $embed ) ) {
                                echo stripslashes(htmlspecialchars_decode($embed));
                            } else {
                                zilla_video( $post->ID, $width );
                            }
                        }

                        if( $portfolio_display_audio == 'on' ) {
                            zilla_audio( $post->ID, $width );
                        }

                        ?>
                    <!--END .entry-media -->
                    </div>

                    <!--BEGIN .entry-content -->
                    <div class="entry-content">

                        <h2 class="entry-title"><?php the_title(); ?></h2>

                        <?php if( !empty($portfolio_caption) ) 
                            echo "<p class='zilla-caption'>" . stripslashes(htmlspecialchars_decode($portfolio_caption)) . "</p>"; ?>

                        <?php if( !empty($portfolio_url) && $media_pos == 'zilla-media-center' ) { ?>
                            <a href="<?php echo esc_url($portfolio_url); ?>" class="more-link"><?php echo $portfolio_button_copy; ?></a>
                        <?php } ?>

                        <?php the_content(); ?>

                        <?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>

                        <?php if( !empty($portfolio_url) && ( $media_pos == 'zilla-media-left' || $media_pos == 'zilla-media-right' ) ) { ?>
                            <a href="<?php echo esc_url($portfolio_url); ?>" class="more-link"><?php echo $portfolio_button_copy; ?></a>
                        <?php } ?>

                    <!--END .entry-content -->
                    </div>

                </div>

            <?php zilla_page_end(); ?>
            <!--END .hentry-->
            </div>
            <?php zilla_page_after(); ?>

        <?php endwhile; ?>

        <!--END #primary .hfeed-->
        </div>

    <?php else: ?>

    <div id="content">
        <!--BEGIN #post-0-->
        <div id="post-0" <?php post_class(); ?>>

            <h2 class="entry-title"><?php _e('Error: No Portfolios Found', 'zilla') ?></h2>

            <!--BEGIN .entry-content-->
            <div class="entry-content">
                <p><?php _e("Sorry, but no portfolios have been created.", "zilla") ?></p>
            <!--END .entry-content-->
            </div>

        <!--END #post-0-->
        </div>
    </div>

    <?php endif; ?>

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