WordPress.org

Support

Support » How-To and Troubleshooting » Keep formatting with post_content and post_title

Keep formatting with post_content and post_title

  • Jose Castaneda suggested this code:

    $pages = get_pages( 'include=7,13,26,35' );
    foreach ( $pages as $page ){
      echo  $page->post_title;
      echo $page->post_content;
    }

    which does the job, but is not formatted. I have looked at this post, but can’t work out how to apply the syntax.

Viewing 14 replies - 1 through 14 (of 14 total)
  • $pages = get_pages( 'include=7,13,26,35' );
    foreach ( $pages as $page ){
      echo  apply_filters( 'the_title', $page->post_title );
      echo apply_filters( 'the_content', $page->post_content );
    }

    formatting might also depend on html tags and CSS classes which are missing in your code.

    Thank you for showing me the correct syntax – but unfortunately, css must be missing as you suggested – looks like I will have to go the long way about it!

    I am guessing it doesn’t inherit the css because of the directory it is in?

    Moderator Jose Castaneda

    @jcastaneda

    THEME COFFEE MONKEY

    It’s not the directory. What alchymth and I provided is PHP code. Unfortunately we don’t know how your theme is structured when it comes to HTML/CSS so all we can really do is provide the basic foundation for you.

    I am guessing then that div class="page-on-page" is why the css isn’t inherited.

    The template code is:

    <?php
    /*Template Name: Subjects
    */
    
    get_header(); ?>
    
    <div id="primary" class="site-content">
        <div id="content" role="main" >
    
            <?php
            $args = array(
              'post_type' => 'page',
              'post__in' => array(156) //list of page_ids
            );
            $page_query = new WP_Query( $args );
            if( $page_query->have_posts() ) :
            echo '<div class="pages-on-page">';
            //print any general title or any header here//
            while( $page_query->have_posts() ) : $page_query->the_post();
            echo '<div class="page-on-page" id="page_id-' . $post->ID . '">';
    		$pages = get_pages( 'include=5,11,21,105,100,7' );
    		foreach ( $pages as $page ){
     	 	echo  apply_filters( 'the_title', $page->post_title );
      		echo apply_filters( 'the_content', $page->post_content );
    		}
            echo '</div>';
            endwhile;
            echo '</div>';
            else:
            //optional text here is no pages found//
            endif;
            wp_reset_postdata();
            ?>
    
        </div><!-- #content -->
    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    If it is, can I somehow write in "page-on-page"="site-content"

    or For "pages-on-page" @import url("../twentytwelve/style.css")

    … the syntax is clearly incorrect here, but hopefully you can see what I am after.

    Can I not just apply the same css to this template?

    the CSS class .page-on-page was only a suggestion to allow the formatting of the content independently of the content of the surrounding page.

    if you want to mirror the theme’s CSS, use the structure as it is used in content-page.php: <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

    example integrated into your cleaned-up template code:

    <?php
    /*Template Name: Subjects
    */
    
    get_header(); ?>
    
    <div id="primary" class="site-content">
        <div id="content" role="main" >
    
            <?php
    		$pages = get_pages( 'include=5,11,21,105,100,7' );
    		foreach ( $pages as $page ){ ?>
    	<article id="post-<?php echo $page->ID; ?>" class="page hentry">
    		<header class="entry-header">
    		<h1 class="entry-title">
     	 	<?php echo  apply_filters( 'the_title', $page->post_title ); ?>
    		</h1>
    		</header>
    		<div class="entry-content">
      		<?php echo apply_filters( 'the_content', $page->post_content ); ?>
    		</div><!-- .entry-content -->
    	</article><!-- #post -->
    		<?php }
            wp_reset_postdata();
            ?>
    
        </div><!-- #content -->
    </div><!-- #primary -->
    
    <?php get_footer(); ?>

    I can put

    <style type="text/css">
    body {
    	line-height: 2;
    }
      </style>

    below get_header(); ?> for example, but the titles are not differentiated from the rest of the text – all text is treated with this style.

    THANK YOU!!! Perfect! I will study the code carefully!

    Just one thing – pages have reverted to alphabetical order – is this a straightforward fix?

    Don’t worry – fixed 🙂

    Can you help me please?

    Let’s say I want the title formatted as <h2>
    Where do I place the <h2> tags?

    <?php get_header(); ?>
    
    <div class="contentbox">
    
    <?php
     $id = 630;
     $p = get_page($id);
     echo apply_filters('the_title', $p->post_title);
     echo apply_filters('the_content', $p->post_content);
    ?>
    
    </div>
    
    <?php get_footer(); ?>

    caveat: get_page() is deprecated http://codex.wordpress.org/Function_Reference/get_page – use get_post() instead http://codex.wordpress.org/Function_Reference/get_post ;

    i.e. the changed code could be, for example:

    <?php get_header(); ?>
    
    <div class="contentbox">
    
    <?php
     $id = 630;
     $p = get_post($id);
     echo '<h2>' . apply_filters('the_title', $p->post_title) . '</h2>';
     echo apply_filters('the_content', $p->post_content);
    ?>
    
    </div>
    
    <?php get_footer(); ?>

    Thanks, alchymnyth.
    Now I understand the syntax.

    It is not just only adding the <h2> but also the 2 dots and the brackets.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Keep formatting with post_content and post_title’ is closed to new replies.
Skip to toolbar