Wordpress:get_post_field()抛出错误,就像它试图重新定义一个函数一样

时间:2016-04-29 17:20:59

标签: php wordpress

我有一个名为getNews()的函数。

我这样打电话给getNews()

<div class="wrapper" id="blog">
    <div class="blogPosts">
        <?PHP getNews($postPerPage, 0, $theme_link, $pageNum); ?>
    </div>
</div>

该功能一直有效,直到它尝试使用get_post_field()从wordpress获取文章文本。

function getNews($numPosts, $offset, $theme_link, $pageNum) {
    $offset = $numPosts * $pageNum;
    $recent_args = array('numberposts' => $numPosts, 'offset' => $offset,);
    $articles = wp_get_recent_posts($recent_args);
    foreach($articles as $article){
        $article_id = $article['ID'];
        ...
        echo $article_text = get_post_field('post_content', $article_id);
        ...
    }
}

当我运行此操作时,我收到错误Fatal error: Cannot redeclare getNews() (previously declared in /homepages/30/d545089862/htdocs/VintageTroubleMusic/wp-content/plugins/enhanced-text-widget/enhanced-text-widget.php(57)

出于某种原因,它运行了两次循环。

有什么建议吗?

修改

我在这里包含完整的代码:这是在&#34;增强文本小部件&#34;内写的。在页面内。

<?PHP
    $theme_link = "/wp-content/themes/thestory-child/";
    $pageNum = $_GET['pa'];
    $postPerPage = 27;
    $qry_numPosts = mysql_query("SELECT * FROM Ykcfdlqhposts WHERE post_status = 'publish' AND post_type = 'post'") or die(mysql_error());
    $numPosts = mysql_num_rows($qry_numPosts);              
    $numPages = ceil($numPosts/$postPerPage) . " pages";
    if(!$pageNum){
        $pageNum = 0;   
    }
?>
<!-- START OF THE BODY HERE -->
<h3 class="widget-title" style="float:left;">Headlines</h3>
<div class="wrapper" id="paginate1">
    <?PHP paginate($numPosts, $numPages); ?>
</div>
<div class="wrapper" id="blog">
    <div class="blogPosts">
        <?PHP getNews($postPerPage, 0, $theme_link, $pageNum); ?>
    </div>
</div>
<div class="wrapper" id="paginate2">
    <?PHP paginate($numPosts, $numPages); ?>
</div>

functions.php页面内

function getNews($numPosts, $offset, $theme_link, $pageNum) {
$offset = $numPosts * $pageNum;
$recent_args = array('numberposts' => $numPosts, 'offset' => $offset,);
$articles = wp_get_recent_posts($recent_args);
foreach($articles as $article){
    $article_id = $article['ID'];
    $article_link = get_permalink($article_id);
    $article_length = 350;
    $article_background = get_the_post_thumbnail($article_id);
    $article_title = get_post_meta( $article_id, 'short_title', true );
    $article_text = "test";
    $article_text = get_post_field('post_content', $article_id);
    //$article_text = get_post_field('post_content', $article_id);
    //$article_text = strip_tags($article_text);
    //$article_text_length = strlen($article_text);
    /*
    if($article_text_length >= $article_length) {
        $article_text_pos = strpos($article_text, " ", $article_length);
        $article_text = substr($article_text, 0, $article_text_pos)."... ";
    }
    */
    ?>

    <div class="article">
        <?PHP //Get the article image
        if (!$article_background) {
            $rand = rand(1, 5);
            $img = $theme_link . "images/img" . $rand . ".jpg";
        } else {
            $img = explode('src="', $article_background);
            $img = explode(".jpg", $img[1]);
            $img = $img[0] . ".jpg";
        }?>   
        <a href="<?= $article_link; ?>">
            <div class="article__image" style="background-image: url('<?= $img; ?>');"></div>
            <div class="article__title"><?= $article_title; ?></div>
        </a>

        <div class="article__details">
            <div class="article-text">
                <?= $article_text; ?> 
            </div>
            <a href="<?= $article_link; ?>">READ MORE</a>
        </div>
    </div>
 <? }
}
function paginate($numPosts, $numPages) {
?>
<div class="page">
    <span>Page:</span><?PHP
        $i = 0;
        while($i < $numPages){
            $i++;
            if($i == $pageNum){
                echo '<a href="?pa='.$i.'"><span style="background-color:transparent; text-decoration:underline; color:white; cursor:default;">'.$i.'</span></a>';
            } else {
                echo "<a href='?pa=".$i."'><span>".$i."</span></a>";    
            }
        }
    ?>
</div>
<?
}

0 个答案:

没有答案