2

How can I put WordPress post loop Codes in javascript?

function top_news() {
echo '<div class="news"></div>';
$query = get_posts(array(
    'showposts' => 3,
    'type' => 'topnews'
));
foreach ($query as $news) :
    the_post($news);
endforeach;
?>
<script type="text/javascript">
    $(function () {
        $(".news").typed({
            strings: ["here........."],
            typeSpeed: 1,
            showCursor: 0,
            loop: 1
        });
    });
</script><?php

}

1
  • I'm not sure you would want to do this. It may be better to make an ajax call and return whatever you need via JSON
    – Howdy_McGee
    Commented Feb 14, 2018 at 20:52

1 Answer 1

0
<?php
    ob_start();
    query_posts(array('post_type' => 'topnews'));

    while (have_posts()) : the_post(); ?>
        {
            // the_title() can use a false to return the value for use with strip_tags (apparently)
            'title'         : '<?php echo stripslashes(the_title(false)); ?>',
            // Where is this $desc coming from?

            'button_list'   : [
                { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
            ],
            'tags'          : ['Portrait']
        }, <?php
    endwhile;
    $data   =   ob_get_contents();
    ob_end_clean();
?>
<script type="text/javascript">
    $(function(){
            <?php echo $data; ?>
        });
</script>
1
  • this is not my answer. because this code not working in my theme. i use typed.js. but i don't know how work in wordpress loop. link
    – Tara
    Commented Feb 15, 2018 at 22:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.