获取当前页面URL的某些分类术语中的帖子

时间:2017-03-16 作者:Nate M.

分类法有点混乱。我想为我的音乐帖子设置一个“情绪”部分。所以“情绪/愤怒”,“情绪/悲伤”等等,如果你去我的URL。com/moods/angry/将有一个标记为“angry”的帖子列表。

因此,我可以为每个情绪“taxonomy moods angry.php”、“taxonomy moods love.php”等创建一个单独的模板页面(这确实有效),然后让wordpress获取正确的模板,或者有没有办法只使用一个模板并获得\'terms\' => 从当前页面url?所以如果你在myurl上。com/moods/love/它将显示我在moods/love标签中添加的所有帖子。

我有这个functions.php 文件:

//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

        )
    );
}
然后我创建了一个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(); ?>
设置为“愤怒”,因此目前仅在该情绪秀中发布帖子。

1 个回复
最合适的回答,由SO网友:rudtek 整理而成

你不应该需要这些。由于您使用的是标准模板,因此已假定查询。因此,不要执行Wp\\U查询。

为什么不试试:

<?php while ( have_posts() ) : the_post(); ?>
    <?php the_content();  //obviously put your content information here.
    ?> 
    <?php endwhile; // end of the loop. ?>
或使用您的代码:

<?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. ?>

相关推荐

如何控制根据Taxonomy术语显示什么模板?

我正在建立一个商业目录,并将给出一些背景,然后在最后有2个问题。The development URL is: http://svcta.lainternet.biz/The website I am rebuilding is: https://www.visitsimivalley.com/当前网站要求每个分类法类型具有唯一的业务概要文件。例如,如果您是一家酒店,并且您也有会议室和婚礼场地,那么您最终会得到3个列表,一个用于酒店,一个用于会议,一个用于婚礼。我希望有一个主配置文件,其中包含我们将显示的