0

我尝试为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();?>

问候,史蒂夫

4

3 回答 3

1

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

于 2010-07-23T10:27:48.657 回答
0

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

解决方案

<?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,谢谢你的建议。

于 2010-07-23T18:45:33.533 回答
0

在默认的 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();

    ?>
于 2017-09-15T12:35:43.963 回答