dpf7891 2013-03-30 21:26
浏览 60

Wordpress短代码无效

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

  • 写回答

1条回答 默认 最新

  • doulin2555 2013-03-30 21:42
    关注

    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');
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)