2013-12-17 43 views
1

我试图在WordPress中使用get_posts()函数来显示我的主页上特定类别的帖子列表。这里是我的代码:为什么不是这个get_posts()WordPress功能不工作?

<div id="announcements"> 

    <?php 
    $myposts = get_posts('category'=>3); 
    foreach ($myposts as $post) : setup_postdata($post); 
    ?> 

    <div id="announcement-post-title"> 
     <b><a href="<?php the_permalink() ?>" title="Permanent Link to 
        <?php the_title(); ?>" class="homepage-post-title"> 
     <?php the_title(); ?></a></b> 
    </div> 


    <div id="announcement-post-content"> 
     <?php the_content(); ?> 
    </div> 

    <div id="announcement-post-read-more"> 
     <a href="<?php the_permalink() ?>" class="read-more-button">Read 
        More</a> 
    </div> 

    <?php endforeach; ?> 

</div> 

当我查看网页,不过,我得到以下错误:

解析错误:语法错误,意想不到的T_DOUBLE_ARROW在/和home1 /吨/的public_html/resources2 /可湿性粉剂内容/themes/twentytwelve/front-page.php在线26

第26行是这个:

$myposts = get_posts('category'=>3); 

任何想法,为什么发生这种情况?

在此先感谢!

+3

你需要用'阵列()'周围的类别件事:'阵列( '类'=> 3)' –

+0

我上投佩卡,他是正确的,你正在存储一个数组内的变量。你的类别3有更多的东西,所以你需要一个数组来存储这些值 – Ljubisa

+0

你能说清楚你的意思是“你的类别3有多少东西在里面”吗?谢谢! –

回答

0

get_posts()函数需要传递数组。

尝试:

$myposts = get_posts(array('category' => 3)); 

See the wordpress codex

+0

这在技术上并非如此。你可以像这样传递一个字符串: '$ myposts = get_posts('category = 3');'' –