Dynamically generating an '<a name="">' reference with PHP
<p>I'm trying to dynamically generate a <code>&lt;a name=""&gt;</code> link reference inside of a Wordpress plugin.  The plugin will output this link reference inside of the posts it generates.  The categories of posts that this plugin generates will always be changing, so when we link to those posts from a separate page, we still need to be able to identify each one individually somehow.  My best guess was to just reference them by a single number (i.e. 1, 2, 3, 4, etc).  </p>

<p>The code is below.....please read the bottom of this post before examining the code.</p>

<pre><code>&lt;?php
/*
Plugin Name: Posts for Page Plugin
Plugin URI: http://www.mywebdeveloperblog.com/development/wordpress/posts-for-page-
wordpress-plugin
Description: This plugin allows for posts to be assigned to pages as snippets or
full posts. Posts can be selected by category slug, category id, tag slug, single
post id, author and ordered by date or title.
Version: 1.5
Author: Simon Hibbard
Author URI: http://www.mywebdeveloperblog.com/
License: GPL2
*/
?&gt;
&lt;?php

//tell wordpress to register the children-excerpt shortcode
add_shortcode("posts-for-page", "sc_posts_for_page");

function remove_images( $content ) {
$postOutput = preg_replace('/&lt;img[^&gt;]+./','', $content);
return $postOutput;
}

