WordPress.org

Ready to get started?Download WordPress

Forums

show page title via slug on homepage (8 posts)

  1. ProbablyBest
    Member
    Posted 1 year 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 1 year 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 1 year 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 1 year ago #

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

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

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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags