WordPress.org

Forums

Keep formatting with post_content and post_title (15 posts)

  1. martinbelton
    Member
    Posted 1 year 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. Michael
    Forum Moderator
    Posted 1 year 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 1 year 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 1 year ago #

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

  5. 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 1 year 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 1 year 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 1 year ago #

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

  9. Michael
    Forum Moderator
    Posted 1 year 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 1 year 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 1 year ago #

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

  12. martinbelton
    Member
    Posted 1 year ago #

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

    Don't worry - fixed :)

  13. der-mali
    Member
    Posted 6 months ago #

    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(); ?>
  14. Michael
    Forum Moderator
    Posted 6 months ago #

    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(); ?>
  15. der-mali
    Member
    Posted 6 months ago #

    Thanks, alchymnyth.
    Now I understand the syntax.

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

Topic Closed

This topic has been closed to new replies.

About this Topic