Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress函数在模板中不可用_Php_Wordpress - Fatal编程技术网

Php Wordpress函数在模板中不可用

Php Wordpress函数在模板中不可用,php,wordpress,Php,Wordpress,我正在编辑front-page.php,在那里我使用get_posts()输出帖子。 其他方法,如使用帖子都不起作用。所以我需要输出摘录,但是$post->post_extract是空的,这个函数什么也不做。我不明白为什么,因为没有错误。代码如下: <?php foreach ( get_posts() as $post ) { ?> <article id="post-<?php the_ID(); ?>" <?php post_c

我正在编辑front-page.php,在那里我使用
get_posts()
输出帖子。 其他方法,如使用
帖子
都不起作用。所以我需要输出摘录,但是$post->post_extract是空的,
这个函数什么也不做。我不明白为什么,因为没有错误。代码如下:

<?php

    foreach ( get_posts() as $post ) { ?>
        <article id="post-<?php the_ID(); ?>" <?php post_class('article-item well'); ?>>
            <h2 class="title text-primary">
                <a href="<?php echo $post->guid; ?>">
                    <?php echo $post->post_title; ?>
                </a>
            </h2>
            <p class="article-info text-center">
            <span class="date">Posted on <time pubdate="" title="12:19 pm" datetime="<?php echo $post->post_date; ?>" class="time">
                    <?php echo $post->post_date; ?>
                </time>
            </span>
            </p>
            <?php if (has_post_thumbnail()) { ?>
                <figure class="img-wrap">
                    <?php the_post_thumbnail('full'); ?>
                    <figcaption class="label label-primary">
                        <?php foreach((get_the_category()) as $category) {
                            echo $category->cat_name . ' ';
                        } ?>
                    </figcaption>
                </figure>
            <?php } ?>
            <p>
                <?php the_excerpt(); ?>
            </p> 


发布于在循环的每次迭代中,您需要“设置”post数据,您可以使用
setup\u postdata($post)来执行此操作

修改您的代码如下:

<?php

foreach ( get_posts() as $post ) { 
    setup_postdata( $post );
    ?>
    <article id="post-<?php the_ID(); ?>" <?php post_class('article-item well'); ?>>
        <h2 class="title text-primary">
            <a href="<?php echo $post->guid; ?>">
                <?php echo $post->post_title; ?>
            </a>
        </h2>
        <p class="article-info text-center">
        <span class="date">Posted on <time pubdate="" title="12:19 pm" datetime="<?php echo $post->post_date; ?>" class="time">
                <?php echo $post->post_date; ?>
            </time>
        </span>
        </p>
        <?php if (has_post_thumbnail()) { ?>
            <figure class="img-wrap">
                <?php the_post_thumbnail('full'); ?>
                <figcaption class="label label-primary">
                    <?php foreach((get_the_category()) as $category) {
                        echo $category->cat_name . ' ';
                    } ?>
                </figcaption>
            </figure>
        <?php } ?>
        <p>
            <?php the_excerpt(); ?>
        </p> 

张贴在