I have a WordPress project im doing, pretty much I need to add 4 individual blog posts with background images behind the specific posts. This is the code i found and used, the only issue is they blog posts aren't clickable, not even the title. I am not sure where to go from here to make each link clickable.
By <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y'); ?> in <?php the_category(', '); ?>
<?php
$post_id = 1;
$queried_post = get_post($post_id); ?>
<h2><?php echo $queried_post->post_title; ?></h2>
<?php echo $queried_post->post_content; ?>
I have a WordPress project im doing, pretty much I need to add 4 individual blog posts with background images behind the specific posts. This is the code i found and used, the only issue is they blog posts aren't clickable, not even the title. I am not sure where to go from here to make each link clickable.
By <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y'); ?> in <?php the_category(', '); ?>
<?php
$post_id = 1;
$queried_post = get_post($post_id); ?>
<h2><?php echo $queried_post->post_title; ?></h2>
<?php echo $queried_post->post_content; ?>
Share
Improve this question
asked Feb 26, 2021 at 16:02
MuscularItalianMuscularItalian
91 bronze badge
2 Answers
Reset to default 0They're not clickable because that output doesn't include any of the post permalinks. You can try this:
By <?php the_author_posts_link(); ?> on <?php the_time('F jS, Y'); ?> in <?php the_category(', '); ?>
<?php
$post_id = 1;
$queried_post = get_post($post_id); ?>
<h2><a href="<?php echo esc_url( get_permalink( $post_id ) ); ?>"><?php echo $queried_post->post_title; ?></a></h2>
<?php echo $queried_post->post_content; ?>
That should get the title working as a link for you.
To explain, you have set the post ID using $post_id = 1
, so using get_permalink( $post_id );
you can get the URL for the entry. Inside your <h2>
you just add an <a href="">
and wrap the title with it.
Hope that helps.
I need to add 4 individual blog posts
If you want to add 4 posts in one place, the best method is to use WP_Query
. To specify posts you can use post__in
.
You can add post ids in the $post_ids
array.
$post_ids = array(1, 2);
$args = array(
'post_type' => 'post',
'post__in' => $post_ids
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
By <?php the_author(); ?> on <?php the_time('F jS, Y'); ?> in <?php the_category(', '); ?>
<h2><a href="<?php the_permalink(); ?>"><?php the_title; ?></a></h2>
<p><?php the_content; ?></p>
<?php
endwhile;
wp_reset_postdata();
endif;