在每个类别wordpress下面显示帖子标题

时间:2013-07-28 06:48:06

标签: wordpress

我有一个wordpress网站,我想在类别中显示帖子标题列表。像

Category 1
post 1
post 2

category 2 
post 1
post 2
post 3

category 3
post 1 
post 2

    $show_count = 0; 
    $pad_counts = 0; 
    $hierarchical = 1; 
    $taxonomy = 'filter';
    $title = true;
    $description = true;

    $args = array(
    'show_count' => $show_count,
    'pad_counts' => $pad_counts,
    'hierarchical' => $hierarchical,
    'taxonomy' => $taxonomy,
    'use_desc_for_title' => $description,
    'title_li' => $title
     );

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


    //display posts
$cat_new = $category->term_id;
$post_args = array( 'numberposts' => -1, 'category' => $cat_new, 'caller_get_posts' => 0 );


$myposts = get_posts( $post_args );
    foreach( $myposts as $post ) :  setup_postdata($post); ?>

echo '<li><a href="<?php the_permalink(); ?>"><?php the_title(); ?>';

   endforeach; }   

上面的代码只显示了一个类别列表,而不是它下面的帖子标题。

我很困惑,我已经阅读了很多博客,并为此发帖但仍未完成。任何帮助将不胜感激。提前谢谢。

3 个答案:

答案 0 :(得分:2)

首先,您的代码已损坏,可能会破坏您的主题,因此帖子不会显示。在endforeach之前的最后一行缺少

  • 和的结束标记。 PHP代码中也存在php开始和结束标记,这是非常错误的。

        $show_count = 0; 
            $pad_counts = 0; 
            $hierarchical = 1; 
            $taxonomy = 'filter';
            $title = true;
            $description = true;
    
            $args = array(
            'show_count' => $show_count,
            'pad_counts' => $pad_counts,
            'hierarchical' => $hierarchical,
            'taxonomy' => $taxonomy,
            'use_desc_for_title' => $description,
            'title_li' => $title
             );
    
           $categories=get_categories($args);
           foreach($categories as $category) { 
          echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> ';
    
    
    
        $cat_new = $category->term_id;
        $post_args = array( 'numberposts' => -1, 'category' => $cat_new, 'caller_get_posts' => 0 );
    
    
        $myposts = get_posts( $post_args );
            foreach( $myposts as $post ) :  setup_postdata($post);
    
        echo '<li><a href="'.the_permalink().'">'.the_title().'</a></li>';
    
           endforeach;
    endforeach;
     }  
    
  • 答案 1 :(得分:0)

    您在此代码中使用了一个非常陈旧且已弃用的参数caller_get_posts

    $post_args = array( 'numberposts' => -1, 'category' => $cat_new, 'caller_get_posts' => 0 );
    

    您应该使用ignore_sticky_posts

    替换
    $post_args = array( 'numberposts' => -1, 'category' => $cat_new, 'ignore_sticky_posts' => 0 );
    

    答案 2 :(得分:-1)

    <?php
        $query = new WP_Query( array( 'post_type' => 'post','posts_per_page' => -1 ) ); 
        while($query->have_posts()) : $query->the_post();
    ?>
    <ul>
        <li>
        <h3><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h3>
            <a href="<?php the_permalink() ?>" rel="bookmark"><?php the_category(); ?></a> 
    
        </li>
    </ul>
    <?php endwhile; ?>