Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

So I have run across a bizzar issue, in which I know is working as i expect it to in terms of the array values, yet the _html variable returns something like string(29) " " which to my knowledge means 29 spaces, since spaces count as characters (feel free to correct me).

Any ways, the class:

<?php

class AisisCore_Template_Helpers_Loop{

    protected $_options;

    protected $_wp_query;

    protected $_html = '';

    public function __construct($options = array()){
        global $wp_query;

        if(isset($options)){
            $this->_options = $options; 
        }

        if(null === $this->_wp_query){
            $this->_wp_query = $wp_query;
        }
    }

    public function init(){}

    public function loop(){
        if(isset($this->_options)){
            if(isset($this->_options['query'])){
                $this->_query_post($this->_options['query']);
            }elseif(isset($this->_options['type']) && $this->_options['type'] == 'single'){
                $this->_single_post();
            }else{
                $this->_general_wordpress_loop();
            }
        }else{
            $this->_general_wordpress_loop();
        }
    }

    protected function _general_wordpress_loop(){
        if($this->_wp_query->have_posts()){
            while($this->_wp_query->have_posts()){
                $this->_wp_query->the_post();
                the_excerpt();
            }
        }
    }

    protected function _query_post($query){
        $empty_query = $this->_wp_query;
        $wp_query = new WP_Query($query);

        if($wp_query->have_posts()){
            while($wp_query->have_posts()){
                $wp_query->the_post();
                the_excerpt();
            }
        }

        next_posts_link('&laquo; Older Entries'); 
        previous_posts_link('Newer Entries &raquo;');

        $wp_query = $empty_query;
    }

    protected function _single_post(){
        if($this->_wp_query->have_posts()){
            while($this->_wp_query->have_posts()){
                $this->_wp_query->the_post();

                if(isset($this->_options['wrapper'])){
                    $this->_html .= '<div ';

                    if(isset($this->_options['wrapper']['class'])){
                        $this->_html .= 'class="'.$this->_options['wrapper']['class'].'"';
                    }elseif(isset($this->_options['wrapper']['id'])){
                        $this->_html .= 'class="'.$this->_options['wrapper']['id'].'"';
                    }

                    $this->_html .= ' >';
                }

                if(isset($this->_options['title_header'])){
                    $this->_html .= '<'.$this->_options['title_header'].'>';
                    the_title();
                    $this->_html .= '</'.$this->_options['title_header'].'>';
                }else{
                    the_title();
                }

                $this->_html .= '<a href="'.get_author_posts_url(get_the_author_meta( 'ID' )).'">'.the_author_meta('display_name').'</a>';
                the_date();
                if(isset($this->_options['image'])){
                    if(isset($this->_options['size']) && isset($this->_options['args'])){
                        the_post_thumbnail($this->_options['image']['size'], $this->_options['image']['args']);
                    }else{
                        the_post_thumbnail('medium');
                    }
                }

                the_content();

                if(isset($this->_options['wrapper'])){
                    $this->_html .= '</div>';
                }
            }
        }
    }

}

the function to focus on is the _single_post() function. To instantiate and use this class we do the following:

$array = array(
    'wrapper' => array(
        'class' => 'span12'
    ),
    'title_header' => 'h1',
    'image' => array(
        'size' => 'type',
        'args' => array(
            'align' => 'centered', 
            'class' => 'thumbnail marginBottom20 marginTop20'
        )
    ),
    'type' => 'single'
);

$loop = new AisisCore_Template_Helpers_Loop($array);
$loop->loop();

So as you can see we set a wrapper div, a title_header, image attributes and a type of single. The problem is, I get into each if statement where I am like if this is a wrapper and the class key is set with a value.. but the $this->_html returns empty.

when I var_dump($this->_html); I get an empty string. One might ask "are you getting into where your doing $this->_html .= 'some content'; ? and the answer is yes I am, how ever the string seems empty.

so I turn to you guys to help me see what I am doing wrong here.

I have tried looking at the source to see if maybe I am crazy, but alas I am not, there is no h1 tags, wrapper tags or any thing else - yet according to the var dumps of _options, its all set and ready to go.

share|improve this question
    
This looks identical, or nearly identical, to stackoverflow.com/questions/14667848/… –  s_ha_dum Feb 3 '13 at 1:15
    
This is a php question and not really related to WordPress. –  Chris_O Feb 3 '13 at 4:05
add comment

1 Answer

so I turn to you guys to help me see what I am doing wrong here.

First thing your doing wrong is over complicating a simple effective API.

Your code makes no sense. Your mixing functions that echo output with arbitrary html mark up that gets assigned to a non existing class property that nothing is ever done with.

Your trying to OOP something that you have written as procedural. Just use a variable that lives inside the method and either return it or echo it and be done. And while your at it use the WordPress functions that return the values instead of printing them and assign to this same variable.

The only reason to use protected properties are for class extensibility and building objects. Your just running the loop.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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