Getting the result/output of a template (5 posts)

  1. GalalAly
    Posted 3 years ago #


    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' );
    		$contents = ob_get_contents();

    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


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

    Any suggestions? :)

    Thank you

  2. bcworkz
    Posted 3 years ago #

    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?

  3. GalalAly
    Posted 3 years ago #

    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.

  4. bcworkz
    Posted 3 years ago #

    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.

  5. GalalAly
    Posted 3 years ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic