Question

I need to insert anchors in an WordPress loop. I'm trying to archive this with JavaScript (Im pretty new to JavaScript) like this:

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

the result i want to archive is - https://www.dropbox.com/s/0ffcq9tu84cc8vu/Screenshot%202014-02-01%2018.36.04.png

the arrow is an icon from FontAwesome - http://fortawesome.github.io/Font-Awesome/

would be great if somebody could help me. and be gentle with me please, im very new to code. !

Était-ce utile?

La solution

Why not do it with 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: ?>

Edit to answer comment question about arrays and styling. Put the IDs you care about in the array $myIDs. This will also output a classname on each a called "link-##" where ## is the post id number.

<?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: ?>

More info:
http://us3.php.net/in_array
http://codex.wordpress.org/Template_Tags
http://codex.wordpress.org/Function_Reference/the_ID

Autres conseils

Add the anchor tag using:

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

You're probably going to want to use CSS to add the icon. Are you familiar with the after pseudo element?

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

My example will work and was provided in response to your question about doing this in JS/jQuery however it's not the best way. Adding the anchor tag should really be done in PHP.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top