在一个页面上发布多个类别帖子

时间:2014-06-17 作者:Nsokyi

是否可以在页面上设置一个部分,显示来自4个不同类别帖子的2段摘录,包括它们的特色图片?

到目前为止,我所拥有的只是:

            <?php query_posts("showposts=2&cat=11,12,13,14"); ?>
            <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
               <?php the_excerpt(); ?>
               <?php featured-image(); ?>
            <?php endwhile; endif; ?>

            <?php wp_reset_query(); ?>
但我不确定它是否会显示4 x 2个摘要,每个摘要中都有一个特色图片?

我的自定义类别添加如下:

function event_post_example() {

register_post_type( \'event_type\',
    array(
    \'labels\' => array(
        \'name\' => __(\'Events Posts\', \'baretheme\'),
        \'singular_name\' => __(\'Event Post\', \'baretheme\'),
        \'all_items\' => __(\'All Event Posts\', \'baretheme\'),
        \'add_new\' => __(\'Add New Event Post\', \'baretheme\'),
        \'add_new_item\' => __(\'Add New Event Type\', \'baretheme\'),
        \'edit\' => __( \'Edit\', \'baretheme\' ),
        \'edit_item\' => __(\'Edit Post Types\', \'baretheme\'),
        \'new_item\' => __(\'New Post Type\', \'baretheme\'),
        \'view_item\' => __(\'View Post Type\', \'baretheme\'),
        \'search_items\' => __(\'Search Post Type\', \'baretheme\'),
        \'not_found\' =>  __(\'Nothing found in the Database.\', \'baretheme\'),
        \'not_found_in_trash\' => __(\'Nothing found in Trash\', \'baretheme\'),
        \'parent_item_colon\' => \'\'
    ), /* end of arrays */
    \'description\' => __( \'This is the example event post type\', \'baretheme\' ), /* Custom Type Description */
    \'public\' => true,
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'menu_position\' => 9,
    \'menu_icon\' => get_stylesheet_directory_uri() . \'/library/images/custom-post-icon.png\',
    \'rewrite\'   => array( \'slug\' => \'event_type\', \'with_front\' => false ),
    \'has_archive\' => \'event_type\',
    \'capability_type\' => \'post\',
    \'hierarchical\' => false,

    \'supports\' => array( \'title\', \'editor\', \'author\', \'thumbnail\', \'excerpt\', \'trackbacks\', \'custom-fields\', \'comments\', \'revisions\', \'sticky\')
    ) /* end of options */
); /* end of register post type */

register_taxonomy( \'event_cat\',\'event_type\',
    array(
        \'labels\' => array(
        \'name\' => __( \'Event Categories\', \'baretheme\' ),
        \'singular_name\' => __( \'Event Category\', \'baretheme\' ),
        \'search_items\' =>  __( \'Search Event Categories\', \'baretheme\' ),
        \'all_items\' => __( \'All Event Categories\', \'baretheme\' ),
        \'parent_item\' => __( \'Parent Event Category\', \'baretheme\' ),
        \'parent_item_colon\' => __( \'Parent Event Category:\', \'baretheme\' ),
        \'edit_item\' => __( \'Edit Event Category\', \'baretheme\' ),
        \'update_item\' => __( \'Update Event Category\', \'baretheme\' ),
        \'add_new_item\' => __( \'Add New Event Category\', \'baretheme\' ),
        \'new_item_name\' => __( \'New Event Category Name\', \'baretheme\' )
    ),
    \'hierarchical\' => true,
    \'show_admin_column\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    \'rewrite\' => array( \'slug\' => \'event-slug\' ),
    )
);

register_taxonomy( \'event_tag\',\'event_type\',
    array(
        \'labels\' => array(
        \'name\' => __( \'Event Tags\', \'baretheme\' ),
        \'singular_name\' => __( \'Event Tag\', \'baretheme\' ),
        \'search_items\' =>  __( \'Search Event Tags\', \'baretheme\' ),
        \'all_items\' => __( \'All Event Tags\', \'baretheme\' ),
        \'parent_item\' => __( \'Parent Event Tag\', \'baretheme\' ),
        \'parent_item_colon\' => __( \'Parent Event Tag:\', \'baretheme\' ),
        \'edit_item\' => __( \'Edit Event Tag\', \'baretheme\' ),
        \'update_item\' => __( \'Update Event Tag\', \'baretheme\' ),
        \'add_new_item\' => __( \'Add New Event Tag\', \'baretheme\' ),
        \'new_item_name\' => __( \'New Event Tag Name\', \'baretheme\' )
    ),
    \'hierarchical\' => false,
    \'show_admin_column\' => true,
    \'show_ui\' => true,
    \'query_var\' => true,
    )
);
}

