Wordpress不会让我在一个标签中包装一个div

时间:2014-09-10 17:48:06

标签: php html html5 wordpress

我有一个非常奇怪的问题,我无法弄明白。我正在使用wordpress中的自定义主题,我有一个带有图像和内部另一个图标图像的div。 我试图让整个图像和图标中的图像成为一个链接。

问题在于,当我尝试在整个div周围放置一个链接时,Wordpress过早地关闭了链接,然后添加了第二个链接 - 这两个链接实际上都没有包含我的div。 如果我将div更改为span,它将让我将其包装在一个链接中。为什么?!发生了什么以及我如何关闭这个“功能”?

以下是我的模板文件中的所选代码:

        <a href="<?php the_permalink(); ?>">
            <div class="img">
                <?php if (has_post_thumbnail()): ?>
                    <img style="display: none;" src="<?php echo $image_attributes[0]; ?>" alt="<?php echo strip_tags(get_the_title()); ?>">
                    <span class="zoom hide-ie"><i class="fa fa-search"></i></span>
                <?php endif; ?>
                <?php if ($categories): ?>
                    <a href="<?php echo get_category_link($categories[0] -> term_id )?>" class="category-link"><?php echo $categories[0]->cat_name?></a>
                <?php endif; ?>
                    <div class="background" style="background-image: url('<?php echo $image_attributes[0]; ?>');"></div>
            </div>
         </a>

但是,这是输出到浏览器的代码:

  <a href="http:somelink">
    </a>
  <div class="img">
    <a href="http:somelink">
      <img style="display: none;" src="imagelink.jpg" alt="This isn’t our beautiful contest – how did we get here?">
      <span class="zoom hide-ie"><i class="fa fa-search"></i></span>
    </a>
    <a href="categorylink/" class="category-link">Agency Mojo</a>
    <div class="background" style="background-image: url('http://imagelink.jpg');"></div>
  </div>

你可以看到,它正在关闭链接,然后在`

中添加另一个链接

任何帮助都会令人惊讶,因为我已经看过这个问题但在其他一些地方没有回答,包括here on this site

1 个答案:

答案 0 :(得分:1)

我通常会尽量避免嵌套锚标签。也许这样的事情会更好。

<div class="img">
    <?php if ( has_post_thumbnail() ): ?>
        <a href="<?php the_permalink(); ?>">
            <img style="display: none;" src="<?php echo $image_attributes[0]; ?>" alt="<?php echo strip_tags(get_the_title()); ?>" />
            <span class="zoom hide-ie"><i class="fa fa-search"></i></span>
        </a>
    <?php endif; ?>
    <?php if ($categories): ?>
        <a href="<?php echo get_category_link($categories[0]-> term_id ); ?>" class="category-link">
            <?php echo $categories[0]->cat_name; ?>
        </a>
    <?php endif; ?>
    <a href="<?php the_permalink(); ?>">
        <div class="background" style="background-image: url('<?php echo $image_attributes[0]; ?>');"></div>
    </a>
</div>