WordPress.org

Ready to get started?Download WordPress

Forums

show page title via slug on homepage (8 posts)

  1. ProbablyBest
    Member
    Posted 11 months ago #

    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?

  2. paulwpxp
    Font hero
    Posted 11 months ago #

    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.

  3. ProbablyBest
    Member
    Posted 11 months ago #

    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();
    				?>
  4. paulwpxp
    Font hero
    Posted 11 months ago #

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

  5. ProbablyBest
    Member
    Posted 11 months ago #

    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();

  6. ProbablyBest
    Member
    Posted 11 months ago #

    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?

  7. paulwpxp
    Font hero
    Posted 11 months ago #

    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;
  8. ProbablyBest
    Member
    Posted 11 months ago #

    Brilliant, that's perfect. Thank you for your help :)

Reply

You must log in to post.

About this Topic

Tags