如何在functions.php中使用get_posts()函数

时间:2015-10-16 08:34:47

标签: php wordpress

我正在尝试使用ajax加载帖子。但我没有得到任何内容的帖子。我没有发布我的javascript,因为它非常直接。 这是我在inc / helper-functions.php中的代码,它包含在main functions.php。

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    <div class="meta">
        <?php the_time('d.m.Y') ?>
    </div>

    <header class="entry-header">
        <?php  the_title( sprintf( '<h2 class="entry-title"><a href="%s" rel="bookmark">', esc_url( get_permalink() ) ), '</a></h2>' ); ?>

    </header>

    <div class="entry-content">
        <?php the_excerpt(); ?>
    </div>

</article>

以下是content.php的样子:

postBack="true"

我正在完成循环,但是没有得到要打印的内容。没有以前的功能。 the_ID(),the_excerpt(),post_class()等不起作用。如果我在模板中使用相同的代码,它可以正常工作。

1 个答案:

答案 0 :(得分:3)

$post是全球性的,因此在使用setup_postdata( $post );之前(必须是$post不是 $anyvar!)您需要添加它到目前的范围。 在使用setup_postdata的函数的开头添加以下内容:

<强> global $post;