WordPress.org

Forums

content before the more tag and functions.php (3 posts)

  1. cooperanet
    Member
    Posted 5 years ago #

    Hi,

    I try to retrieve the content before the <!--more--> tag of a page.

    I use a custom query and I created a specific function in my functions.php file.

    Here is my custom query in my index.php page

    <?php
    		$arg = array('post_type'=>'page','post_parent'=>5, 'showposts'=>1, 'orderby'=>'rand', 'post__not_in'=>array(36));
    		$randomService = new WP_Query($arg);
    			while($randomService->have_posts()) : $randomService->the_post();
    		?>
    <!-- this is the function supposed to display the stuff -->
    <?php the_page_excerpt(150); ?>
    
    <?php endwhile; ?>

    and this is the function in my functions.php file

    function the_page_excerpt($length) {
    	global $more;
    	if ($pos=strpos($post->post_content, '<!--more-->')) { $more = 0; the_content(''); }
    	else { $page_excerpt = $post->post_content; echo substr(strip_tags($page_excerpt),0,$length).'&nbsp;...'; }
    }

    But nothing displays... and if I add these lines in my index.php:

    global $more;
    	if ($pos=strpos($post->post_content, '<!--more-->')) { $more = 0; the_content(''); }
    	else { $page_excerpt = $post->post_content; echo substr(strip_tags($page_excerpt),0,200).'&nbsp;...'; }

    It works...

    So, what is wrong with the function in my functions.php file?

    Thanks a lot for your help!

  2. Samuel B
    moderator
    Posted 5 years ago #

    did you put the functions.php code at the very top?

  3. cooperanet
    Member
    Posted 5 years ago #

    Thanks for your response.

    I tried to put my code at the very top of the functions.php file but it changes nothing.

    Any idea is welcome!

Topic Closed

This topic has been closed to new replies.

About this Topic