0

I think the best way to approach this is jump straight in and explain as I go.

I am using wordpess with a custom made template (we made it) this code is to deal with the twitter widget pro. (this isn't the prettiest of code granted, I will clean it up later)

<?php

/*
  * TWITTER SIDE BAR
  *
  * Sets the twitter area to display: none and pulls in the content from the server
  * Javascript then does a string replace to remove a bit of unwanted text
  * finally javascript will write the doctored string to the client browser
*/

?><div id="twitterRight">

  <ul class="xoxo" style="list-style-type:none; font-size:11px; margin:0px 20px 0 0;">
    <li id="twitter-2" class="widget-container widget_twitter">
    <div id="twitHold" style="display:none;">
    <h3 class="widget-title"><span class='twitterwidget twitterwidget-title'><a href="http://www.twitter.com/username" target="_blank"><img src="<?php echo home_url(); ?>/images/twitterName.png" width="208" height="27" alt="EhomeS" /></a></span></h3>

  <ul>
  <?php 
    $twitter = dynamic_sidebar('primary-widget-area');
  ?></ul>
  </div>
  <div id="twitHolder">

  <script type="text/javascript">
    // Get the posted content from the server
    var str = $('#twitHold').html();
    var x = str.replace("Twitter: @username", "");
    //var shortString = x.substr( 0, 10 );
    document.write(x);
  </script>    
  </div>

  </li></ul>
</div><!-- END #twitterRight -->

The problem is the wordpress function dynamic_sidebar doesn't return a string or anything, only a boolean value so I cannot manipulate that. So what I have done is stored the outputted HTML in a js variable x and manipulated it from there.

What I am trying to achieve is to simply limit the number of characters in each list item (tweets) however I cannot find a way of doing so. I have tried this so far with no luck (im thinking because the javascript is writing it out and parsing it, im not sure).

Is there a way to perform the substr on the list items?

Community
  • 1
  • 1
zomboble
  • 835
  • 2
  • 10
  • 23

1 Answers1

1

Question is a little unclear, if you want a collection of the list items then try this.

use a jquery selector, jquery has good documentation http://jquery.com/

be sure to add the jquery library to your document.

<ul id="twitterlist">
  <?php 
    $twitter = dynamic_sidebar('primary-widget-area');
?></ul>

<script type="text/javascript">
var listitems = $('#twitterlist').children();
</script>
lukenz
  • 139
  • 1
  • Hey thanks for the comment, all I am getting on the browser window is [object Object] any reason why? (given you an upvote) – zomboble Aug 28 '12 at 09:43
  • use the chrome javascript debugger, to have a look inside the javascript execution variables, you should be able to get a clearer picture – lukenz Aug 28 '12 at 09:48
  • In he outer html its giving "" so I think this is where I am getting stuck, the javascript is parsed rather than actually adding to the "html" or am i wrong? – zomboble Aug 28 '12 at 09:53
  • 1
    use $('#twitholder').html(x) to insert into the DOM – lukenz Aug 28 '12 at 09:56
  • Ahh thank you! Now, one final(i hope) question, its outputting [li#twitter-2.widget-container, li, li, li] so.. can i iterate through this and edit the text inside of that then write it back out to the document? Thanks for all your help by the way! Fast edit, if i select an array key eg listitems[0] then it gives me list items: [object HTMLLIElement] so it must be possible some how to do this :S ? – zomboble Aug 28 '12 at 10:04
  • no worries, and yes of course you can, look up the jquery .each function. It wouldn't hurt you to read some documentation for youself. – lukenz Aug 28 '12 at 10:07
  • IMHO it's not wise to include such big library as jQuery just to solve such small problem. – nez Aug 28 '12 at 10:27
  • As an example: for (var i = 0, ul = document.getElementById('twitterlist'); i < ul.children.length; i++) {ul.children[i].innerHTML = ul.children[i].innerHTML.replace('Twitter: @designworks_UK', '').substr(0, 10)} instead of loading jQ – nez Aug 28 '12 at 10:35
  • You might be right but then you have to worry about things like browser compatibility which requires a deeper knowledge – lukenz Aug 28 '12 at 10:38