WordPress.org

Support

Support » Themes and Templates » show page title via slug on homepage

show page title via slug on homepage

  • Im using the following code to pull the about page into the homepage

    <?php
    					// query for the about page
    					$your_query = new WP_Query( 'pagename=about' );
    					// "loop" through query (even though it's just one page)
    					while ( $your_query->have_posts() ) : $your_query->the_post();
    						the_content();
    					endwhile;
    					// reset post data (important!)
    					wp_reset_postdata();
    				?>

    But its not showing the page title in this, just the content. Is there way to pull the page title in as well as the content?

Viewing 7 replies - 1 through 7 (of 7 total)
  • paulwpxp

    @paulwp

    Font hero

    Function Reference/the_title()
    http://codex.wordpress.org/Function_Reference/the_title

    So your example above, replace

    the_content();

    with this

    <article class="my-about">
    	<h3 class="entry-title"><?php the_title(); ?></h3>
    	<div class="entry-content"><?php the_content(); ?></div>
    </article>

    so that you can style it.

    thank you for getting back to me however that has caused an error.

    This is the updated code:

    <?php
    					// query for the about page
    					$your_query = new WP_Query( 'pagename=about' );
    					// "loop" through query (even though it's just one page)
    					while ( $your_query->have_posts() ) : $your_query->the_post();
    						<h3 class="entry-title"><?php the_title(); ?></h3>
    						<div class="entry-content"><?php the_content(); ?></div>
    					endwhile;
    					// reset post data (important!)
    					wp_reset_postdata();
    				?>
    paulwpxp

    @paulwp

    Font hero

    Not HTML5 ? Change <article> to <div>, but all these are necessary only if you need to style it.

    no its not the html 5 causing the issue it doesn’t like have <?php the_title(); ?> and <?php the_content(); ?> written in that way. it only works if content is written like the_content();

    I have managed to fix it by the following

    <?php
    					// query for the about page
    					$your_query = new WP_Query( 'pagename=about' );
    					// "loop" through query (even though it's just one page)
    					while ( $your_query->have_posts() ) : $your_query->the_post();
    						the_title();
    						the_content();
    					endwhile;
    					// reset post data (important!)
    					wp_reset_postdata();
    				?>

    However it wont let me put in any html around it. Is there anywhere to pull in a page slug that allows you to add HTML?

    paulwpxp

    @paulwp

    Font hero

    Ok, I got it now, you just need to have a closing the php tag before and an opening one after.

    while ( $your_query->have_posts() ) : $your_query->the_post(); ?>
    		<div class="my-about">
    			<h3 class="entry-title"><?php the_title(); ?></h3>
    			<div class="entry-content"><?php the_content(); ?></div>
    		</div>
    <?php endwhile;

    Brilliant, that’s perfect. Thank you for your help 🙂

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘show page title via slug on homepage’ is closed to new replies.
Skip to toolbar