0

i want to loop all post title inside javascript to build my gallery but it does not works i get strange output My code :

  <?php
        query_posts(array( 
            'post_type' => 'musings'
        ) );  
         while (have_posts()) : the_post(); 
        $title = the_title();
        $desc = the_content();
        $galleryslider .= "{
                        'title'         : '".$title."',
                        'description'   : '".$desc."',
                        'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                        'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                        'button_list'   :
                        [
                            { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                            { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                        ],
                        'tags'          : ['Portrait']
                    },";
     endwhile;
     $galleryslider = rtrim($galleryslider,',');
    ?>
    <script type="text/javascript">
    $(function(){
        <?php echo $finalslider;?>
    });
    </script>

Expected out put

{
                'title'         : 'post 1 title',
                'description'   : 'post 1 desc',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            },{
                'title'         : 'post 2 title',
                'description'   : 'post 2 desc',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            }

Ouptut i get (wordress pages show title and desc above javasscript but value on title and desc inside javascrit is blank see below code)

{
                'title'         : '',
                'description'   : '',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            },{
                'title'         : '',
                'description'   : '',
                'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
                'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
                'button_list'   :
                [
                    { 'title':'Demo', 'url' : 'http://bonchen.net/' },
                    { 'title':'Download', 'url':'http://porfolio.bonchen.net/'}
                ],
                'tags'          : ['Portrait']
            }
3
  • query_posts(array( 'post_type' => 'musing', ) ); has a comma that it shouldn't have I think.
    – bassxzero
    Apr 11, 2015 at 3:59
  • Could you give us an example of output you are expecting?
    – Paul Sasik
    Apr 11, 2015 at 4:03
  • removed that i am not getting title inside jquery 'title' :
    – mydeve
    Apr 11, 2015 at 4:03

2 Answers 2

1

From the little I know about Wordpress, try either echoing the data right out or save to the output buffer and echo later. Also, you may have to return false on the the_title() in order to use strip_tags(). Also, $desc does not appear to be define.:

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

    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?
            'description'   : '<?php echo $desc; ?>',
            'thumbnail'     : ['images/small/1.jpg', 'images/small/2.jpg'],
            'large'         : ['images/large/1.jpg', 'images/large/2.jpg'],
            '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>
4
  • What do you mean? What other way would you like to do it?
    – Rasclatt
    Apr 11, 2015 at 4:26
  • it means to say that using ob_get_contents is right way , can we do some changes in wp query to get results
    – mydeve
    Apr 11, 2015 at 4:27
  • Using the output buffer just basically writes to memory before it writes to the page, then you assign the contents to a variable. It's basically the same thing you are doing, however, some of the wp functions echo which you can not assign to a variable. You can change the functions to return and not echo (according to the manual) then what you have "should" work
    – Rasclatt
    Apr 11, 2015 at 4:30
  • What I have done is fine, there is nothing wrong with using it as is.
    – Rasclatt
    Apr 11, 2015 at 4:31
0

I don't believe you had to change quite so much of your code as you appear to have simply not been using the right Wordpress functions/not passing the right arguments to them. By default the_title() & the_content() cause display at run time.

To make use of them programmatically you need something like this:

<?php
        query_posts(array( 
            'post_type' => 'musings'
        ) );  
         while (have_posts()) : the_post(); 
        $title = the_title(false);
        $desc = get_the_content();
        $galleryslider .= "{

        ...

Your original code sample then echoes out a $finalslider variable - is that a typo? If not how is that getting set when you're using $galleryslider in the build up

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.