• Hello,

    I am trying to get the output of a template file and save it as HTML. The following is my approach:

    1) I have a page template, called page-template.php.
    2) In my plugin.php I have

    add_action('edit_post', 'static_post');
    add_action('publish_post', 'static_post');
    
    function static_post($post_id){
    	$post_type = strtolower(get_post_type($post_id));
    	if($post_type == 'page'){
    		$template = get_post_meta( $post_id, '_wp_page_template' );
    		ob_start();
    		require_once(locate_template($template[0]));
    		$contents = ob_get_contents();
    		custom_debug($contents);
    		ob_end_clean();
    	}
    }

    The custom_debug function writes the $contents to a .txt file for debugging.

    3) The output is as follows:
    Say the page-template.php contains

    <?php while(have_posts()): the_post(); ?> <h2><?php the_title(); ?></h2>

    The output in the debug .txt file mentioned above is only

    <h2></h2>

    which means that the loop is not being executed. Or the query. I am not really sure.

    Any suggestions? 🙂

    Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    The fact you are getting the h2 header tags output (assuming your snippet is the only place they could have come from) means the query was executed, post(s) were found, and the loop was initiated. For some reason, the_title() has not returned anything. Either the post in question has no title, or some error occurred somewhere. Do you have debug mode enabled in wp-config.php?

    Thread Starter GalalAly

    (@galalaly2)

    Ok I turned it on and got a couple of warnings. The empty <h2> tag was actually because of an array i created that contains an empty element. The <h2> tags that should be output by the loop do not exist at all.

    It’s worth mentioning that when I call the get_the_title(); outside the loop it works. So I think the problem is in the loop itself.

    Moderator bcworkz

    (@bcworkz)

    You should probably fix the warnings, depending on what they are, it may make a difference. There’s not enough information here to determine what may be the problem. It’s really a matter of basic debugging. Place something like die(print_r($test_var)); where you think there might be a problem and verify the important variables contain expected values. Good luck.

    Thread Starter GalalAly

    (@galalaly2)

    Yeah the warnings were fixed before posting my last message and believe me I’ve done a lot of debugging before posting in here 🙂 Thank you

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Getting the result/output of a template’ is closed to new replies.