-1

Only that Read More issue is remaining.

<?php
            while(have_posts()): the_post();

         echo '<h2>'.get_the_title().'</h2>';
        echo  get_the_post_thumbnail(get_the_ID(),'medium');
        the_excerpt();
        echo "<br>";
        <a class="button" href="<?php the_permalink(); ?>">Read More</a>
        echo "<hr>";
        endwhile;
        ?>
2

EDIT

Based on your last concern, here how I would do this.

The only problem left on your code is that you are calling some HTML inside PHP without `echo. So I split all those into seperates HTML tags and called them separatly in PHP.

<?php while(have_posts()): the_post(); ?>

        <h2><?php the_title(); ?></h2>

        <figure class="thubmnail">
            <?php the_post_thumbnail(get_the_ID(), 'medium'); ?>
        </figure>

        <p><?php the_excerpt(); ?></p>

        <br/>

        <a href="<?php the_permalink(); ?>" class="button">Read more</a>

        <hr/>

    <?php endwhile; ?>

You should really look at the WordPress Codex to understand how and when using those functions as your question is not clear.

Link to the documentation : https://codex.wordpress.org/Class_Reference/WP_Query#Usage

In your PHP file or Template file, you should have something like this (loop) to be able to show the value of get_the_title()and the_permalink()or it will display the parent page values.

<?php 
// the query
$the_query = new WP_Query( $args ); ?>

<?php if ( $the_query->have_posts() ) : ?>

    <!-- pagination here -->

    <!-- the loop -->
    <?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <h2><?php the_title(); ?></h2>
        <a href="<?php the_permalink(); ?>">Read more</a>
    <?php endwhile; ?>
    <!-- end of the loop -->

    <!-- pagination here -->

    <?php wp_reset_postdata(); ?>

<?php else : ?>
    <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

As for your second issue, the_title() does not need to be in an <?php echo ... ?>. The function will echo itself. https://codex.wordpress.org/Function_Reference/the_title

Displays or returns the unescaped title of the current post. This tag may only be used within The Loop, to get the title of a post outside of the loop use get_the_title. If the post is protected or private, this will be noted by the words "Protected: " or "Private: " prepended to the title

Hope it help you a little bit to clearly understand the concept.

If unclear, please update your question.

  • you totally misunderstood my questions. let solve 2nd question first. i know the echo is needed when its get_the_title. my question is different. i just want to know how to get output if its inside php tag like this. <?php '<h2>'.the_title().'</h2>'; ?> – TheDose Dec 2 '17 at 17:22
  • <?php echo '<h2>' . get_the_title() . '</h2>'; ?> – Patrice Poliquin Dec 2 '17 at 17:25
  • That's ok But why this is not working: <?php while(have_posts()): the_post(); '<h2>'.the_title().'</h2>'; the_excerpt(); ?> – TheDose Dec 2 '17 at 17:35
  • Becauce you can not just write '<h2>' ... '</h2>' inside PHP. You need to echo this. Or you do , <?php echo '<h2>'; the_title(); echo '</h2>'; ?> OR you do <?php echo '<h2>'.get_the_title().'</h2>; ?> – Patrice Poliquin Dec 2 '17 at 17:36
  • Thanks got it. now come to first issue. why the reason its not working. its working outside php tags. but within php tags its not working. <?php <a class="button" href="<?php the_permalink(); ?>">Read More</a> ?> – TheDose Dec 2 '17 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.