Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How do i dynamcially apply a class to links in my post entries? WordPress

  • SOLVED

when our content writers enter content into a post, any link they enter doesn’t have a class associated to it. they have a drop down in the link pop-up window, but those classes seem non-sensical. for example, they are “aligncenter; wp-caption; wp-oembed, etc”.

I would like to have classes be dynamically added to links that the editors enter via a post.

Can someone explain to me how to dynamically apply a class to a link that appears in a post?

There’s a chance the following function is interfering, but i’m not sure. This function allows me to limit the word count in posts. It’s in my child theme.

function content($num) {
$theContent = get_the_content();
$output = preg_replace('/<img[^>]+./','', $theContent);
$output = preg_replace( '/<blockquote>.*<\/blockquote>/', '', $output );
$output = preg_replace( '|\[(.+?)\](.+?\[/\\1\])?|s', '', $output );
$limit = $num+1;
$content = explode(' ', $output, $limit);
array_pop($content);
$content = implode(" ",$content)."...";
echo $content;
echo '<a href="'.get_permalink().'">[READ MORE]</a>';
}


thank you!

related link: [[LINK href="http://sandbox.bucktowndigital.com/buzz/"]]http://sandbox.bucktowndigital.com/buzz/[[/LINK]]

Answers (4)

2011-02-04

jonnyjaniero answers:

i echo michael.
surely a class already exists if the intended output is for all posts.

out of interest, you are using a child theme, is it for a particluar framework?


jonnyjaniero comments:

do you want the class added to links for further output via e.g jquery ?


sergi comments:

no framework. using Chris Coyier's Blank-Theme as parent theme.

jonnyjaneiro, yeah, i do want to be able to trigger a jquery plugin w/ a class.


jonnyjaniero comments:

how about using jquery to add class?

$(".entry-content a").addClass("my_class");

entry-content is the standard but may be different in Chris' Blank Theme

2011-02-04

Oleg Butuzov answers:

simple example
add_filter('the_content', 'the_content_filter');
function the_content_filter($string){
return str_replace('<a ', '<a class="your class name">', $string);
}


its a simple example, so it dosn't check the previously declareted classes in text.
---
in more adwanced examples you need to parse you text links with a bit more advanced parser (preg_match patterns or sax ...)


Oleg Butuzov comments:

correct Sebastian!


sergi comments:

i pasted this into my child function, w/ sebastien's edit. didn't work.

hmmm.

how could i pass a class to that READ MORE link in the function code i referenced above?

thanks, oleg


sergi comments:

note: i'm on a different build now:

http://project.bucktowndigital.com/buzz/

that's where i tried your function


Oleg Butuzov comments:

this edit will work for the "the_content" function work... to make it workable for you ...

1)

$theContent = get_the_content();
replace by
$theContent = the_content_filter(get_the_content());

2) add you custom classes to the more link...

2011-02-04

Sébastien | French WordpressDesigner answers:

oleg
i think it's
<a class="your class name"
and no
<a class="your class name">

No ? :-)

2011-02-04

Michael Fields answers:

Sergi,

May I ask what it is you are trying to accomplish? It does not make sense to me why you would want to dynamically apply a class to all links in your post content. Just about any method of doing this would require filtering the post content via php and applying classes to all anchor tags within. You can access these links via css by using the post_class. Have you thought about trying something like

.hentry a { color:red; }


sergi comments:

hi, michael,

you know what? i just realized my html was misfunctioning. i was having a b_tch of a time targeting my css because it simply didn't make sense. all the blog posts were children of the first blog post - so it left me flummoxed every time i tried a pseudo selector.

you know what was messing it up (i'm almost certain) - that chunk of php i've included in this post. if i put a character count of 200, it was breaking my html structure. only by lowering the count to 100 would it work. at least it seems....

maybe i have fixed height that was being picked up somewhere;

i'm noticing that certain advanced selectors aren't registering in firebug (ff 3.6)...does that sound correct?