By Tracy Ridge
The following tutorial will display your latest posts in a news ticker format. The code is basically the same as my popular post Display Your Latest Posts on a Static Page except that the output is in a unordered list for the jQuery news ticker to work. This tutorial is meant for non WordPress websites to access their WordPress powered blog. To add a news ticker to your WordPress powered blog to display your latest posts try this tutorial. To add a news ticker to your WordPress blog with external RSS feeds try this tutorial.
Image may be NSFW.
Clik here to view.
Prerequisites
A PHP web server or hosting, preferably a Linux server running WordPress, although this works with other servers. Please Note: This will not work if your blog is on a different server. A little knowledge of PHP, HTML and CSS is recommended but not required. We will be using the jQuery News Ticker script.
Download
Updated 24th March 2014
Image may be NSFW.
Clik here to view.
Retrieving the WordPress Posts
In order to retrieve our blog posts we require to link to the WordPress blog header. At the very top of your page add the following above the doctype.
<php require('wp-blog-header.php');//link to your blog-header.php using relative path /*if you are getting 404 errors uncomment the next 2 lines*/ //status_header(200); //nocache_headers(); ?>
Please Note: The path has to be relative so you can’t link to the file directly using http://
Now in the body (<body></body>) section you need to add the following snippet of code to retrieve the posts.
<?php /*Code to fetch our WP Posts. Arguments (numberposts etc)) can be changed*/ $args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date"); $postslist = get_posts( $args );?> <ul id="events" class="js-hidden"> <?php /*Outputs our posts in a unordered list required for the jQuery script to run*/ foreach ($postslist as $post) : setup_postdata($post); ?> <li class="news-item"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul>
Adding the jQuery News Ticker
Upload the folders CSS, Images and JS folders to your server. Add the following in-between the opening and closing head tags (<head></head>) in your page.
<!--Links to CSS and JS files--> <link href="css/ticker-style.css" rel="stylesheet" type="text/css" /> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <script src="js/news_ticker.min.js"></script>
Underneath, still in-between the opening and closing the head tags, you want to add a little bit of jQuery to initialize the news ticker script
<script type="text/javascript"> $(function () { $('#events').ticker(); }); </script>
FAQs
Can I change any of the news ticker settings?
The jQuery news ticker comes with options that you can change.
$(function () { $('#js-news').ticker( speed: 0.10, // The speed of the reveal // MUST BE ON THE SAME DOMAIN AS THE TICKER feedType: 'xml', // Currently only XML htmlFeed: true, // Populate jQuery News Ticker via HTML debugMode: true, // Show some helpful errors in the console or as alerts // SHOULD BE SET TO FALSE FOR PRODUCTION SITES! controls: true, // Whether or not to show the jQuery News Ticker controls titleText: 'Latest', // To remove the title set this to an empty String displayType: 'reveal', // Animation type - current options are 'reveal' or 'fade' direction: 'ltr' // Ticker direction - current options are 'ltr' or 'rtl' pauseOnItems: 2000, // The pause on a news item before being replaced fadeInSpeed: 600, // Speed of fade in animation fadeOutSpeed: 300 // Speed of fade out animation ); });
Conclusion
For a more in-depth detail please read index.php in the zip file. If you are having any trouble please feel free to leave a comment or email me. I would like to thank the Rhodri George for creating the jQuery News Ticker script.
The post Display WordPress Posts as a News Ticker appeared first on WorldOWeb.