我正在为wordpress创建一个菜单,但当我点击固定链接时,内容保持不变。我的第二个dropdown不能工作。问题是,在我的部分,内容需要是它没有显示出来。内容只接受最后一条消息,而不是我在菜单上选择的帖子。
https://i.gyazo.com/1698056c27baa40768659d2edab5e3d9.png这就是即使我点击另一个帖子,它也会一直这样。
<?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>发布于 2017-11-17 20:33:59
看看我下面的其他两条评论和建议的解决方案,这不是你面临的WP问题,这是JS/jQuery
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'>";和:
function myFunction(cat_slug) {
$('.btn-' + cat_slug).show();
}尝试一下,应该是可行的:)
https://stackoverflow.com/questions/47347917
复制相似问题