Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 如何编辑此代码,以将每个帖子作为单独的div生成?可湿性粉剂_Wordpress - Fatal编程技术网

Wordpress 如何编辑此代码,以将每个帖子作为单独的div生成?可湿性粉剂

Wordpress 如何编辑此代码,以将每个帖子作为单独的div生成?可湿性粉剂,wordpress,Wordpress,如何编辑此代码,以将每个帖子作为单独的div生成? 或者这是更好的方式来管理显示其他类别的帖子 <?php if ( is_single() ) { $categories = get_the_category(); if ($categories) { foreach ($categories as $category) { // echo "<pre>"; print_r($category); echo "</pre>";

如何编辑此代码,以将每个帖子作为单独的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())
?>


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

<?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())
?>
ECHO”“。。。。。回声“”
我希望它会有帮助…Good day

标记之前添加
,并在
之后关闭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())
?>