Php Wordpress短码无效
我为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 ) :
<?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');
?>