如何编辑此代码,生成每个帖子作为分隔div? WP

时间:2014-03-19 12:02:04

标签: wordpress

如何编辑此代码,将每个帖子生成为单独的div? 或者更好的方法是管理显示来自类别的其他帖子?

<?php
if ( is_single() ) {
  $categories = get_the_category();
  if ($categories) {
    foreach ($categories as $category) {
      // echo "<pre>"; print_r($category); echo "</pre>";
      $cat = $category->cat_ID;
      $args=array(
        'cat' => $cat,
        'post__not_in' => array($post->ID),
        'posts_per_page'=>-1,
        'caller_get_posts'=>1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<h2>Inne wpisy z tej kategorii: '. $category->category_description . '</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Przejdź do <?php the_title_attribute(); ?>"><h2><?php the_title(); ?></h2></a></p>
          <?php the_post_thumbnail( 'medium' ); ?> 
       <?php the_excerpt('10');
        endwhile;
      } //if ($my_query)
     } //foreach ($categories
  } //if ($categories)
  wp_reset_query();  // Restore global post data stomped by the_post().
} //if (is_single())
?>

2 个答案:

答案 0 :(得分:0)

如果您想在单独的div中打印每个帖子/类别,那么您可以添加foreach循环中包含的ECHO语句,例如:

ECHO "<div> <PRE>"; ..... ECHO "</div>"

我希望它会有所帮助......美好的一天

答案 1 :(得分:0)

<div>标记之前添加<p>并在<?php the_excerpt('10');?>之后关闭div应该实现此目的。

<?php
if ( is_single() ) {
  $categories = get_the_category();
  if ($categories) {
    foreach ($categories as $category) {
      // echo "<pre>"; print_r($category); echo "</pre>";
      $cat = $category->cat_ID;
      $args=array(
        'cat' => $cat,
        'post__not_in' => array($post->ID),
        'posts_per_page'=>-1,
        'caller_get_posts'=>1
      );
      $my_query = null;
      $my_query = new WP_Query($args);
      if( $my_query->have_posts() ) {
        echo '<h2>Inne wpisy z tej kategorii: '. $category->category_description . '</h2>';
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
          <div>
          <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Przejdź do <?php the_title_attribute(); ?>"><h2><?php the_title(); ?></h2></a></p>
          <?php the_post_thumbnail( 'medium' ); ?> 
       <?php the_excerpt('10');?>
           </div>
       <?php
        endwhile;
      } //if ($my_query)
     } //foreach ($categories
  } //if ($categories)
  wp_reset_query();  // Restore global post data stomped by the_post().
} //if (is_single())
?>