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.

I would like the H4 title to display before the loop starts and only once.

The problem I'm having is that H4 tag title I've created either gets stuck in the loop or it displays on the top of the post, before the content, which is what I do not want.

This is what I have.

//Related stock photo function
function custom_content_after_post($content){
    if (get_post_type( get_the_ID() ) == 'portfolio') {

        //Query for the the related posts - portfolio custom post type
        $loop = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 5, 'orderby' => 'rand'));
        $sptitle = '<h4>Related Stock Photos</h4>';

        while ( $loop->have_posts() ) {
            $loop->the_post();

            $content .= '<div style="margin:25px 10px 0px 10px;">';
            $content .= '<ul style="list-style-type:none;">';
            $content .= '<li style="display:inline;float:left;">';              
            //Display post thumbnail assigned to it.
            $content .= '<div style="margin:25px 10px 0px 10px;">';
            $content .= '<a href="' .get_permalink(). '" >' .get_the_post_thumbnail($page->ID, 'thumbnail'). '</a>';
            $content .= '<p><strong><a href="' .get_permalink(). '" >' . get_the_title() . '</a></strong></p>';                 
            $content .= '</li>';
            $content .= '</ul>';
            $content .= '</div>';
        }
    }
    return $content;
}

add_filter( 'the_content', 'custom_content_after_post' );
share|improve this question
    
Do you mean the <h4 tag? –  s_ha_dum Jul 4 '13 at 22:11
    
yeah, sorry, I meant h4 tag. –  Jerry R. Jul 5 '13 at 0:09
    
One of your div tags is not being closed, you open two but only close one. –  t31os Jul 5 '13 at 10:06
    
@t31los thanks for the heads up! Appreciate it. –  Jerry R. Jul 5 '13 at 17:29
add comment

1 Answer 1

up vote 1 down vote accepted

Try this instead :

   //Related stock photo function
   function custom_content_after_post($content){
    if (get_post_type( get_the_ID() ) == 'portfolio') {

    //Query for the the related posts - portfolio custom post type
    $loop = new WP_Query( array('post_type' => 'portfolio', 'posts_per_page' => 5, 'orderby' => 'rand'));
    $sptitle = '<h4>Related Stock Photos</h4>';

    while ( $loop->have_posts() ) {
        $loop->the_post();

        $div  = '<div style="margin:25px 10px 0px 10px;">';
        $div .= '<ul style="list-style-type:none;">';
        $div .= '<li style="display:inline;float:left;">';              
        //Display post thumbnail assigned to it.
        $div .= '<div style="margin:25px 10px 0px 10px;">';
        $div .= '<a href="' .get_permalink(). '" >' .get_the_post_thumbnail($page->ID, 'thumbnail'). '</a>';
        $div .= '<p><strong><a href="' .get_permalink(). '" >' . get_the_title() . '</a></strong></p>';  
        $div .= '</div>';               
        $div .= '</li>';
        $div .= '</ul>';
        $div .= '</div>';

    }

 }

 return $content .$sptitle .$div;
}

add_filter( 'the_content', 'custom_content_after_post' );
share|improve this answer
    
thanks for the help! I added return $content .$sptitle .$div; and that displayed the H tag once before the query loop. –  Jerry R. Jul 5 '13 at 0:20
    
That worked perfect! See here pixelgrunt.com/stock-photo/zebra-stock-photo Thanks again! –  Jerry R. Jul 5 '13 at 0:22
    
you're welcome mark it as resolved. –  JMau Jul 5 '13 at 0:24
1  
Same comment here, you open two div tags and close only one. Whilst it may work, it's invalid markup. –  t31os Jul 5 '13 at 10:07
    
True, I did not make any attention to that. I was focused on filter. I'm editing –  JMau Jul 5 '13 at 11:57
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.