I create this shortcode for wordpress but no works
<?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');
?>
I think the problem it´s with the tags or something but it´s my first shortcode , regards
At least one thing that appears to be wrong is $a
will not be defined when you return it. The reason is that your line $a=<a href...
is not inside a PHP codeblock.
Also $a
is overwritten each time your foreach
loop executes. Maybe you want to append each link to the previous instead?
This might work better (although I'm not completely sure what you're trying to do, so it might not):
<?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');
?>