0

Taxonomies are a bit confusing. I want a "Moods" section for my music posts. So "Moods/Angry", "Moods/Sad", etc. and if you go to myurl.com/moods/angry/ there will be a list of posts that are tagged in the mood "Angry".

So I can either create a separate template page for each mood "taxonomy-moods-angry.php", "taxonomy-moods-love.php", etc. (this does work) and let wordpress grab the correct template or is there a way to only have 1 template and get the 'terms' => from the current page url? So if you're on myurl.com/moods/love/ it'll show all posts I've added to in the moods/love tag.

I have this in my functions.php file:

//MOODS TAXONOMY
add_action( 'init', 'create_moods_taxonomy' );
function create_moods_taxonomy() {
    $labels = array(
        'name'                           => 'Moods',
        'singular_name'                  => 'Mood',
        'search_items'                   => 'Search Moods',
        'all_items'                      => 'All Moods',
        'edit_item'                      => 'Edit Moods',
        'update_item'                    => 'Update Moods',
        'add_new_item'                   => 'Add New Moods',
        'new_item_name'                  => 'New Moods Name',
        'menu_name'                      => 'Moods',
        'view_item'                      => 'View Moods',
        'popular_items'                  => 'Popular Moods',
        'separate_items_with_commas'     => 'Separate moods with commas',
        'add_or_remove_items'            => 'Add or remove moods',
        'choose_from_most_used'          => 'Choose from the most used moods',
        'not_found'                      => 'No moods found'
    );
    register_taxonomy(
        'moods',
        'post',
        array(
            'label' => __( 'Moods' ),
            'hierarchical' => true,
            'labels' => $labels,
            'public' => true,
            'show_in_nav_menus' => false,
            'show_tagcloud' => false,
            'show_admin_column' => true

        )
    );
}

Then I created a taxonomy-moods.php:

<?php get_header(); ?>

<section class="content">

    <?php hu_get_template_part('parts/page-title'); ?>

    <div class="pad group">

        <div class="notebox">
            <?php echo term_description(); ?>
        </div>

<?php
  // set up or arguments for our custom query
  $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'post',
    'post_status'=>'publish',
    'posts_per_page' => 11,
    'paged' => $paged,
    'tax_query' => array(
        array(
            'taxonomy' => 'moods',
            'field' => 'name',
            'terms' => 'angry'
             )
        )
  );
  // create a new instance of WP_Query
  $the_query = new WP_Query( $query_args );
?>

    <?php $count = 1; ?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('group post-standard'); ?>>
    <div class="post-inner">
        <div class="post-thumbnail">
            <a href="<?php the_permalink(); ?>">
                <?php if ( has_post_thumbnail() ): ?>
                    <?php hu_the_post_thumbnail('beatpost-thumb'); ?>
                <?php elseif ( hu_is_checked('placeholder') ): ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/front/img/thumb-standard.png" alt="<?php the_title(); ?>" />
                <?php endif; ?>
                <?php if ( has_post_format('video') && !is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-play"></i></span>'; ?>
                <?php if ( has_post_format('audio') && !is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-volume-up"></i></span>'; ?>
                <?php if ( is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-star"></i></span>'; ?>
            </a>
            <?php if ( comments_open() && ( hu_is_checked( 'comment-count' ) ) ): ?>
                <a class="post-comments" href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i><?php comments_number( '0', '1', '%' ); ?></span></a>
            <?php endif; ?>
        </div><!--/.post-thumbnail-->

        <div class="post-content">

            <h2 class="post-title">
                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h2><!--/.post-title-->

            <div class="entry excerpt"><?php the_content(); ?></div>

            <div class="post-meta group">
            <p class="post-category"><?php the_category(', '); ?><i class="fa fa-angle-double-right"></i></p><p class="post-date"><?php the_time('m/d/Y'); ?><span class="anglemobile"><i class="fa fa-angle-double-right"></i></span></p><p class="cat-posts"><a href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i> <?php comments_number( '0', '1', '%' ); ?><span class="comment-text"> Comments</span></span></a></p>
            </div><!--/.post-meta-->
        </div><!--/.post-content-->
    </div><!--/.post-inner-->
</article><!--/.post-->
    <?php if ($count == 4) : ?>
    <div class="adsenseposts"><img src="https://storage.googleapis.com/support-kms-prod/SNP_3094702_en_v0" width="970" height="90"></div>
    <?php endif; $count++; ?>
<?php endwhile; ?>

<div id="navigation">
<?php $big = 999999999; // need an unlikely integer
echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'mid_size' => 3,
    'end_size' => 1,
    'total' => $the_query->max_num_pages
) ); ?>
</div>

<?php else: ?>
  <article>
    <h1>Sorry...</h1>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
  </article>
<?php endif; ?>


    </div><!--/.pad-->
</section><!--/.content-->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Which is set to "Angry" so currently only posts in that mood show.

1 Answer 1

2

You shouldn't need all that. the query is already assumed becuase you are using a standard template. So don't do a Wp_query.

Why not just try:

<?php while ( have_posts() ) : the_post(); ?>
    <?php the_content();  //obviously put your content information here.
    ?> 
    <?php endwhile; // end of the loop. ?>

or with your code:

<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('group post-standard'); ?>>
    <div class="post-inner">
        <div class="post-thumbnail">
            <a href="<?php the_permalink(); ?>">
                <?php if ( has_post_thumbnail() ): ?>
                    <?php hu_the_post_thumbnail('beatpost-thumb'); ?>
                <?php elseif ( hu_is_checked('placeholder') ): ?>
                    <img src="<?php echo get_template_directory_uri(); ?>/assets/front/img/thumb-standard.png" alt="<?php the_title(); ?>" />
                <?php endif; ?>
                <?php if ( has_post_format('video') && !is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-play"></i></span>'; ?>
                <?php if ( has_post_format('audio') && !is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-volume-up"></i></span>'; ?>
                <?php if ( is_sticky() ) echo'<span class="thumb-icon"><i class="fa fa-star"></i></span>'; ?>
            </a>
            <?php if ( comments_open() && ( hu_is_checked( 'comment-count' ) ) ): ?>
                <a class="post-comments" href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i><?php comments_number( '0', '1', '%' ); ?></span></a>
            <?php endif; ?>
        </div><!--/.post-thumbnail-->

        <div class="post-content">

            <h2 class="post-title">
                <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
            </h2><!--/.post-title-->

            <div class="entry excerpt"><?php the_content(); ?></div>

            <div class="post-meta group">
            <p class="post-category"><?php the_category(', '); ?><i class="fa fa-angle-double-right"></i></p><p class="post-date"><?php the_time('m/d/Y'); ?><span class="anglemobile"><i class="fa fa-angle-double-right"></i></span></p><p class="cat-posts"><a href="<?php comments_link(); ?>"><span><i class="fa fa-comments-o"></i> <?php comments_number( '0', '1', '%' ); ?><span class="comment-text"> Comments</span></span></a></p>
            </div><!--/.post-meta-->
        </div><!--/.post-content-->
    </div><!--/.post-inner-->
</article><!--/.post-->
    <?php endwhile; // end of the loop. ?>
1
  • So I was severely over complicating it lol, thanks rudtek that worked great.
    – Nate M.
    Mar 18, 2017 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.