javascript - WP post contains only a url, how to turn it into a link? -
so using 1 of them cool new wp templates come editor , (enfold, project).
i use built-in enfold (avia layout builder) components list blog posts in area on site. these posts contain url, want accomplish keep using built-in components listing posts (links) once user clicks 1 of post titles, should taken url of post body instead of page displaying url in plain text.
i using setup in order users able create these posts wordpress "publish emailing" feature, otherwise have used link plug-in instead.
speaking of that, sick of updating 1 million plugins time, don't want bloat wp install plugin purpose, , don't want pay writing such plugin well. , in experience wp plugins have been major security risk well. none of that.
my solution hijack link clicks in list. created 2 arrays containing post titles , corresponding urls.
i started adding script block right before tag (in header.php):
<script> //newsredirecter news_redirector_title = new array(); news_redirector_content = new array(); <?php global $post; $tmp_post = $post; $recent = new wp_query("cat=34&showposts=3"); while($recent->have_posts()) : $recent->the_post(); echo(" news_redirector_content.push('".get_the_content()."'); news_redirector_title.push('".get_the_title()."'); "); endwhile; $post = $tmp_post; ?>
the line
$recent = new wp_query("cat=34&showposts=3");
tells wp pull out posts of category 34, , pull out 3 of them.
in enfold, can add id section, did. called section containing blog posts "newsreplacer", , code below hijacked link clicks within section , redirected them using data in arrays above.
$( document ).ready( function(){ //click event edit buttons $("#newsreplacer").on('click', 'a', function(event) { url = $(this).attr('href'); current_title = $(this).html(); //console.log('original link to: ' + url); console.log('clicked link title: '+current_title); (i = 0; < news_redirector_title.length; i++) { if (news_redirector_title[i] == current_title) { event.preventdefault(); window.location.href = news_redirector_content[i]; } } }); } ) </script>
worth noting - had other normal blog posts listed in same seciton, , didn't want them hijacked. fine, since last for-loop looks in array title identical title of clicked link.
Comments
Post a Comment