0

我需要在 WordPress 循环中插入锚点。我正在尝试使用 JavaScript(我对 JavaScript 很陌生)将其存档,如下所示:

document.getElementByClass("post-40").insertAfter("<a href='#anker1'>\f107</a>");

我要存档的结果是 - https://www.dropbox.com/s/0ffcq9tu84cc8vu/Screenshot%202014-02-01%2018.36.04.png

箭头是来自 FontAwesome 的图标 - http://fortawesome.github.io/Font-Awesome/

如果有人可以帮助我,那就太好了。请对我温柔一点,我对代码很陌生。!

4

2 回答 2

0

使用以下命令添加锚标记:

jQuery(document).ready(function($) {
    $('.post-40').after('<a href="#anker1"></a>');
});

您可能会想要使用 CSS 来添加图标。你熟悉 after 伪元素吗?

[selector]:after {
    content: '\f107';
    more styling here...
}

我的示例将起作用,并且是为了回答您关于在 JS/jQuery 中执行此操作的问题而提供的,但这不是最好的方法。添加锚标记确实应该在 PHP 中完成。

于 2014-02-01T17:59:05.000 回答
0

为什么不用 WordPress/PHP 来做呢?

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="entry"> <!-- or whatever your loop code is... -->
       <?php the_content(); ?>
    </div>
    <?php if ( $post->ID == "40" ) : ?>
        <a href='#anker1'>\f107</a>
    <?php endif; ?>

<?php endwhile; else: ?>

编辑以回答有关数组和样式的评论问题。将您关心的 ID 放入数组中$myIDs。这还将在每个名为“link-##”的名称上输出一个类名,其中## 是帖子 ID 号。

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <div class="entry"> <!-- or whatever your loop code is... -->
       <?php the_content(); ?>
    </div>
    <?php
        $myIDs = array(40, 42, 147, 256); 
        if ( in_array($post->ID, $myIDs) ) : ?>
        <a href='#anker1' class="button-link link-<?php the_ID() ?>">\f107</a>
    <?php endif; ?>

<?php endwhile; else: ?>

更多信息:
http ://us3.php.net/in_array
http://codex.wordpress.org/Template_Tags
http://codex.wordpress.org/Function_Reference/the_ID

于 2014-02-01T18:08:27.320 回答