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 Wordpress短码无效_Php_Wordpress_Shortcode - Fatal编程技术网

Php Wordpress短码无效

Php Wordpress短码无效,php,wordpress,shortcode,Php,Wordpress,Shortcode,我为wordpress创建了这个短代码,但没有效果 <?php function theme_tfw_posts() { ?> <?php global $post; $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 ); $myposts = get_posts( $args ); foreach( $myposts as $post ) :

我为wordpress创建了这个短代码,但没有效果

<?php
function theme_tfw_posts()
{
?>
<?php
    global $post;
    $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :
        setup_postdata($post);
?>
        $a=<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>;
    <?php endforeach; ?>
<?php
    return $a;
}
?>
<?php
add_shortcode('tfw_posts','theme_tfw_posts');
?>

$a=;

我认为问题在于标签或其他东西,但这是我的第一个短码,至少有一件事情看起来是错误的,
$a
在返回时不会被定义。原因是您的行
$a=';
}
返回$a;
}
添加_短码('tfw_posts','theme_tfw_posts');
?>

更详细的信息会让您更快地得到更好的答案。你说它不起作用是什么意思?到底发生了什么?它与您预期的情况有何不同?
<?php
function theme_tfw_posts()
{
    $args = array( 'numberposts' => 5, 'offset'=> 1, 'category' => 1 );
    $myposts = get_posts( $args );
    $a = '';
    foreach( $myposts as $post )
    {
        setup_postdata($post);
        $a .= '<a href="' . the_permalink() . '">' . the_title() . '</a>';
    }
    return $a;
}
add_shortcode('tfw_posts','theme_tfw_posts');
?>