代码之家  ›  专栏  ›  技术社区  ›  gearsdigital

如何合并自定义的post类型数组?

  •  0
  • gearsdigital  · 技术社区  · 16 年前

    我尝试设置每年(按月分组)的存档 自定义日志类型 在WordPress中。但是我的代码没有按预期工作。也许对于更熟悉WordPress和PHP的人来说是显而易见的,但我不能让它工作。

    下面的代码是按月分组的 但是 每个post类型本身。也许我需要合并布思。但是如何呢?

    <?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();?>
    

    问候,史提夫

    3 回复  |  直到 8 年前
        1
  •  1
  •   negatif    16 年前

    您需要: http://wordpress.org/extend/plugins/posts-to-posts/ 以实现你想要的定制帖子。

        2
  •  0
  •   gearsdigital    16 年前

    好啊。。。喝了一杯茶之后,我用另一种(可能更好的)方法喝到了。

    解决方案

    <?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
    

    现在只要稍微美化一下就行了。顺便说一句 “否定” ,谢谢你的建议。

        3
  •  0
  •   Dipak Mahajan    8 年前

    简单而正确的方法 在默认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();
    
        ?>