//children excerpt shortcode worker function
function sc_posts_for_page($atts, $content = null){

if($atts['length'] &gt; 0 ){
}else{
    $atts['length'] = 50;
}
//echo $atts['read_more'];
if($atts['read_more'] != "" ){
    $_opts['readMoreText'] = $atts['read_more'];
}else{
    $_opts['readMoreText'] = "Read More &amp;raquo;";
}

if($atts['prev_text'] != "" ){
    $_opts['prevText'] = $atts['prev_text'];
}else{
    $_opts['prevText'] = "&amp;laquo; Newer Entries";
}

if($atts['next_text'] != "" ){
    $_opts['nextText'] = $atts['next_text'];
}else{
    $_opts['nextText'] = "Older Entries &amp;raquo; ";
}

// set values from the shortcode
$_opts['cat_slug'] = $atts['cat_slug'];
$_opts['cat'] = $atts['cat'];
$_opts['tag_slug'] = $atts['tag_slug'];
$_opts['order_by'] = $atts['order_by'];
$_opts['post_id'] = $atts['post_id'];
$_opts['author'] = $atts['author'];
$_opts['num'] = $atts['num'];

$_opts['showFullPost'] = $atts['show_full_posts'];

// handle paging if limit on posts set
//if($_opts['num'] != "")
//{
//  $thispage = get_permalink();
//  $_opts['page_num'] = get_query_var('page'); 
//  if($_opts['page_num'] != "")
//  {
//      $prev = $_opts['page_num'] - 1;
//      $next = $_opts['page_num'] + 1;
//      if($prev == 0)
//      {
//          $prevLink = "&lt;a href='" . $thispage . "' 
class='pfpPrev'&gt;Prev&lt;/a&gt;";
//      }
//      else
//      {
//          $prevLink = "&lt;a href='" . $thispage . "/?page=" .
$prev . "' class='pfpPrev'&gt;Prev&lt;/a&gt;";
//      }
//      $nextLink = "&lt;a href='" . $thispage . "/?page=" . $next . "'
class='pfpPrev'&gt;Next&lt;/a&gt;";
//  }
//  else
//  {
//      $prevLink = "";
//      $nextLink = "&lt;a href='" . $thispage . "?page=1' 
class='pfpPrev'&gt;Next&lt;/a&gt;";
//  }
//}
global $paged;
if (empty($paged)) {
        $_opts['cur_page'] = 1;
}
else
{
    $_opts['cur_page'] = $paged;
}

// these do not currently work - wp needs the image already saved to a size we just use wp thumbnails for now
//$_opts['imageH'] = $atts['thumb_h'];
//$_opts['imageW'] = $atts['thumb_w'];
$_opts['hide_images'] = $atts['hide_images'];

//get the id of the current article that is calling the shortcode
$parent_id = get_the_ID();

$output = "";

$i = 0;

$children  = pfp_get_posts($_opts);

if (is_array($children))
{
    if(count($children) &lt;= 0){
        $out = "&lt;strong&gt;Items for Page:&lt;/strong&gt; There are no posts that match the selection criteria.";
        return $out;
    }

    foreach( $children as $child ) {
        $title = $child-&gt;post_title;
        $link = get_permalink($child-&gt;ID);

        if($_opts['showFullPost'] == "true")
        {
           $output .= "&lt;div class='pfpItem entry-content'&gt;";
           $output .= "&lt;h2 class='entry-title'&gt;&lt;a href='$link'&gt;$title&lt;/a&gt;&lt;/h2&gt;";
            $output .= $child-&gt;post_content;
            $output .= "&lt;div class='clear'&gt;&lt;/div&gt;";
           //$output .= "&lt;hr&gt;";             
            $output .= "&lt;/div&gt;";    
        }
        else
        {

            // remove all images from the post
            $child-&gt;post_content = remove_images($child-&gt;post_content);

            $args = array(
            'numberposts' =&gt; 1,
            'order'=&gt; 'ASC',
            'post_mime_type' =&gt; 'image',
            'post_parent' =&gt; $child-&gt;ID ,
            'post_status' =&gt; null,
            'post_type' =&gt; 'attachment'
            );

            if($_opts['hide_images'] == 'true')
            {
                $imageSrc =  '';
            }
            else
            {
                //get the first image and resize it 
                $attachments = get_children( $args );
                //echo "here" .$_opts['imageH'];
                if ($attachments) {
                    foreach($attachments as $attachment) {

                        //if(($_opts['imageH'] &gt; 0) &amp;&amp; ($_opts['imageW'] &gt; 0)){
                        //  // get size based on params
                        //  $size =  array($_opts['imageW'], $_opts['imageH']);
                        //  echo "Here" . $size[0];
                        //  $imageSrc = '&lt;img src="'.wp_get_attachment_image_src( $attachment-&gt;ID, $size).'" /&gt;';
                        //}else{

                            $image_attributes = wp_get_attachment_image_src( $attachment-&gt;ID, 'thumbnail' )  ? wp_get_attachment_image_src( $attachment-&gt;ID, 'thumbnail' ) : wp_get_attachment_image_src( $attachment-&gt;ID, 'full' );
                            $imageSrc = '&lt;img src="'.wp_get_attachment_thumb_url( $attachment-&gt;ID ).'" /&gt;';

                        //}

                    }
                }   
                else
                {
                    $imageSrc =  '';
                }
            }

            //split excerpt into array for processing
            $words = explode(' ', strip_tags($child-&gt;post_content));

            //chop off the excerpt based on the atts-&gt;lenth
            $words = array_slice($words, 0, $atts['length']);

            //merge the array of words for the excerpt back into sentances
            $child_excerpt = $imageSrc . implode(' ', $words);












             $output .= "&lt;div class='box'&gt;";
                             $output .= "&lt;div class='border-top'&gt;";
                             $output .= "&lt;div class='border-bottom'&gt;";
                             $output .= "&lt;div class='corner-top-right'&gt;";
                             $output .= "&lt;div class='corner-top-left'&gt;";
                             $output .= "&lt;div class='corner-bottom-left'&gt;";
                             $output .= "&lt;div class='corner-bottom-right'&gt;"; 
                             $output .= "&lt;div class='indent-box' style='margin-top:-10px'&gt;";
                             $output .= "&lt;div class='container'&gt;";                                  

           //original plugin display code starts here
            $output .= "&lt;div class='pfpItem entry-summary'&gt;";
           $output .= "&lt;h2 class='entry-title'&gt;&lt;a name='&lt;?php echo $i++; ?&gt;' style='color:#4a79a4; font-family:comic; font-size:1em' href='$link'&gt;$title&lt;/a&gt;&lt;/h2&gt;";
            $output .= $child_excerpt ."...";
            $output .= "&lt;a href='$link' class='pfpReadMore'&gt;" . $_opts['readMoreText'] . "&lt;/a&gt;";
            $output .= "&lt;div class='clear'&gt;&lt;/div&gt;";
           //$output .= "&lt;hr&gt;";
            $output .= "&lt;/div&gt;";
           //original plugin display code


            $output .= "&lt;br/&gt;&lt;br/&gt;";
            $output .= "&lt;img class='centerad' src='/wp-content/themes/twentyten/images/affiliate/footer-banner4.png' width='540' height='100'/&gt;";
                            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
            $output .= "&lt;/div&gt;";
                $output .= "&lt;br/&gt;&lt;br/&gt;";
        }           
    }
}

// output links
//if($_opts['num'] != "")
//{
//echo $wp_query-&gt;found_posts;
//  $output .= $prevLink;
//  if($wp_query-&gt;found_posts &gt; ($_opts['num'] * $_opts['page_num']))
//  {
//      $output .= $nextLink;
//  }
//}     
global $wp_query;
$page_links_total =  $wp_query-&gt;max_num_pages;

//echo "currentpage:" . $_opts['cur_page'];
//echo "totalpages:" . $page_links_total;

if($_opts['cur_page'] &gt; 1)
{
    // show prev
    //previous_posts_link($_opts['prevText']);
    $output .= "&lt;span class='pfpNav'&gt;" . get_previous_posts_link($_opts['prevText']) . "&lt;/span&gt;";
}
if($_opts['cur_page'] &lt;  $page_links_total)
{
    // show next
    //next_posts_link($_opts['nextText']);
    $output .= "&lt;span class='pfpNav'&gt;" . get_next_posts_link($_opts['nextText']) . "&lt;/span&gt;";
}


wp_reset_query();
return $output;

}