add_action( \'init\', \'event_post_example\');

2 个回复
最合适的回答,由SO网友:Pieter Goosen 整理而成

我首先想插嘴说,你不应该使用query_posts, 我的重点,在法典中也有说明,我引用

Note: 此功能不适用于插件或主题。如后文所述,有更好、性能更好的选项来更改主查询。query\\u posts()是一种过于简单且有问题的方法,通过将页面的主查询替换为新的查询实例来修改它。它效率低下(重新运行SQL查询),并且在某些情况下会彻底失败(尤其是在处理POST分页时)

也就是说,你可以使用WP_Query 构建自定义循环。您需要在这里运行四个不同的循环,因为您将显示来自三个不同分类法和一个类别的两篇文章。您还需要显示the_excerpt()the_post_thumbnail()

此处可以使用以下参数:

  • posts_per_page 将要检索的帖子数量设置为2cat 从一个循环的特定类别中获取帖子tax_query 要从分类中获取帖子,这里是从类别中获取帖子的完整查询。只需将cat ID更改为类别的ID,并添加所需的所有html标记

    <?php 
    $args = array(
        \'cat\' => 21,
        \'posts_per_page\' => \'2\'
    );
    
    $the_query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
            if ( has_post_thumbnail() ):
                the_post_thumbnail();
            endif;
    
            the_excerpt(); 
    
        endwhile; 
    wp_reset_postdata(); 
    endif; 
    
    对于分类法来说,这就可以了。记得去看看WP_Query

    $args = array(
    \'post_type\' => \'event_type\',
    \'posts_per_page\' => \'2\'
        \'tax_query\' => array(
            array(
                \'taxonomy\' => \'event_cat\',
                \'field\' => \'slug\',
                \'terms\' => \'testcat\'
            )
        )
    );
    $the_query = new WP_Query( $args );
    
    if ( $the_query->have_posts() ) : 
        while ( $the_query->have_posts() ) : $the_query->the_post(); 
    
            if ( has_post_thumbnail() ):
                the_post_thumbnail();
            endif;
    
            the_excerpt(); 
    
        endwhile; 
    wp_reset_postdata(); 
    endif; 
    

SO网友:eyoung100

这应该让您开始:(我将把Pieter的答案与这个答案结合起来,并附上注释,以便您了解循环的位置和原因:

<?php
// We build the query as an array to
// allow us to update it easily in one place
// without breaking it
$sql_args = array(\'category__and\' => array(11,12,13,14), \'orderby\' = rand, \'posts_per_page\' = 2);
// We now "overload" the function with the array
// we constructed above.
$custom_query = new WP_Query($sql_args);
// OuterLoop, this is the loop you need to customize
  while ($custom_query->have_posts()) : $custom_query->the_post();
  $do_not_duplicate[] = $post->ID;
     if ( has_post_thumbnail() ):
        the_post_thumbnail();
    endif;

    the_excerpt(); 
 endwhile;
    wp_reset_postdata();

// InnerLoop, we can manipulate items further
// in the default WordPress query. As Pieter 
// pointed out, do not use query_posts.
      if (have_posts()) : while (have_posts()) : the_post(); 
      if(in_array ($post->ID == $do_not_duplicate )) continue;
// Show all Posts in all categories
// that weren\'t chosen by random above.
// If you don\'t want to display the leftovers
// Remove the InnerLoop
       the_content();
  endwhile;
 endif; ?>

结束

相关推荐

使用Get_Categories显示类别的图像,或显示任何子帖子中的图像

我正在使用get\\u categories列出父类别的子类别。我想使用get\\u categories输出将图像添加到子类别。我可以从我正在使用get\\u categories的类别的子类别(即父类别的子类别)的任何帖子中获取特色图像。我不想显示任何其他孙儿信息,只想从每组类别的孩子中获得一张特色图片我当前使用的代码是$args = array(\'child_of\' => 1 ); $categories = get_categories($args); forea