dongshanji3102 2015-11-09 15:42
浏览 53
已采纳

wordpress post excerpt in bootstrap popover

I'm trying to dynamically fill out a bootstrap popover using wordpress post exerpts. I feel that I am pretty close to getting this working similar to this fiddle: http://jsfiddle.net/dalecameron/QZj5P/11/

The code I have so far is as follows:

function powerloop_excerpt( $atts ) {
   global $post;

   $defaults = array(
     'class' => '',
    'title' => 'Popover Title',
    'content' => 'Content'
   );
   $atts = shortcode_atts( $defaults, $atts );
    ob_start();

            ?>
            <script>
                    jQuery(function(){
                         jQuery("a[rel=popover]")
                        .popover({
                            html:true,
                            trigger: 'hover',
                            content: function () {
                            return $out;
                            },
                            title: ''
                        })
                        .click(function(e) {
                            e.preventDefault()
                        });
                    });
            </script><?php
   $excerpt = get_the_excerpt();

   //post format
    $media = pagelines_media( array( 'thumb-size' => 'full' ) );
    $powerloop_quote = has_post_format( 'quote' );
    $powerloop_link = has_post_format( 'link' );

    if(!$powerloop_quote && !$powerloop_link):
      $out = sprintf('<p class="%s power-excerpt">%s</p>',
      $atts['class'],
      $excerpt
      );
    else:
      $out = '';
    endif;
    $expop = sprintf('<a href="#" rel="popover" data-placement="right"><i class="fa fa-info-circle"></i></a>');
    return $expop;
}
  • 写回答

2条回答 默认 最新

  • douzhi2012 2015-11-09 16:20
    关注

    You're mixing PHP and JS variables here. This:

    content: function () {
      return $out;
    },
    

    Can't work as $out is a PHP variable, and it is defined after you're trying to print it.

    An other issue you have is that you use ob_start() to capture the output (which is a good idea) but never return it content. You return instead the popup link, so the JS part is being skipped.

    I suggest to change your function to this:

    function powerloop_excerpt( $atts ) {
        global $post;
    
        $defaults = array(
            'class' => '',
            'title' => 'Popover Title',
            'content' => 'Content'
        );
        $atts = shortcode_atts( $defaults, $atts );
    
        //post format
        $media = pagelines_media( array( 'thumb-size' => 'full' ) );
        $powerloop_quote = has_post_format( 'quote' );
        $powerloop_link = has_post_format( 'link' );
        $excerpt = get_the_excerpt();
        $title = get_the_title();
        if(!$powerloop_quote && !$powerloop_link) {
            $content = sprintf('<p class="%s power-excerpt">%s</p>',
                $atts['class'],
                $excerpt
            );
        } else {
            $content = '';
        }
    
        ob_start();
        ?>
        <script type="text/javascript">
            jQuery(function(){
                jQuery("a[rel=popover]")
                    .popover({
                        html:true,
                        trigger: 'hover',
                        title: '<?php echo $title; ?>',
                        content: '<?php echo $content; ?>'
                    })
                    .click(function(e) {
                        e.preventDefault()
                    });
            });
        </script>
        <?php
        $out = ob_get_clean();
    
        $out  .= '<a href="#" rel="popover" data-placement="right"><i class="fa fa-info-circle"></i></a>';
        return $out;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教