Search code examples
javascriptjquerywordpressclassinsertafter

insertAfter links in loop


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. !


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:
    https://www.php.net/in_array
    http://codex.wordpress.org/Template_Tags
    http://codex.wordpress.org/Function_Reference/the_ID