-1

I'm looking to run a function 'catloop' after the content. Instead of just placing it below the_content(); I wanted to do it the right way. There is also a social plugin that places some sharing function after all the content that I need to be above, so, I have a higher priority on the filter. This is what I have in my functions file:

function catloop($content) {
if(is_page_template('page-category.php')) {
    function foo() {

   $term = get_field('category_list');
   $term_id = $term->term_id; 
   $catname = $term->name;

   // assign the variable as current category

    // concatenate the query
$args = 'cat=' . $term_id;

if( $term ): $showposts = 10;
$args = array('cat' => $term_id, 'orderby' => 'post_date', 'order' =>       'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post();

    echo '<div class="timeline">';
    echo '<a href="';
    echo  the_permalink();
    echo '"><h3>';
    echo the_title(); 
    echo '</h3></a>';

         endwhile; else: 
            _e('No Posts Sorry.'); 
        endif; 
    echo '</div>';

 endif;
    wp_reset_query(); 
}

    $foo = foo;

    $new_content = $foo();
    $content .= $new_content;   
}   
return $content;
}
add_filter('the_content', 'catloop',9);

It's working but, the problem is that it is placing this above the content. I read in other questions that I need to use 'return' instead of 'echo' but it breaks. Not sure where to go from here.

  • $foo = foo;? This cannot work. – fuxia Feb 5 '17 at 15:30
  • Hi Jason, Whats happening is that you are echoing the result, instead of passing it through the filter, since 'the_content' filter gets called before showing the content. What you could do is to save your content as a string in a variable, then append it to the content as you return it. – Pedro Coitinho Feb 5 '17 at 15:37
0

If it was me I'd not echo the text I'd make it a variable e.g. $additional_text = "" then at the end of the function return $additional_text;.

if (have_posts()) : while (have_posts()) : the_post();
    $additional_text = '<div class="timeline">';
    $additional_text .=  '<a href="';
    $additional_text .= the_permalink();
    $additional_text .= '"><h3>';
    $additional_text .= the_title(); 
    $additional_text .= '</h3></a>';
    $additional_text .= '</div>';

 endwhile; else: 
    $additional_text = 'No Posts Sorry.'; 
 endif; 

Then, when you call the function it's going to get the variable as opposed to writing to the page.

0

Ok I got it to work:

function catloop($content) {
if(is_page_template('page-category.php')) {
    function foo() {

   $term = get_field('category_list');
   $term_id = $term->term_id; 
   $catname = $term->name;

// assign the variable as current category


// concatenate the query
   $args = 'cat=' . $term_id;



$args = array('cat' => $term_id, 'orderby' => 'post_date', 'order' => 'DESC', 'posts_per_page' => $showposts,'post_status' => 'publish');
query_posts($args);


if (have_posts()) :

        while (have_posts()) : the_post();
     $perma = get_permalink();
     $title = get_the_title();
     $loopy .= '<div class="timeline"><a href="'. $perma . '"><h3>' . $title . '</h3></a>';



         endwhile; else: 
            _e('No Posts Sorry.'); 
        endif; 


endif;

    wp_reset_query(); 
         return $loopy;
}


    $foo = foo;

    $new_content = $foo();
    $fullcontent = $content . $new_content; 
}   
return $fullcontent;
}
add_filter('the_content', 'catloop',9);

If anyone has a more elegant solution I would be grateful. Thanks Pedro Coitinho and sdexp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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