Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.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 显示特定类别的最后5篇文章(Wordpress)_Php_Wordpress_Function - Fatal编程技术网

Php 显示特定类别的最后5篇文章(Wordpress)

Php 显示特定类别的最后5篇文章(Wordpress),php,wordpress,function,Php,Wordpress,Function,我正试图显示某个特定类别的最后5篇文章,这些文章将链接到一个函数,这样我就可以在Wordpress页面中插入短代码。我的代码如下,它做了我需要的一切,尽管我也想添加特色图片,但它不显示特定类别的帖子 我尝试了很多方法,但都找不到有效的解决方案 function Last5posts() { $args = array( "showposts" => 5, "category" => 3 ); $content = ""; query_posts($a

我正试图显示某个特定类别的最后5篇文章,这些文章将链接到一个函数,这样我就可以在Wordpress页面中插入短代码。我的代码如下,它做了我需要的一切,尽管我也想添加特色图片,但它不显示特定类别的帖子

我尝试了很多方法,但都找不到有效的解决方案

function Last5posts()
{
    $args = array( "showposts" => 5, "category" => 3 ); 
    $content = "";   

    query_posts($args);

    if ( have_posts() ) : 

        while ( have_posts() ) :
            the_post();

            $link = get_permalink();
            $title = get_the_title();
            $date = get_the_date();                              

            $content .= "<div class='latest-posts'>";
            $content .= "<h3><a href='$link' target='_top'>$title / $date</a </h3>\n";
            $content .= "<p class='excerpt'>" . get_the_excerpt() . "</p>";
            $content .= "</div>";

        endwhile;

        wp_reset_query(); 

     endif;

     return $content;
}

add_shortcode('Last5Posts', 'Last5posts' );   
任何帮助都将不胜感激。

请查看:;你绝对应该使用cat而不是category。 另外,您是否以endwhile;?结束您的while;? 您的完整代码现在是什么样子的?

请查看以下内容:;你绝对应该使用cat而不是category。 另外,您是否以endwhile;?结束您的while;?
您的完整代码现在是什么样子的?

您可以像下面一样使用代码

query_posts( 'cat=3&posts_per_page=5' );

默认情况下,使用此wordpress将使用此代码之后的最后5篇文章…

您可以使用如下代码

query_posts( 'cat=3&posts_per_page=5' );
默认情况下,使用此wordpress将使用此代码之后的最后5篇文章…

使用此 $catnames[1]表示您希望使用与该帖子相关的类别

<?php $catnames = get_the_category();  
$postcatid=$catnames[1]->term_id;

$catquery = new WP_Query( 'cat='.$postcatid.'&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ;?>" rel="bookmark"><?php the_title(); ?></a>    </h3>
</li>
</ul>
 <?php endwhile; 
?>
用这个 $catnames[1]表示您希望使用与该帖子相关的类别

<?php $catnames = get_the_category();  
$postcatid=$catnames[1]->term_id;

$catquery = new WP_Query( 'cat='.$postcatid.'&posts_per_page=4' );
while($catquery->have_posts()) : $catquery->the_post();
?>
<ul>
<li><h3><a href="<?php the_permalink() ;?>" rel="bookmark"><?php the_title(); ?></a>    </h3>
</li>
</ul>
 <?php endwhile; 
?>

我正在使用下面添加的代码作为答案;它可以工作,但似乎也显示了“留一个回复”框,这是不应该的,因为我放置快捷码的页面已禁用评论;它可以工作,但似乎也显示了“留一个回复”框,这是不应该的,因为我放置快捷码的页面已禁用评论。