0

I'm using WordPress and trying to replace the content of a div using JQuery - that bit works. However the content has a URL in it and it isn't working and I cannot see why.

I'm generating a variable in PHP:

$query = new WP_Query( array( 'category_name' => 'news' ) );
if ( $query->have_posts() ) {
    // The Loop
    while ( $query->have_posts() ) {
        $query->the_post();
        $news_item = '<h2>' . get_the_title() . '</h2>';
        $news_item .= get_the_excerpt();    
        $news_item .= "<BR>";
        $news_item .= "<a href=";
        $news_item .= get_permalink();
        $news_item .= ">";      
        $news_item .= "Read More...</a>";
    }
    wp_reset_postdata();
}

That bit works.

I'm putting that into a jQuery variable:

<script type="text/javascript"> 
<?php  
    echo "var newsItem = '{$news_item}';";
?>

jQuery(document).ready(function() {
    jQuery('#soap-frontpagenews').fadeOut(500, function() {
        jQuery(this).replaceWith(newsItem).fadeIn(500);
    });
});
</script> 

and that bit works. However the resultant HTML code is not right in the way it fails to produce the link correctly:

<h1>News</h1>
<h2>Taking Better Photos With Your Nikon</h2>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque fermentum ac urna et auctor. at…
<br>
<a href="http://212.159.165.84/~soapwp/2013/09/29/taking-better-photos-with-your-nikon"></a>
Read More...

in that Read More... is OUTSIDE the link tags.

Where am I going wrong please?

Best Regards

Dave

demsley
  • 319
  • 3
  • 6

2 Answers2

1

Problem :

Your string is breaking because missing a single quote.You need to wrap up url into a single quoted string.

So Your code is something like this:

$query = new WP_Query( array( 'category_name' => 'news' ) );
if ( $query->have_posts() ) {
    // The Loop
    while ( $query->have_posts() ) {
        $query->the_post();
        $news_item = '<h2>' . get_the_title() . '</h2>';
        $news_item .= get_the_excerpt();    
        $news_item .= "<BR>";
        $news_item .= "<a href='";
        $news_item .= get_permalink();
        $news_item .= "'>";      
        $news_item .= "Read More...</a>";
    }
    wp_reset_postdata();
}

Javascript Code (Updated Code)

<script type="text/javascript"> 
var newsItem = "<?php echo $news_item; ?>"; // Variable assignment 

jQuery(document).ready(function() {
    jQuery('#soap-frontpagenews').fadeOut(500, function() {
        jQuery(this).replaceWith(newsItem).fadeIn(500);
    });
});
</script> 

Update :

variable should be quoted var newsItem = "<?php echo $news_item; ?>";

Vasim Shaikh
  • 4,485
  • 2
  • 23
  • 52
1

If you assign a PHP variable to a Javasript variable, it is better to use json_encode():

<script type="text/javascript"> 
<?php  
    echo "var newsItem = " . json_encode( $news_item ) . ";";
?>

But I would do it in another way: You can put all the news items directly from PHP in HTML div container with display: none and manage the news rotation with jQuery then.

Andy Tschiersch
  • 3,716
  • 1
  • 17
  • 24