douwen1929 2014-11-18 23:25
浏览 42
已采纳

Javascript元素不起作用

I am working on a webpage on Wordpress and I wanted to insert a slider with the posts from 6 blogs. The posts would be chosen by me, for that I am using Advanced Custom Fields plugin for that. The php part works properly, but I don't get the js working. Here is my html code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="jquery.flexslider.js"></script>
<script>
$(window).load(function() {
    $(".flexslider").flexslider({
        animation: "slide",
    });
 });
</script>

Here is the html for the slider:

<div id="grey" class="row main-post">
<div class="flexslider">
  <ul class="slides">
    <?php
    echo get_chosen_post();
    echo get_chosen_post_1();
    echo get_chosen_post_2();
    ?>
  </ul>
  </div>
</div>

And one of the functions for retrieving the posts from the fields:

  function get_chosen_post() {
    global $wpdb;
    $post_title = get_field('post_title');
    $blog = get_field('blog_number');
    switch_to_blog( $blog );
    $posts = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}posts WHERE post_type='post' AND post_status='publish' AND post_title LIKE '$post_title' LIMIT 1");
    foreach($posts as $post) {
        //Thumbnail
            $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
            $url = $thumb['0'];
        $html .= '<div class="col-xs-12 col-sm-6 col-lg-4 mainpost "><div class="main-center"><a class="img-wrapper" href="'.get_permalink( $post->ID ).'"><img class="main-post-img" src="'.$url.'"></img></a></div></div>';

        $html .= '<div class="col-xs-12 col-sm-6 col-lg-4 mainpost no-padding"><div class="main-center mobile-text-fix">';
        //Title
        $html .= '<h4><a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'">'.$post->post_title.'</a></h4>';
        //Excerpt
        $html .= '<p>'.$post->post_excerpt.'</p>';
        $html .= '</div></div>';

    }
switch_to_blog(1);
return $html;
}

The query works fine, I use the LIKE statement to avoid errors if the post title contains quotes. My slider doesn't work, it just shows all the posts that I chose and the links to them, but no slider. I would appreciate your help, thank you!

  • 写回答

1条回答 默认 最新

  • dongpengyu1363 2014-11-18 23:56
    关注

    I think slider not works because in html result there isn't LI element, but only DIV. You can try

      <ul class="slides">
        <li>  <?php   echo get_chosen_post(); ?></li>
        <li> <?php   echo get_chosen_post_1(); ?></li>
        <li> <?php echo get_chosen_post_2(); ?></li>
      </ul>
    

    or

    foreach($posts as $post) {
        $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large' );
        $url = $thumb['0'];
        $html .= '<li>';
        $html .= '<div class="col-xs-12 col-sm-6 col-lg-4 mainpost "><div class="main-center"><a class="img-wrapper" href="'.get_permalink( $post->ID ).'"><img class="main-post-img" src="'.$url.'"></img></a></div></div>';
    
        $html .= '<div class="col-xs-12 col-sm-6 col-lg-4 mainpost no-padding"><div class="main-center mobile-text-fix">';
        //Title
        $html .= '<h4><a href="'.get_permalink($post->ID).'" title="'.$post->post_title.'">'.$post->post_title.'</a></h4>';
        //Excerpt
        $html .= '<p>'.$post->post_excerpt.'</p>';
        $html .= '</div></div>';
        $html .= '</li>';
    }
    

    i hope this can help you

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据