首页 文章

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

提问于
浏览
-1

我有一个wordpress网站,我想在类别中显示帖子 Headers 列表 . 喜欢

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; }

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

我很困惑,我已经阅读了很多博客并发布了这个但仍然没有完成 . 任何帮助将不胜感激 . 提前致谢 .

3 回答

  • 0

    首先,您的代码被破坏,可能会破坏您的主题,因此帖子不会显示 . 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;
     }
    
  • 2

    您在此代码中使用了一个非常古老且已弃用的参数 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 );
    
  • -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; ?>
    

相关问题