Leyb Leyb - 3 months ago 9x
PHP Question

How to show a specific post on a page on my wordpress theme

I'm trying to show on my wordpress theme a specific post on a page. In this case, I'm trying to show the post on the home page and I've tried a lot to at least show the title, but all that I get is the title of the page, not of the post itself.

I've tried the_title and get_the_title() but the magic didn't happen.


Here comes the code relevant to my problem.



if ( have_posts() ) :

/* Start the Loop */
while ( have_posts() ) : the_post();?>

* Include the Post-Format-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Format name) and that will be used instead.
// if ( has_post_format( 'video' )) {
// echo 'this is the video format';
// }
get_template_part( 'template-parts/content-chat', get_post_format('chat') );


else :

// get_template_part( 'template-parts/content', 'none' );

endif; ?>

And it calls the file (it is calling the right file, double checked)

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >

<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
<?php the_content();
echo get_post_format();

</article><!-- #post-## -->

In summary, the questions are:

1) Why is it showing the title of the page?

2) How can I show properly this kind of post format in my page?


For your first question wp grabs the title of the home page based on query_posts() this is what makes wordpress so dynamic, you can actually check a page or category id and then select with query posts what to show on it. Since you do not have a query_posts in your code wp defaults to the current post id which is your home page.

For your second, I had a code snippet, but it is at home, you can look it up here https://developer.wordpress.org/reference/functions/query_posts/

Ok, so here is the snippet. Sorry for the delay, I have been on vacation.

First you have to determine the ID of your home page, or you can use the is home page function. If you go the former route you simply go to the back end to your home page and hover over it with your mouse. the home page should show up in a link tooltip at the bottom of your page. It will be something like post=5 the number is what you need. Next I would create a category for the post you want to display on the front page and get that ID from the category page in the back end. Again it will be something like tag_id=12


            $currpage = $post -> ID;
            if($currpage == 5) { 
            while (have_posts()): the_post();  

You can have multiple posts on one page this way, infact you can have your whole loop on that page then under it dynamically populate other posts under it like in this example: http://testex-ndt.com/products/ect-products/ The top part is the page that is working a loop and the bottom (testimonials) are posts that are assigned to a specific category, the showposts here are set to 3 and will display excerpts of the latest three posts. This is one website I did in the past.

One other thing you may want to keep in mind is page templates, if you want one page to work one way and you want another type of page to work another you will have to look into templating.

here is the entire code for that page.

Template Name:Main Product
<?php get_header(); ?><!-- BANNER is part of main homepage only -->
    <?php if (have_posts()) : ?>
        <?php while (have_posts()): the_post(); ?>
        <?php $postName = get_the_title($post); ?>
    <!-- title link dynamic -->
        <?php edit_post_link('Edit this item','',' | '); ?>
        <?php the_content('Continue Reading'); ?>
        <?php posts_nav_link(); ?>
        <?php endwhile; ?>
        <?php else : ?>
        <div class="w3-col text">
        <h2>Not Found</h2>
        <p><?php _e("Sorry, no posts or pages could be found. Why not search for what you were trying to find?"); ?></p>
        <?php get_search_form(); ?>
        <?php endif; ?>

    <!-- insert testimonial here -->
    <section class="w3-row cl testimonials">

    <!-- make code for query based on the page, then get the top three testimonials in excerpt form  This is commented out because I did it with a function I attached to the header I will show below
        $currpage = $post -> ID;
        if($currpage == xx){
            $cat  = x;

        <h2>Testimonials for <?php echo $postName; ?></h2>  
            $currpage = $post -> ID;
            $cat = testimonial($currpage);  //here is the function 
            query_posts('showposts=3&cat=' .$cat ); ?>
        <?php while (have_posts()): the_post(); ?>
        <div class="w3-col excerpt" style="width:30%;"> 
        <?php the_excerpt(''); ?>
        <?php endwhile;
         wp_reset_query(); // DO THIS EVERYTIME YOU ALTER QUERY_POSTS


    <!-- /content float -->
<!-- /content -->

So the Function was set up like as a seperate php file so I could alter it modularly. I called the file category_help.php

/*This particular block of code is only for determining what category     of testimonial is allowed in a post. */

function testimonial($myPageID){
    //services (all cats)
    $id = "-15,-14,-13";
    if($myPageID == 18) $id = 2;
    if($myPageID == 22) $id = 4;
    if($myPageID == 20) $id = 3;
    if($myPageID == 24) $id = 5;
    if($myPageID == 26) $id = 8;
    return $id;

Then I called this in the top of header.php

<?php require 'category_help.php'; ?>