显示wordpress中每个指定类别的最新帖子

时间:2013-03-12 06:33:19

标签: php wordpress

<div id="portfolio">
        <ul id="portfolio-items" >
            <?php $query = new WP_Query(); $count = 1;
            //$catquery = new WP_Query( 'category_name=roomsandspecials&posts_per_page=10' );

             $categories12 = get_terms('skill-type', $cat_args );


// These functions and add_filters could go in your functions.php file
function mam_posts_fields ($fields) {
   global $mam_global_fields;
   // Make sure there is a leading comma
   if ($mam_global_fields) $fields .= (preg_match('/^(\s+)?,/',$mam_global_fields)) ? $mam_global_fields : ", $mam_global_fields";
   return $fields;
}
function mam_posts_where ($where) {
   global $mam_global_where;
   if ($mam_global_where) $where .= " $mam_global_where";
   return $where;
}
add_filter('posts_fields','mam_posts_fields');
add_filter('posts_where','mam_posts_where');

$mam_global_fields = ", $wpdb->term_relationships.term_taxonomy_id";
$mam_global_where = " AND post_date =
   (SELECT MAX(p.post_date)
   FROM $wpdb->posts p, $wpdb->term_relationships tr
   WHERE p.ID = tr.object_id
   AND tr.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
   AND p.post_type = 'portfolio'
   AND p.post_status = 'publish'
   AND p.post_date < NOW() ) ";



for ($i = 0; $i <  count($categories12); $i++) {


query_posts( array(             
                'post_type' => 'portfolio',
                'tax_query' => array(
                    array(
                        'taxonomy' => 'skill-type',
                        'field' => 'id',
                        'terms' => $categories12[$i]->term_id
                    )
                )
            ) );

//query_posts($args);

 if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
 <li <?php post_class() ?> id="post-<?php the_ID(); ?>">
 <?php get_the_image( array( 'size' => 'portfolio-thumb',  'width' => 220, 'height' => 140, 'link_to_post' => false  ) ); ?>
  <div class="meta">
 <h3><a href="<?php the_permalink(); ?>" rel="bookmark" title="<?php printf(__('Permanent Link to %s', 'wpzoom'), get_the_title()); ?>">  <?php the_title(); ?></a></h3>
  <?php echo $categories12[$i]->name; ?>
 </div>
   </li>
 <?php 
 //echo $categories12[$i]->term_id; 
 endwhile; endif; }
  ?>
</ul></div>

我正在使用上面的代码显示来自类别的最新帖子。当我不使用add_filter('posts_fields','mam_posts_fields')时,使用此代码滑块在风险主题中不起作用; 的add_filter( 'posts_where', 'mam_posts_where'); 这显示了所有类别的帖子。我想获得所有类别的最新帖子

1 个答案:

答案 0 :(得分:0)

请尝试以下脚本并告诉我

<?php
$catquery = new WP_Query( 'cat=3&posts_per_page=5' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>    </h3>
<ul><li><?php the_content(); ?></li>
</ul>
</li>
</ul>
<?php endwhile; ?>




or

<?php query_posts('category_name=wordpress&showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink(); ?>">
<?php the_title(); ?>  

      </a>  </li>

    <?php endwhile; ?>

请检查以下两个脚本        第一个脚本

    <?php 
    $cat_args = array(
      'orderby' => 'name',
      'order' => 'ASC',
      'child_of' => 0
    );

    $categories =   get_categories($cat_args); 

    foreach($categories as $category) { 
        echo '<dl>';
        echo '<dt> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a></dt>';

         $post_args = array(
          'numberposts' => 1,
          'category' => $category->term_id 
        );

        $posts = get_posts($post_args);

        foreach($posts as $post) {
        ?>
            <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
        <?php 
        } 
        echo '<dd class="view-all"> <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>View all posts in ' . $category->name.'</a></dd>';
        echo '</dl>';
    } 
    ?>
   **Second script**

        This will print categories and posts
        <?php
        //for example categories ids 42,6,8, show 1 posts
        $cat_args=array(
          'include' => '42,6,8',
          'orderby' => 'name',
          'order' => 'ASC'
           );
        $categories=get_categories($cat_args);
          foreach($categories as $category) {
            $args=array(
              'showposts' => 1,
              'category__in' => array($category->term_id),
              'caller_get_posts'=>1
            );
            $posts=get_posts($args);
              if ($posts) {
                echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
                foreach($posts as $post) {
                  setup_postdata($post); ?>
                  <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
                  <?php
                } // foreach($posts
              } // if ($posts
            } // foreach($categories
        ?>