Wordpress帖子>ID问题


Wordpress post->ID Problem

这是一个wordpress问题。我正试图使用一些代码,在我的主页上我的内页模板上工作得很好:

query_posts('cat=4');
    // The Loop
    echo '<div id="cal_details"><ul>';
    while ( have_posts() ) : the_post();
        $cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
        $cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
            $my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));

            $issetdate = get_post_meta($post->ID, 'date_value', true);
            if (isset($issetdate)) {
            echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
            echo '<a href="' . get_permalink() . '">';
            the_title();                        
                echo '</a></li>';
            }
    endwhile;
    echo '</ul></div>';

然而,这似乎对内页不起作用。所有的标题链接都正确地输出了,但是它不能正确地打印get_post_meta部分。

列表项都显示类似于<li class="cal_event_li list_item_1_1">

我认为可能有一些问题的方式,我试图使用$post->ID,但我不知道这里发生了什么。什么好主意吗?

当您使用query_posts时,您必须调用global $post来获取post_meta。如果您只调用一个类别,为什么不直接使用归档模板呢?

另外,如果您要使用query_posts,请确保您重置查询后,以便插件,侧边栏等仍然可以与循环的条件等进行交互。

global %post;
query_posts('cat=4');
    // The Loop
    //more stuff
endwhile;
wp_reset_query();

尝试将$post->ID替换为内页中的the_ID()。像这样

query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
    $cal_date_j = date('j', intval(get_post_meta(the_ID(), 'date_value', true)));
    $cal_date_n = date('n', intval(get_post_meta(the_ID(), 'date_value', true)));
        $my_array[] = date('j, n', intval(get_post_meta(the_ID(), 'date_value', true)));

        $issetdate = get_post_meta(the_id(), 'date_value', true);
        if (isset($issetdate)) {
        echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
        echo '<a href="' . get_permalink() . '">';
        the_title();                        
            echo '</a></li>';
        }
endwhile;
echo '</ul></div>';