Morning everyone, I’m sorry to disturb, but I need help if possible.
The code below I need to publish part of the contents of some pages.
Firstly I have a problem with “the_permalink” despite write the correct address, this is where he wants (see image). Puts for example before H2 and however outside the tag A.
Secondly I would like to filter out and ONLY a few pages with specific ID but have not been able to find a solution. I tried to put it but it does not work.
new WP_Query( 'post_type=page&include=30,60' );
Code that I used
<?php
// Lista Pagine con the_excerpt e the_post_thumbnail
// Query Pages
$my_query = new WP_Query( 'post_type=page' );
// The Loop
while ( $my_query->have_posts() ) :
$my_query->the_post();
echo '
<h2>' . get_the_title() . '</h2>
<p>' . get_the_excerpt() . '</p>
<p>' . get_the_post_thumbnail() . '</p>
<a href="' . the_permalink() . '">' . get_the_title() . '</a>';
endwhile;
wp_reset_query();
?>
Thanks in advance.
the_permalink()
will echo the content immediately. You can’t use it for string concatenation. What is happening is that your permalink getsecho
ed bythe_permalink()
before the string is finished building, so the permalink ends up in the wrong place.What you need instead is
get_the_permalink()
.Side note: Because PHP’s
echo
will take multiple parameters, separating your strings with a comma (argument delimiter) rather that a period (concatenation operator) should also work:If you do it that way, every component of the string
echo
s immediately. You are never concatenating a string so things never get out of order.As far as excluding pages, you want
posts__in
notinclude
but that requires, or limits the query to, the specified post IDs. To exclude, you wantposts__not_in
. And don’t use the “query var” syntax. It will trip you up. Use an array like this from example from the Codex: