Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/233.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.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_Loops - Fatal编程技术网

Php Wordpress短码在调用两次时复制帖子

Php Wordpress短码在调用两次时复制帖子,php,wordpress,loops,Php,Wordpress,Loops,我已经建立了一个短代码,可以显示特定类别中的最新帖子,下面是代码 function latest_post($atts = '') { $atts = shortcode_atts( array( 'cat' => '', ), $atts ); $args = array( 'posts_per_page' => 1, 'cat' => $atts['cat'] ); $content =

我已经建立了一个短代码,可以显示特定类别中的最新帖子,下面是代码

function latest_post($atts = '') {
    $atts = shortcode_atts( array(
        'cat' => '',
    ), $atts );
    $args = array(
       'posts_per_page' => 1,
       'cat' => $atts['cat']
    );

    $content = "";
    $posts = get_posts($args);

    foreach($posts as $post):
       $content = $content."<h2>". apply_filters( 'the_title', $post->post_title) ."</h2>";
       $content = $content."<img src='".get_the_post_thumbnail_url(get_the_ID(),'full')."' />";
       $content = $content."<p class='post-content-custom'>". apply_filters( 'the_content', $post->post_content ) ."</p>";
    endforeach;

    return $content;
}

add_shortcode('latest_post', 'latest_post');
函数最新发布($atts=''){
$atts=短码_atts(数组)(
“猫”=>“,
)(港币),;
$args=数组(
“每页帖子数”=>1,
“cat”=>$atts[“cat”]
);
$content=“”;
$posts=get_posts($args);
foreach($post为$post):
$content=$content.“。应用_过滤器('the_title',$post->post_title)。”;
$content=$content.“;
$content=$content.“

”。应用_过滤器('the_content',$post->post_content)。“

”; endforeach; 返回$content; } 添加快捷码(“最新发布”、“最新发布”);
现在的问题是,我需要在同一个页面中调用这个插件两次或更多次,但是如果最新的帖子已经通过相同的短代码显示在页面上,我需要它来显示下一篇帖子

例如,我需要:

[latest_post cat=“cars”]
[latest_post cat=“books”]``[latest_post cat=“cars”]


现在,当我第二次调用
[latest_post cat=“cars”]
时,它应该会显示
最新的post-1
,因为最新的post已经被第一个短代码调用了。

短代码不是这样工作的——它们会破坏内容。你应该更新你的快捷码以接受许多要显示的帖子,而不是两次调用同一个帖子,并期望它知道有一个预期的偏移量。@BenM对我来说应该是这样的,因为页面应该是由多个类别的多个最新帖子组成的,它们应该是手工混合的,这可能是,但如果不经过第二个论证,就无法实现这一目标。例如:
[latest\u post cat=“book”count=“2”]
取代
[latest\u post cat=“books”][latest\u post cat=“cars”]
。可能值得一次调用
wp\u reset\u postdata()!