首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Wordpress链接和内容

Wordpress链接和内容
EN

Stack Overflow用户
提问于 2017-11-17 18:00:46
回答 1查看 41关注 0票数 1

我正在为wordpress创建一个菜单,但当我点击固定链接时,内容保持不变。我的第二个dropdown不能工作。问题是,在我的部分,内容需要是它没有显示出来。内容只接受最后一条消息,而不是我在菜单上选择的帖子。

https://i.gyazo.com/1698056c27baa40768659d2edab5e3d9.png这就是即使我点击另一个帖子,它也会一直这样。

代码语言:javascript
复制
  <?php
  get_header();
  ?>
  <div class="menu">

    <div>
        <a href="<?php echo home_url(); ?>"><h1>Documentatie</h1></a>
    </div>


      <?php $cats = get_categories();

      foreach ($cats as $cat) {
        $cat_id= $cat->term_id;
        echo "<div class='dropdown'>";
        echo "<button onclick='myFunction()' class='dropbtn'>".$cat->name."</button>";
        echo "<div id='myDropdown' class='dropdown-content'>";

        query_posts("cat=$cat_id&post_per_page=100");

    if (have_posts()) : while (have_posts()) : the_post(); ?>

     <button class="dropbutton"><h2><a href="<?php the_permalink();?>"><?php the_title(); ?></a></h2></button>

  <?php endwhile;
?>
</div>
</div>
<?php
   else :
    echo "<p>Geen content gevonden</p>";
  endif;
   wp_reset_postdata();
 }
?>
</div>
  <div class="content">
   <h2><?php the_title(); ?></h2>
   <div class="wpcontent"><?php the_content(); ?> </div>
 </div>
<?php wp_footer(); ?>
<script>
/* When the user clicks on the button,
toggle between hiding and showing the dropdown content */
function myFunction() {
    document.getElementById("myDropdown").classList.toggle("show");
}

// Close the dropdown if the user clicks outside of it
window.onclick = function(event) {
  if (!event.target.matches('.dropbtn')) {

    var dropdowns = document.getElementsByClassName("dropdown-content");
    var i;
    for (i = 0; i < dropdowns.length; i++) {
      var openDropdown = dropdowns[i];
      if (openDropdown.classList.contains('show')) {
        openDropdown.classList.remove('show');
      }
    }
  }
}
</script>
EN

回答 1

Stack Overflow用户

发布于 2017-11-17 20:33:59

看看我下面的其他两条评论和建议的解决方案,这不是你面临的WP问题,这是JS/jQuery

代码语言:javascript
复制
    echo "<div class='dropdown'>";
    echo "<button onclick='myFunction(" . $cat->slug . ")' class='dropbtn btn-" . $cat->slug . "'>".$cat->name."</button>";
    echo "<div id='myDropdown' class='dropdown-content'>";

和:

代码语言:javascript
复制
function myFunction(cat_slug) {
   $('.btn-' + cat_slug).show();
}

尝试一下,应该是可行的:)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47347917

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档