将递增的 class 与来自不同循环 [jQuery / Wordpress] 的递增 ID 匹配
Match incremented class with incremented ids from different loops [jQuery / Wordpress]
经过数小时的研究、尝试和错误,我从这里开始我的第一个问题。
如果能用wordpress语法来完成就更好了。否则我想我已经接近解决方案了。
我想在 wordpress 模板页面上创建一个自定义图库,但带有来自某些类别的 post 图片以及其中的内容。 到目前为止一切顺利。
在我的 designerin.php 模板中,我给每张图片一个 class img-x.
class="img-<?php echo $img_counter; ?>
并且内容获得 id img-x
id="img-<?php echo $post_counter; ?>
计数器只是一个增量。所以图像 1 有 class img-1,图像 2 img-2 等等。
现在我希望在您单击此特定图像时显示在 wordpress 编辑器中创建的该图像的相关内容。
假设您点击图片 5 并想要图片 5 中的内容。
我卡在了显示内容的步骤(在 jQuery 中)
if .img-x == #img-x {
display the_content()
}
说得够多了 - 这是我的 jQuery 代码:
$(".large-img-wrapper").hide();
$('.gallery li').on('click', function(e) {
e.preventDefault();
var largeImgLink = $(this).find('a').attr('href');
$(".large-img-wrapper").first().fadeIn();
var imgContent = $('#img-5').html();
$('#large-img').remove();
$('#append-id').remove();
$(".large-img").append('<img id="large-img" src="' + largeImgLink + '">').fadeIn();
$(".large-img").append('<div id="append-id">' + imgContent + '</div>').fadeIn();
$(".large-img-container .img-title").remove();
});
var imgContent 只是一个测试。此时每张图片都显示了图片 5 的内容。
我的模板页面代码:
<div class="row content full-width">
<div class="cats row">
<a href="#" class="sort-link" data-category="Alle Kategorien">Alle Kategorien</a>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= $category->cat_name;
}
}
$cat_name = get_category(get_query_var('cat'))->name;
?>
<a href="#" class="sort-link" data-category="<?php echo trim($output); ?>"><?php echo trim($output); ?></a>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<ul class="row small-block-grid-3 medium-block-grid-5 large-block-grid-8 gallery">
<?php $img_counter = 0; ?>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$img_counter++;
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$large_url_array = wp_get_attachment_image_src($thumb_id, 'large', true);
$thumb_url = $thumb_url_array[0];
$large_url = $large_url_array[0];
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= $category->cat_name;
}
}
?>
<li>
**<a href="<?php echo $large_url; ?>" class="thumbnail img-<?php echo $img_counter; ?>" data-categories="<?php echo trim($output); ?>"><img src="<?php echo $thumb_url; ?>" alt="<?php echo the_title(); ?>"></a>**
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
<?php $post_counter = 0; ?>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$post_counter++;
?>
**<ul class="large-img-wrapper">
<li>
<div class="large-img-container">
<div class="large-img"></div>
<div class="img-content" id="img-<?php echo $post_counter; ?>">
<div class="img-title">
<?php echo the_title(); ?>
</div>
<div class="img-text">
<?php echo the_content(); ?>
</div>
</div>
</div>
</li>
</ul>**
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
第一个查询用于按 slug 过滤类别。
我最后一次尝试感觉很接近..
var inc = '5';
var objClass = $(this).find("a").hasClass( "img-" + inc );
console.log(objClass);
当我点击第 5 张图片时,我得到 "true"。但是我怎样才能得到与我的模板 php 代码相关的 X (inc)?
对不起我的英语和可能的错误代码
如果我理解正确,这可能会有所帮助
$('.gallery li').on('click', function(e) {
e.preventDefault();
var className = $(this).find('a').attr('class');
var imgContent = $('#'+className).html();
});
经过数小时的研究、尝试和错误,我从这里开始我的第一个问题。 如果能用wordpress语法来完成就更好了。否则我想我已经接近解决方案了。
我想在 wordpress 模板页面上创建一个自定义图库,但带有来自某些类别的 post 图片以及其中的内容。 到目前为止一切顺利。
在我的 designerin.php 模板中,我给每张图片一个 class img-x.
class="img-<?php echo $img_counter; ?>
并且内容获得 id img-x
id="img-<?php echo $post_counter; ?>
计数器只是一个增量。所以图像 1 有 class img-1,图像 2 img-2 等等。 现在我希望在您单击此特定图像时显示在 wordpress 编辑器中创建的该图像的相关内容。
假设您点击图片 5 并想要图片 5 中的内容。 我卡在了显示内容的步骤(在 jQuery 中)
if .img-x == #img-x {
display the_content()
}
说得够多了 - 这是我的 jQuery 代码:
$(".large-img-wrapper").hide();
$('.gallery li').on('click', function(e) {
e.preventDefault();
var largeImgLink = $(this).find('a').attr('href');
$(".large-img-wrapper").first().fadeIn();
var imgContent = $('#img-5').html();
$('#large-img').remove();
$('#append-id').remove();
$(".large-img").append('<img id="large-img" src="' + largeImgLink + '">').fadeIn();
$(".large-img").append('<div id="append-id">' + imgContent + '</div>').fadeIn();
$(".large-img-container .img-title").remove();
});
var imgContent 只是一个测试。此时每张图片都显示了图片 5 的内容。 我的模板页面代码:
<div class="row content full-width">
<div class="cats row">
<a href="#" class="sort-link" data-category="Alle Kategorien">Alle Kategorien</a>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= $category->cat_name;
}
}
$cat_name = get_category(get_query_var('cat'))->name;
?>
<a href="#" class="sort-link" data-category="<?php echo trim($output); ?>"><?php echo trim($output); ?></a>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
<ul class="row small-block-grid-3 medium-block-grid-5 large-block-grid-8 gallery">
<?php $img_counter = 0; ?>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$img_counter++;
$thumb_id = get_post_thumbnail_id();
$thumb_url_array = wp_get_attachment_image_src($thumb_id, 'thumbnail', true);
$large_url_array = wp_get_attachment_image_src($thumb_id, 'large', true);
$thumb_url = $thumb_url_array[0];
$large_url = $large_url_array[0];
$categories = get_the_category();
$separator = ' ';
$output = '';
if($categories){
foreach($categories as $category) {
$output .= $category->cat_name;
}
}
?>
<li>
**<a href="<?php echo $large_url; ?>" class="thumbnail img-<?php echo $img_counter; ?>" data-categories="<?php echo trim($output); ?>"><img src="<?php echo $thumb_url; ?>" alt="<?php echo the_title(); ?>"></a>**
</li>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</ul>
<?php $post_counter = 0; ?>
<?php query_posts( 'category_name=designerin&posts_per_page=-1'); if (have_posts()) : while (have_posts()) : the_post();
$post_counter++;
?>
**<ul class="large-img-wrapper">
<li>
<div class="large-img-container">
<div class="large-img"></div>
<div class="img-content" id="img-<?php echo $post_counter; ?>">
<div class="img-title">
<?php echo the_title(); ?>
</div>
<div class="img-text">
<?php echo the_content(); ?>
</div>
</div>
</div>
</li>
</ul>**
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
</div>
第一个查询用于按 slug 过滤类别。
我最后一次尝试感觉很接近..
var inc = '5';
var objClass = $(this).find("a").hasClass( "img-" + inc );
console.log(objClass);
当我点击第 5 张图片时,我得到 "true"。但是我怎样才能得到与我的模板 php 代码相关的 X (inc)?
对不起我的英语和可能的错误代码
如果我理解正确,这可能会有所帮助
$('.gallery li').on('click', function(e) {
e.preventDefault();
var className = $(this).find('a').attr('class');
var imgContent = $('#'+className).html();
});