添加 CSS-class 和 PHP
Add CSS-class with PHP
我正在安装 WordPress,我想将其用于单页站点。我正在使用 'query_post' 数组获取所有内容。对于每个页面部分,它都会创建一个带有 id #content 的 div。
<?php query_posts('post_type=page&order=ASC'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="content">
<div id="inner-content" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
我现在要做的是根据顺序为每个页面部分添加一个特定的 ID。像
<div id="content section-1">...</div>
<div id="content section-2">...</div>
<div id="content section-3">...</div>
等等。我怎样才能修改我的代码来实现这个?
<?php query_posts('post_type=page&order=ASC'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="content section-<?php the_ID(); ?>">
<div id="inner-content" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
这也是工作
<?php query_posts('post_type=page&order=ASC'); ?>
<div id="content section-<?php echo $i++; ?>">
<div id="inner-content" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
首先逻辑上一个元素应该只有一个id。并且由于您要重复整个 id="content",我建议您最好添加 class,并且您可以添加 id="content" 的包装器。要获取 id,您可以简单地使用 the_ID();那应该工作。所以代码应该看起来像
<div id="content">
<?php query_posts('post_type=page&order=ASC'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="inner-content<?php the_ID(); ?>" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
所以现在所有这些都将拥有唯一的 ID。 :)干杯!!!
我正在安装 WordPress,我想将其用于单页站点。我正在使用 'query_post' 数组获取所有内容。对于每个页面部分,它都会创建一个带有 id #content 的 div。
<?php query_posts('post_type=page&order=ASC'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="content">
<div id="inner-content" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
我现在要做的是根据顺序为每个页面部分添加一个特定的 ID。像
<div id="content section-1">...</div>
<div id="content section-2">...</div>
<div id="content section-3">...</div>
等等。我怎样才能修改我的代码来实现这个?
<?php query_posts('post_type=page&order=ASC'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="content section-<?php the_ID(); ?>">
<div id="inner-content" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
这也是工作
<?php query_posts('post_type=page&order=ASC'); ?>
<div id="content section-<?php echo $i++; ?>">
<div id="inner-content" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
</div>
<?php endwhile; endif; ?>
首先逻辑上一个元素应该只有一个id。并且由于您要重复整个 id="content",我建议您最好添加 class,并且您可以添加 id="content" 的包装器。要获取 id,您可以简单地使用 the_ID();那应该工作。所以代码应该看起来像
<div id="content">
<?php query_posts('post_type=page&order=ASC'); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<div id="inner-content<?php the_ID(); ?>" class="wrap cf">
<div id="main" class="m-all">
<h2><?php the_field('headline') ?></h2>
<div class="sidebar"><?php the_field('sidebar') ?></div>
<div class="main-content"><?php the_content(); ?></div>
</div>
</div>
<?php endwhile; endif; ?>
</div>
所以现在所有这些都将拥有唯一的 ID。 :)干杯!!!