Forums

[resolved] Custom Post Types, taxonomies and terms: how do I list entries/post randomly? (5 posts)

  1. gavinwebb
    Member
    Posted 1 year ago #

    Hello everyone.

    Before I start, I'm still learning WordPress and PHP so please be gentle :)

    I thought I'd try to get my head around custom taxonomies. I'm using the plugin Custom Post Types UI to create a post type of 'Directory Listing', a custom taxonomy of Directory Category, some terms inside this category, therefore looking like this:

    Directory Listing (custom post type)
    > Directory Category (custom taxonomy)
    > Children and Family (term)
    > Entertainment (term)
    > Motoring (term)
    > Pets (term)

    I have the following page template:
    loop-page-directory.php (also with a page-directory.php) the former containing only the following code:

    <?php wp_tag_cloud( array( 'taxonomy' => 'directory-category', 'format'=>'list', 'smallest' => 16, 'largest' => 16, 'unit' => 'px' ) ); ?>

    This lists all the terms above Children, Entertainment, Motoring, Pets; I can click each item and it takes me to the business listings I've added with that term. This is housed on a page template called:
    loop-taxonomy-directory-category.php (I've created a corresponding taxonomy-directory-category.php). The former contains the following code:

    <!-- Business and Organisation Listings Page -->
    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <div class="entry-content">
    <!-- Custom Fields -->
    	<div id="wrapper-dirlist">
    	<div class="dirlist-brand"><?php echo get_post_meta($post->ID, 'bizbrand', true); ?></div>
        <div class="dirlist-content">
                <div class="dirlist-title"><?php the_title(); ?></div>
                <div class="dirlist-address"><?php echo get_post_meta($post->ID, 'dirlist-address', true); ?></div>
                <div class="dirlist-description"><?php the_content(); ?></div>
                <div class="dirlist-contactinfo">
                <li><?php echo get_post_meta($post->ID, 'dirlist-phone', true); ?></li>
                <li><?php echo get_post_meta($post->ID, 'dirlist-email', true); ?></li>
                <li><?php echo get_post_meta($post->ID, 'dirlist-website', true); ?></li>
                <li><?php echo get_post_meta($post->ID, 'dirlist-document', true); ?></li>
                <li><?php echo get_post_meta($post->ID, 'dirlist-voucher', true); ?></li>
                </div>
    	</div>
        </div><!-- .entry-content -->
    </div>
    <?php endwhile; // end of the loop. ?>

    This lists the items perfectly fine and they are styled okay with CSS.

    My problem and difficulty arises when I try to order the listed items randomly, so that my most recently added listing is not on top, but that all listings have a chance, through random ordering, to be on top.

    I've tried loads of things yesterday (this was my brick wall) including various pieces of code with 'orderby' => 'rand' in them, but I'm clearly not understanding this.

    Any help and advice is welcome.

    Thank you.

    Gavin.

  2. alchymyth
    The Sweeper
    Posted 1 year ago #

    http://codex.wordpress.org/Function_Reference/query_posts#Usage_Note
    http://codex.wordpress.org/Function_Reference/WP_Query#Order_.26_Orderby_Parameters

    try:

    <?php global $query_string;
    query_posts( $query_string . "&orderby=rand" );
    
    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
  3. gavinwebb
    Member
    Posted 1 year ago #

    Hi alchymyth

    Thank you very much, these appears to have worked. I should have got in touch sooner (and maybe got to bed earlier :))

    As a matter of interest, is the way I'm approaching this okay? It works, but if there is a better way to do it, what suggestions do you have?

    Thank you again.

  4. gavinwebb
    Member
    Posted 1 year ago #

    alchymyth

    I had both the codex references you posted in my bookmarks and had read through them several times. I think the issue for me was fully understanding what had been written, and where to put the final piece of code.

  5. alchymyth
    The Sweeper
    Posted 1 year ago #

    the codex is like an encyclopedia - all the words are there - but not the poem.

    'code is poetry'

    (motto of wordpress - bottomright of this page)

Topic Closed

This topic has been closed to new replies.

About this Topic