最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Show a specific post in wordpress with links

programmeradmin1浏览0评论

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
Add a comment  | 

2 Answers 2

Reset to default 0

They'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;
发布评论

评论列表(0)

  1. 暂无评论