【问题标题】:Call/link wordpress category with jquery/isotope classes使用 jquery/isotope 类调用/链接 wordpress 类别
【发布时间】:2016-03-10 07:07:05
【问题描述】:

我正在处理我网站的投资组合部分,这些代码在 filter.js 脚本中(在 wp-includes/js 文件夹中)

jQuery(document).ready(function(){
    jQuery(".grid-item").first().addClass("comercial");
    jQuery(".grid-item:eq(1)").addClass("residencial");
    jQuery(".grid-item:eq(2)").addClass("cultural");
    jQuery(".grid-item:eq(3)").addClass("interiores");
    jQuery(".grid-item:eq(4)").addClass("residencial");
            jQuery(".grid-item:eq(5)").addClass("residencial");
    jQuery('.grid').isotope({
          itemSelector: '.grid-item',
          layoutMode: 'fitRows'
    });

    jQuery(".dcjq-parent-li ul li a").click(function() {
        jQuery(".dcjq-parent-li ul li a").removeClass("active");
        jQuery(this).addClass("active");
        var filtro=jQuery(this).attr("data-filter");
        jQuery(".grid").isotope({ filter: filtro});
    });

    jQuery(".dcjq-parent").click(function() {
        if(jQuery(this).siblings("ul").css("display")=="block")
            jQuery(this).siblings("ul").slideToggle( "slow" );
        else
        {
            jQuery(".accordionMenu ul").each(function( index ) {
                if(jQuery(this).css('display')=="block")
                {
                    jQuery(this).slideToggle( "slow" );
                    jQuery(this).siblings("a").removeClass("active");
                }
            });
            jQuery(this).siblings("ul").slideToggle( "slow" );
            jQuery(this).addClass("active");
        }
    });

});

我在上述代码中有不同的类,但我不确定如何在 wordpress 帖子中使用这些类,或者如何创建函数/循环以在帖子中使用这些类。

我的投资组合页面中的代码:

<div id="aside">
    <ul id="subMenu" class="accordionMenu" data-option-key="filter">                        
        <li class="dcjq-parent-li">
            <a href="#" class="dcjq-parent active">Tipologia<span class="dcjq-icon"></span>
            </a>
            <ul style="display: block;">
                <li><a href="#filter" data-filter="*" class="active">todos</a></li>
                <li><a href="#filter" data-filter=".residencial">Residencial</a></li>
                <li><a href="#filter" data-filter=".comercial">Comercial</a></li>
                <li><a href="#filter" data-filter=".cultural">Cultural</a></li>
                <li><a href="#filter" data-filter=".interiores">Interiores</a></li>
            </ul>
        </li>
        <li class="dcjq-parent-li">
            <a href="#" class="dcjq-parent">Datas<span class="dcjq-icon"></span></a>
            <ul style="display: none;">
                <li><a href="#filter" data-filter="*" class="actv">todos</a></li>
                <li><a href="#filter" data-filter=".1980, .1981, .1982, .1983, .1984">1980 - 1984</a></li><li><a href="#filter" data-filter=".1985, .1986, .1987, .1988, .1989">1985 - 1989</a></li><li><a href="#filter" data-filter=".1990, .1991, .1992, .1993, .1994">1990 - 1994</a></li><li><a href="#filter" data-filter=".1995, .1996, .1997, .1998, .1999">1995 - 1999</a></li><li><a href="#filter" data-filter=".2000, .2001, .2002, .2003, .2004">2000 - 2004</a></li><li><a href="#filter" data-filter=".2005, .2006, .2007, .2008, .2009">2005 - 2009</a></li><li><a href="#filter" data-filter=".2010, .2011, .2012, .2013, .2014">2010 - 2014</a></li>
            </ul>
        </li>                                   
    </ul>
</div>

<div class="projecto"><div class="grid">
<?php $my_query = new WP_Query('post_type=portfolio&posts_per_page=-1');
  while ($my_query->have_posts()) : $my_query->the_post(); ?>
<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID) );?>

<?php if($feat_image!=''){?>    <li class="grid-item"><a href="<?php the_permalink(); ?>"><div class="proimg"><img alt="" src="<?php echo $feat_image;?>" /></div></a>
<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                       <div class="textblock"><?php echo $text = get_post_meta( $post->ID, 'text', true ); ?>
</div>
                       </li><?php } ?>
