• Hello guys! I’ve been playing a bit with PHP and WP code to display a definite number of post from particular categories in index.php. And this is what I got so far:

    <?php $args = array(
    	'cat' => 10,
    	'posts_per_page' => 3
    );
    $query = new WP_Query( $args );
    while($query->have_posts()):
    	$query->next_post();
    	$id = $query->post->ID;
    	echo '<h2>';
    	echo '<a href="'. the_permalink() .'">'. get_the_title($id) .'</a>';
    	echo '</h2>';
    	echo the_excerpt();
    endwhile;
    ?>

    Everything seems to be working fine, except for a little detail related to the post title and it’s link to the full post. So this is what I got in return when the PHP is processed:

    <div id="noticia">
        <h2>https://www.url.com/post-slug<a href="?preview=1&template=la97&stylesheet=la97">Post Title</a></h2>A lot of text representing the excerpt<a href="https://www.url.com/post-slug?preview=1&template=la97&stylesheet=la97">[...]</a>
    </div>

    As you can see, I cannot find the way to get the post link in the proper place. It’s being displayed outside of the link tags, so it generates a not-working link instead.

    Any ideas? :S

Viewing 1 replies (of 1 total)
  • Ok, I got it solved on my own! This is how it goes:

    <div id="noticia">
        <?php $args = array(
            'cat' => 10,
            'posts_per_page' => 3
        );
        $query = new WP_Query( $args );
            while($query->have_posts()):
                $query->next_post();
                $id = $query->post->ID;
                echo '<h2>';
                echo '<a href="'. get_permalink($id) .'">'. get_the_title($id) .'</a>';
                echo '</h2>';
                echo get_the_excerpt($id);
            endwhile;
        ?>

    I didn’t realize that “echo” and “the_permalink()” were both trying to print information. When I realized that I just replaced “the_permalink()” for “get_permalink($id)” (the $id between brackets is very important too), so it call the information leaving the printing part to “echo” ??

    Now I have another issue. “get_the_excerpt()” is getting the excerpt from the most recent article instead of using the current article :S

    Any ideas please?

Viewing 1 replies (of 1 total)
  • The topic ‘the_permalink little problem’ is closed to new replies.