我正在创建一个wordpress主题,其中有一个部分“HUMOUR“。在这一节中,我创建了accordians, 第一个手风琴演奏者class="active" 剩下的就不要了。
例如
<dl>
<dt class="active">
<h1 class="link_heading">
What are Benifits ?
</h1>
<div>
<img src="images/arrowdown.png" class="arrow" id="down">
<img src="images/arrowup.png" class="arrow" id="up" style="display: none;">
</div>
</dt>
<dd style="display: block;">
<p>lorem ipsumdolor sitamet consectetur adipisicing elitseddo eiusmod tempor incididunt lorem ipsum dolor sitamet consectetur adipisicing elit seddo eiusmod
</p>
</dd>
<dt class="">
<h1 class="link_heading">
CAN I OPT OUT ?
</h1>
<div>
<img src="images/arrowdown.png" class="arrow" id="down" style="display: none;">
<img src="images/arrowup.png" class="arrow" id="up" style="display: block;">
</div>
</dt>
<dd style="display: none;">
<p>lorem ipsumdolor sitamet consectetur adipisicing elitseddo eiusmod tempor incididunt lorem ipsum dolor sitamet consectetur adipisicing elit seddo eiusmod</p>
</dd>
<dt class="">
<h1 class="link_heading">
IS MY CAPITAL SAFE ?
</h1>
<div>
<img src="images/arrowdown.png" class="arrow" id="down" style="display: none;">
<img src="images/arrowup.png" class="arrow" id="up" style="display: block;">
</div>
</dt>
<dd style="display: none;">
<p>lorem ipsumdolor sitamet consectetur adipisicing elitseddo eiusmod tempor incididunt lorem ipsum dolor sitamet consectetur adipisicing elit seddo eiusmod</p>
</dd>
<dt class="">
<h1 class="link_heading">
WHAT IS THE RISK ?
</h1>
<div>
<img src="images/arrowdown.png" class="arrow" id="down" style="display: none;">
<img src="images/arrowup.png" class="arrow" id="up" style="display: block;">
</div>
</dt>
<dd style="display: none;">
<p>lorem ipsumdolor sitamet consectetur adipisicing elitseddo eiusmod tempor incididunt lorem ipsum dolor sitamet consectetur adipisicing elit seddo eiusmod</p>
</dd>
</dl>
这是必需的
Accordian 在wordpress中创建相同内容时,不会在中显示第一篇文章class="active". 而且,它没有按预期工作。
i、 e类
<dl>
<dt id="post-224">
<h1 class="link_heading">WHAT IS THE RISK ?</h1>
<div>
<img src="http://localhost/wp/wp-content/themes/cultcapital-wp/images/arrowdown.png" class="arrow" id="down">
<img src="http://localhost/wp/wp-content/themes/cultcapital-wp/images/arrowup.png" class="arrow" id="up">
</div>
</dt>
<dd style="display: block;">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam facilisis nisi in ligula semper rutrum. Aliquam placerat sapien dolor, non tristique massa ultrices sit amet. </p>
</dd>
<dt id="post-222">
<h1 class="link_heading">IS MY CAPITAL SAFE ?</h1>
<div>
<img src="http://localhost/wp/wp-content/themes/cultcapital-wp/images/arrowdown.png" class="arrow" id="down">
<img src="http://localhost/wp/wp-content/themes/cultcapital-wp/images/arrowup.png" class="arrow" id="up">
</div>
</dt>
<dd style="display: none;">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam facilisis nisi in ligula semper rutrum. Aliquam placerat sapien dolor, non tristique massa ultrices sit amet. </p>
</dd>
<dt id="post-220">
<h1 class="link_heading">CAN I OPT OUT ?</h1>
<div>
<img src="http://localhost/wp/wp-content/themes/cultcapital-wp/images/arrowdown.png" class="arrow" id="down">
<img src="http://localhost/wp/wp-content/themes/cultcapital-wp/images/arrowup.png" class="arrow" id="up">
</div>
</dt>
<dd style="display: none;">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam facilisis nisi in ligula semper rutrum. Aliquam placerat sapien dolor, non tristique massa ultrices sit amet. </p>
</dd>
</dl>
这是我的预览
我使用自定义的帖子类型来显示手风琴,其中有标题和摘录。这是我的密码。
<div class="humour">
<h1 class="section_heading"> HUMOUR ON INDIAN INVESTMENT </h1>
<dl>
<?php
$args = array(
\'post_type\' => \'humour\'
);
$humour_query = new WP_Query($args);
if ($humour_query->have_posts()) : while($humour_query->have_posts()) : $humour_query->the_post();
?>
<dt id="post-<?php the_ID(); ?>">
<h1 class="link_heading"><?php the_title(); ?></h1>
<div>
<img src="<?php print IMAGES; ?>/arrowdown.png" class="arrow" id="down"/>
<img src="<?php print IMAGES; ?>/arrowup.png" class="arrow" id="up"/>
</div>
</dt>
<dd>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25);
?>
</p>
</dd>
<?php endwhile; endif; ?>
</dl>
</div>
我想当用户发布任何帖子时,它会检查是否是第一篇帖子,如果是第一篇帖子,即当前的最新帖子,它会向其中添加class=“active”,并保持其余帖子不变。我不知道该怎么做才能完成这项任务。任何人都可以帮忙。。
最合适的回答,由SO网友:Rohil_PHPBeginner 整理而成
根据你的描述,我得到的是你想要add class active
到last published post by the user.
通过查看output
和HTML
, 看来active
缺少类。
为了实现这一点,我们将添加一个将打印的虚拟条件class="active"
. 我还建议修改您的$arg
然后进入DESC
以先获取上次发布的帖子。
最终代码如下:
<div class="humour">
<h1 class="section_heading"> HUMOUR ON INDIAN INVESTMENT </h1>
<dl>
<?php
$args = array(
\'post_type\' => \'humour\',
\'order\' => \'DESC\' //Add order to get post in DESC order
);
$humour_query = new WP_Query($args);
$i=0; //Take variable and assign it 0
if ($humour_query->have_posts()) : while($humour_query->have_posts()) : $humour_query->the_post();
?>
<?php
if($i==0): //This condition will be true only once as I have added increament at the end
echo "<dt id=\'post-".the_ID()."\' class=\'active\'>";
else:
?>
<dt id="post-<?php the_ID(); ?>">
endif;
<h1 class="link_heading"><?php the_title(); ?></h1>
<div>
<img src="<?php print IMAGES; ?>/arrowdown.png" class="arrow" id="down"/>
<img src="<?php print IMAGES; ?>/arrowup.png" class="arrow" id="up"/>
</div>
</dt>
<dd>
<p>
<?php
$excerpt = get_the_excerpt();
echo string_limit_words($excerpt,25);
?>
</p>
</dd>
<?php
$i++; //Incremented that variable so class wont be added from second post
endwhile;
endif; ?>
</dl>
</div>
NOTE: 我认为手风琴是与课堂合作的
active
.