Firstly, you should never use query_posts
to construct custom queries. This is not just my emphasis, but the codex as well. The one big problem with query_posts
is, it many circumstances, pagination fails
Note: This function isn’t meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
You need to construct you custom query with WP_Query
.
Take a look at the_title( $before, $after, $echo );
. Here are the parameters
$before
(string) (optional) Text to place before the title.
Default: None
$after
(string) (optional) Text to place after the title.
Default: None
$echo
- (Boolean) (optional) Display the title (TRUE) or return it for use in PHP (FALSE).
- Default: TRUE
So you can simply just do something like this
the_title( '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a>' );
You query should then look like this with all the before said. Before I forget, showposts
has long time been depreciated, it was replaced with posts_per_page
. If you had debug set to true in wp-config, you should have received a depreciation notice in the front end that would have indicated this. Read here: Debugging WordPress
<?php
$args = array(
'cat' => 4,
'posts_per_page' => 1
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title( '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a>' );
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();