• Resolved rebekahford

    (@rebekahford)


    I need to add an attribute to all the category links in all posts on a site I’m building for someone. Is there a simple way of modifying get_category_links ()?

    At the moment I’m using this code in the single.php file:

    <div class="entry-meta">
             <?php
    	// Get the ID of a given category
    		$category_id = get_cat_ID( 'Category Name' );
    	// Get the URL of this category
    		$category_link = get_category_link( $category_id );
    		foreach((get_the_category()) as $category)
    		{
    		echo '<a onclick="tb_remove()" href="'.$category_link.' ">'.$category->cat_name.'</a>' ;
    		}
    		?>
    </div>

    However this is outputting the following

    <a href="https://www.xxxx.xxx/?cat=0 " onclick="tb_remove()">1960s</a>

    Which works to an extent but why is it returning a cat=0?

    Is there another way to add onclick=”tb_remove()” to the links?

    Any help greatly appreciated as I’m getting worried now that I won’t be able to do it in time

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter rebekahford

    (@rebekahford)

    I solved this myself if anyone’s interested:

    <?php
    	  foreach((get_the_category()) as $category)  {
                  echo  '<a href="' . get_category_link( $category->term_id ) . '"onclick="self.parent.tb_remove();" target="main"  title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a>';
          ?>

    this is useful if you are putting your post pages in a thickbox or shadowbox ajax overlay in iframe mode

    Thanks for sharing the solution, it helped me. ??

    Thread Starter rebekahford

    (@rebekahford)

    glad it helped. i always try and post a solution if i get one, i’m hoping it means people will help me as well ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Adding code to the category links and’ is closed to new replies.