0

Maybe this has already been asked here, but I can't seem to find a solution, so forgive me if this is a duplicate.

I have coded a custom template to display all posts from a category with the ID 36. I would then like to provide the viewer with the option to sort the displayed posts either alphabetically or chronologically.

Everything is working fine - the posts are displaying correctly and are paginated - but I can't get the sorting links to work.

Some help with this would be GREATLY appreciated!

<div id="content-sidebar-wrap" class="fullwidth">
    <div class="hfeed" id="content">
        <div id="headline">
            <?php $sort= $_GET['sort'];
                if($sort == "title")
                {
                $order= "orderby=title";
                }
                if($sort == "date")
                {
                $order= "orderby=date";
                }
            ?>
            <h1>My Headline</h1>
            <h2>My sub-headline.</h2>
            <a href="?sort=title">Sort by Title</a>
            <a href="?sort=date">Sort by Date</a>
        </div>
        <div id="video_gallery">
            <?php 
                $temp = $wp_query; 
                $wp_query = null; 
                $wp_query = new WP_Query(); 
                $wp_query->query('cat=36&posts_per_page=6'.'&paged='.$paged.$order.'&order=DEC'); 
            ?>
            <?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
            <?php $url = wp_get_attachment_url( get_post_thumbnail_id($post->ID) );?>
                <div class="video_item">
                    <a href="<?php the_permalink(); ?>">
                        <div class="play_button"></div>
                        <img src="<?php echo $url; ?>" class="video_image"/>
                    </a>
                    <p>
                        <a href="<?php the_permalink(); ?>" class="video_title"><?php the_title(); ?></a><br />
                        <span class="video_date"><?php the_date(); ?></span>
                    </p>
                    <div class="videoExcerpt"><?php the_excerpt(); ?></div>
                    <a href="<?php the_permalink(); ?>" class="watch_video">Watch Video ></a>
                </div><!-- .video_item -->
            <?php endwhile; ?>
            <div class="clear"></div>
            <center id="paginate"><?php posts_nav_link( ' ', '<img src="http://mysite.com/wp-content/themes/metric/images/prev.png" />', '<img src="http://mysite.com/wp-content/themes/metric/images/next.png" />' ); ?></center>
            <?php 
                $wp_query = null; 
                $wp_query = $temp;
            ?>
        </div><!-- #video_gallery -->
    </div><!-- #content -->
</div><!-- #content-sidebar-wrap -->

I should add that PHP and Wordpress are not my areas of specialty, so any other input you may have about my above code is also more than welcome. Thank you!

  • showposts is depreciated, you should use posts_per_page – Pieter Goosen Jun 5 '14 at 14:33
  • Thanks, @PieterGoosen! I'll make that change now and edit my question to reflect it. – Manly Jun 5 '14 at 14:38
  • What sorting links are you talking about? – Pieter Goosen Jun 5 '14 at 14:48
  • I would like to add an option at the top of my page, to allow the viewer to sort the displayed posts by date or alphabetically. I have updated my question, to clarify this. – Manly Jun 5 '14 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.