【问题标题】:WordPress: How to merge custom post type array?WordPress:如何合并自定义帖子类型数组?
【发布时间】:2010-07-22 21:52:34
【问题描述】:

我尝试为 WordPress 中的自定义帖子类型设置一个年度(按月分组)存档。但是我的代码没有按方面工作。对于更熟悉 WordPress 和 PHP 但我无法正常工作的人来说,这显然是显而易见的。

下面的代码按月分组每个帖子类型本身。也许我需要合并展位。但是怎么做呢?

<?php query_posts (array ('post_type' => array('images', 'articles')));?>

    <?php
        if (have_posts()) : while (have_posts()) : the_post();

        // save month to a variable
        $month = the_date('M', '', '', FALSE);

        // If not used before print it 
        if ($month != $m_check) {
            echo "<h2>" . $month . "</h2>";
        }

        // save month to check variable
        $m_check = $month;
    ?>

    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a><br/>

    <?php endwhile; ?>
    <?php endif; ?>
<?php wp_reset_query();?>

问候,史蒂夫

【问题讨论】:

    标签: arrays wordpress merge custom-post-type


    【解决方案1】:

    你需要这个:http://wordpress.org/extend/plugins/posts-to-posts/ 来实现你想要的自定义帖子。

    【讨论】:

      【解决方案2】:

      好的……喝完茶后,我用另一种(可能更好)的方法得到了它。

      解决方案

      <?php
      
          $args = array(
              'post_type' => array('images', 'articles'),
              'numberposts' => -1,
              'post_status' => null,
              'post_parent' => null,
              );
      
          $posts = get_posts($args);
      
          if ($posts) {
              foreach ($posts as $post) {
                  setup_postdata($post);
                  $month =  mysql2date('m', $post->post_date);
      
                  if ($month != $check) {
                      echo "<h2>" . $month . "</h2>";
                  }
      
                  // save month to check variable
                  $check = $month;
      
                  echo $post->post_title;
                  echo '<br/>';
              }
          }
      
          ?>
      

      输出

      07
          Eagle creek
          Lorem Ispum dolor
          Vancouver Island
          Ottawa
          Vancouver
      06
          Losabim oxygenium
      

      现在它只需要一点美化,我就完成了。顺便说一下 @negatif,感谢您的建议。

      【讨论】:

        【解决方案3】:

        将我们的自定义循环参数合并到默认 wordpress 循环中的简单而正确的方法。

            $argss = array(
                    'paged' => $paged,
                    'posts_per_page' => 9,
                    'meta_query' => array(
                                        'relation' => 'OR',
                                        array (
                                            'key' => '_expiration-date',
                                            'value' => $tode,  // custom values
                                            'compare' => '>='
                                        ),
                                        array (
                                            'key' => '_expiration-date',
                                            'compare' => 'NOT EXISTS'
                                        ),
                                    ),
                );
        
        
                global $wp_query;
        
                // Merge custom query with $wp_query
                $merged_args = array_merge( $wp_query->query, $argss );
        
                // Query posts using the modified arguments
                query_posts( $merged_args );
        
                if ( have_posts() ) : while (have_posts()) : the_post(); 
                .
                .
                .
                .
                .
                ..
                 else : ?>
                <p><?php _e('Apologies, but no entries were found.', 'tb'); ?></p>
            <?php endif;
        
            //wp_reset_query();
        
            ?>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-27
          • 1970-01-01
          • 2016-12-29
          相关资源
          最近更新 更多