WordPress.org

Ready to get started?Download WordPress

Forums

Keep formatting with post_content and post_title (12 posts)

  1. martinbelton
    Member
    Posted 2 months ago #

    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.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 months ago #

    $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.

  3. martinbelton
    Member
    Posted 2 months ago #

    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!

  4. martinbelton
    Member
    Posted 2 months ago #

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

  5. Jose Castaneda
    Member
    Posted 2 months ago #

    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.

  6. martinbelton
    Member
    Posted 2 months ago #

    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(); ?>
  7. martinbelton
    Member
    Posted 2 months ago #

    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.

  8. martinbelton
    Member
    Posted 2 months ago #

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

  9. alchymyth
    The Sweeper & Moderator
    Posted 2 months ago #

    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(); ?>
  10. martinbelton
    Member
    Posted 2 months ago #

    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.

  11. martinbelton
    Member
    Posted 2 months ago #

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

  12. martinbelton
    Member
    Posted 2 months ago #

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

    Don't worry - fixed :)

Reply

You must log in to post.

About this Topic