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

Modify default Related Posts Code to Custom Post Type

programmeradmin1浏览0评论

I'd like to modify this basic related posts code to my custom post type "Videos". Since I'm new to this stuff, can someone help me understand what I need to modify in this code so that it pull related content from my custom post type Videos?

Code in question:

<?php
// Get the current post ID
$post_id = get_the_ID();

// Get the post category
$cats = wp_get_post_categories($post_id);

if ($cats) {

?> 
          <h4>Related Posts</h4>

               <div id="posts-container" style="display: flex; align-items: center; justify-content: center;">

        <?php

    // Get the category ID
    $cat_id = $cats[0];

    // Build the query
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => '4',
        'cat' => $cat_id
    );


    $query = new WP_Query($args);

    $posts = $query->posts;

    // Loop through the posts
    foreach ($posts as $post) {

        // Exclude the current post
        if ($post->ID != $post_id) {

?>

        <a class="ct-link-text" href="<?php
            echo get_the_permalink($post->ID);
?>" target="_self" style="padding: 5px;">
                    <div style="width: 300px; height: 300px; align-items: center; text-align: center; justify-content: center; 
            background-repeat: no-repeat; background-position: 50% 50%;background-image: linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)), url(<?php
            echo get_the_post_thumbnail_url($post->ID);
?>);   ;background-size: auto;" class="ct-div-block">
              <h4 style="color: #fff; font-size: 20px;"><?php
            echo get_the_title($post->ID);
?></h4>
            </div>
            </a>


        <?php

        }

    }

    wp_reset_query();

?></div><?php

}

?>

I'd like to modify this basic related posts code to my custom post type "Videos". Since I'm new to this stuff, can someone help me understand what I need to modify in this code so that it pull related content from my custom post type Videos?

Code in question:

<?php
// Get the current post ID
$post_id = get_the_ID();

// Get the post category
$cats = wp_get_post_categories($post_id);

if ($cats) {

?> 
          <h4>Related Posts</h4>

               <div id="posts-container" style="display: flex; align-items: center; justify-content: center;">

        <?php

    // Get the category ID
    $cat_id = $cats[0];

    // Build the query
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => '4',
        'cat' => $cat_id
    );


    $query = new WP_Query($args);

    $posts = $query->posts;

    // Loop through the posts
    foreach ($posts as $post) {

        // Exclude the current post
        if ($post->ID != $post_id) {

?>

        <a class="ct-link-text" href="<?php
            echo get_the_permalink($post->ID);
?>" target="_self" style="padding: 5px;">
                    <div style="width: 300px; height: 300px; align-items: center; text-align: center; justify-content: center; 
            background-repeat: no-repeat; background-position: 50% 50%;background-image: linear-gradient(rgba(0,0,0,0.4), rgba(0,0,0,0.4)), url(<?php
            echo get_the_post_thumbnail_url($post->ID);
?>);   ;background-size: auto;" class="ct-div-block">
              <h4 style="color: #fff; font-size: 20px;"><?php
            echo get_the_title($post->ID);
?></h4>
            </div>
            </a>


        <?php

        }

    }

    wp_reset_query();

?></div><?php

}

?>
Share Improve this question asked Mar 15, 2020 at 17:22 user5248user5248 1111 bronze badge 1
  • developer.wordpress/reference/classes/wp_query/… – Michael Commented Mar 15, 2020 at 19:09
Add a comment  | 

1 Answer 1

Reset to default 0

Assuming your CPT is 'videos' you'd use:

// Build the query
    $args = array(
        'post_type' => 'videos',
        'posts_per_page' => '4',
        'cat' => $cat_id
    );
发布评论

评论列表(0)

  1. 暂无评论