<?php endwhile;  wp_reset_query(); ?>
</div>
</div>
<?php get_sidebar( $blog-widgets ); ?>
<?php get_footer(); ?>

我通过阅读 wordpress/isotope 集成教程完成了所有这些代码,在 jquery 中创建类,在项目页面上做代码,我不明白的是如何在 wordpress 帖子中使用这些同位素/jquery 类 因此它们将显示为过滤器。 我知道,它可以通过类别过滤、标签、元框或自定义循环来完成,但我不知道如何创建自定义循环,它使用类别中的过滤器类。

抱歉,如果我没有以正确的方式提出问题,我是初学者,并且正在尝试通过教程和专家的帮助来完成所有工作。

【问题讨论】:

  • 发布生成 HTML 元素的代码
  • 哪些代码?过滤器通过同位素脚本goo.gl/s0K0eJ工作,项目页面和过滤器代码在上面,所以我必须为哪个html元素提供代码,请解释一下,我会在这里发布代码。
  • 我贴出项目页面的完整代码,生成html。

标签: javascript php jquery wordpress jquery-isotope


【解决方案1】:

去掉自动添加你想手动添加的类的JS代码,

这些行

jQuery(".grid-item").first().addClass("comercial");
jQuery(".grid-item:eq(1)").addClass("residencial");
jQuery(".grid-item:eq(2)").addClass("cultural");
jQuery(".grid-item:eq(3)").addClass("interiores");
jQuery(".grid-item:eq(4)").addClass("residencial");
jQuery(".grid-item:eq(5)").addClass("residencial");

在您的 wordpress 循环中,获取当前项目的类别列表并将它们存储在以空格分隔的变量中,因为稍后将用作类,您可以使用 get_the_category 函数

//get the category assign to the post
//$cat_objects = get_the_category();
$cat_objects = wp_get_post_terms( get_the_ID(), 'portfolio_category');



/**Define category variable to be use as class, leave empty if you don't want to add global classes you can add 'grid-item' in here and remove it on the LI element, **/
$categories = '';

// Loop through each category object
foreach ( $cat_objects as $cat ) {

    // extract the category slug and add them to $categories variable, 
    $categories .=  ' '. $cat->slug; /** Added Space at the beginning of each category so they will be separated during actual output**/
}

然后,您可以添加存储在上述代码中的 $categories 变量中的类别列表作为网格项 li 的附加类 &lt;li class="grid-item&lt;?php echo $categories; ?&gt;"&gt;

你的循环应该是这样的

<div class="projecto"><div class="grid">
<?php $my_query = new WP_Query('post_type=portfolio&posts_per_page=-1');
    while ($my_query->have_posts()) : $my_query->the_post(); 

    $cat_objects = get_the_category();
    $categories = '';
    foreach ( $cat_objects as $cat ) {
        $categories .=  ' '. $cat->slug;
    }

    ?>
<?php $feat_image = wp_get_attachment_url(get_post_thumbnail_id($post->ID) );?>

<?php if($feat_image!=''){?>  <li class="grid-item<?php echo $categories; ?>"><a href="<?php the_permalink(); ?>"><div class="proimg"><img alt="" src="<?php echo $feat_image;?>" /></div></a>
<h5><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                       <div class="textblock"><?php echo $text = get_post_meta( $post->ID, 'text', true ); ?>
</div>
                       </li><?php } ?>
<?php endwhile;  wp_reset_query(); ?>
</div>

【讨论】:

  • 如何将类别存储在变量中,我在循环中做了以下操作: $cat_objects = get_the_category(); $categories = '商业,数据'; foreach ( $cat_objects as $cat ) { $categories .= 'comercial, datas'。 $猫->蛞蝓; }
  • 其余部分非常简单,感谢您以最简单的方式定义它。
  • 我在上面编辑了我的答案并提供了一些额外的解释get_the_category 是一个将该项目类别作为对象提取的函数,您无需在 php 代码上手动添加类别,只需分配创建帖子时的类别(或您正在使用的任何帖子类型),并且类别 slug 将自动添加为 HTML 输出上的类。你不需要修改上面的代码
  • 伙计,我完全按照上面的方法做了,但仍然无法正常工作。在后端,我为投资组合帖子类型提供了单独的类别选项。所以应该是 对吧?
猜你喜欢
  • 1970-01-01
  • 2016-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 1970-01-01
  • 2017-04-26
相关资源
最近更新 更多