function pfp_get_posts($pfp_opts) {

$params = array();
if ($pfp_opts['post_id'] == '') { // for multiposts

    if ($pfp_opts['tag_slug'] != ''){
        $params['tag_slug__in'] = explode(",", $pfp_opts['tag_slug']);
    }
    if ($pfp_opts['cat'] != ''){
        $params['category__in'] =  explode(",", $pfp_opts['cat']);
    }
    if ($pfp_opts['cat_slug'] != '') {
        $params['category_name'] = $pfp_opts['cat_slug'];
    }
    if($pfp_opts['author'] != '') {
        $params['author'] = $pfp_opts['author'];
    }
    if($pfp_opts['order_by'] != '') {
        $params['order_by'] = $pfp_opts['order_by'];
    }
    if($pfp_opts['num'] != '') {
        //$params['numberposts'] = $pfp_opts['num'];
        $params['posts_per_page'] = $pfp_opts['num'];
        $params['paged'] = $pfp_opts['cur_page'];
        //if($pfp_opts['page_num'] != '') {
            // work out offset depending on page num
            //$offset = $pfp_opts['page_num'] * $pfp_opts['num'];

            //$params['offset'] = $offset;
        //}
    }
    else
    {
        $params['posts_per_page'] =  -1; // gets them all
    }
    // apply whatever the case:
    $params['suppress_filters'] = false;


    // get the posts
    //$postslist = get_posts($params);
    $postslist = query_posts($params);

}else{ // for single posts
    $postslist[0] = wp_get_single_post($pfp_opts['post_id']);
}
return $postslist;
}


//add_action('admin_menu', 'my_plugin_menu');

//function my_plugin_menu() {
//add_options_page('Posts for Page Plugin Options', 'Posts for Page Plugin', 'manage_options', 'pfp-unique-identifier', 'my_plugin_options');
//}

function my_plugin_options() {
if (!current_user_can('manage_options'))  {
    wp_die( __('You do not have sufficient permissions to access this page.') );
}
echo '&lt;div class="wrap"&gt;';
echo '&lt;p&gt;How to use: In your page (HTML view) add shortcode e.g. [posts-in-page cat_slug=\'asp-net-programming-2\'] &lt;/p&gt;&lt;p&gt;This will get all posts with the category slug \'asp-net-programming-2\'&lt;/p&gt; ';
echo '&lt;p&gt;post_id - Gets a post by ID, tag_slug - gets posts by tag slug, cat - gets posts with a category ID, cat_slug - gets posts with this category slug, author - gets posts by author, order_by - date or title, num - number of posts to show&lt;/p&gt;';
echo '&lt;/div&gt;';
}

add_action('wp_head', 'get_pfp_css');

function get_pfp_css()
{
echo '&lt;link type="text/css" rel="stylesheet" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/posts-for-page/pfp.css" /&gt;' . "\n";
}
?&gt;
</code></pre>

<p>Below is the exact line of output I'm trying to modify in the code above (in the code above, about 2/3 down the page). Notice that I have tried to use an "increment" in order to add some type of identifier to the <code>&lt;a name=""&gt;</code> reference. I don't care what the identifier name is, but I need it to generate automatically so that I can reference it in a link from another page.</p>

<pre><code>$output .= "&lt;h2 class='entry-title'&gt;&lt;a name='&lt;?php echo $i++; ?&gt;' style='color:#4a79a4; font-family:comic; font-size:1em' href='$link'&gt;$title&lt;/a&gt;&lt;/h2&gt;";
</code></pre>
