/** * Load related posts */ (function($) { var getEndpointURL; // Utility get related posts JSON endpoint from URLs /** * Utility get related posts JSON endpoint from URLs * * @param string URL (optional) * @return string endpoint URL */ getEndpointURL = function( URL ) { var locationObject = document.location; if ( 'string' == typeof( URL ) && URL.match( /^https?:\/\// ) ) { locationObject = document.createElement( 'a' ); locationObject.href = URL; } if ( '' == locationObject.search ) { return locationObject.pathname + '?relatedposts'; } else { return locationObject.pathname + locationObject.search + '&relatedposts'; } }; getAnchor = function( post ) { if ( '' == post.excerpt ) var anchor_title = post.title; else var anchor_title = post.title + "\n\n" + post.excerpt; return [ '', '' ]; } generateMinimalHtml = function( posts ) { var html = ''; $.each( posts, function( index, post ) { var anchor = getAnchor( post ); var classes = 'jp-relatedposts-post jp-relatedposts-post' + index; html += '

'; html += '' + anchor[0] + post.title + anchor[1] + ''; html += '' + post.context + ''; html += '

'; } ); return ''; } generateVisualHtml = function( posts ) { var html = ''; $.each( posts, function( index, post ) { var anchor = getAnchor( post ); var classes = 'jp-relatedposts-post jp-relatedposts-post' + index; if ( '' == post.img.src ) classes += ' jp-relatedposts-post-nothumbs'; else classes += ' jp-relatedposts-post-thumbs'; html += '
'; if ( '' != post.img.src ) html += anchor[0] + '' + post.title + '' + anchor[1]; html += '

' + anchor[0] + post.title + anchor[1] + '

'; html += '

' + post.excerpt + '

'; html += '

' + post.context + '

'; html += '
'; } ); return ''; } $( function() { $.getJSON( getEndpointURL(), function( response ) { if ( 0 == response.items.length ) { return; } if ( !response.show_thumbnails ) { var html = generateMinimalHtml( response.items ); } else { var html = generateVisualHtml( response.items ); } $( '#jp-relatedposts' ).append( html ).show(); } ); } ); })(jQuery);