loop - get_post_type is always post

admin2025-06-02  4

Sorry to bother you all with this, but it's driving me nuts and I know I've missed something obvious.

I want to display different post types, differently, within the loop. I'm trying to get the post type with get_post_type() and using an if. The trouble is, all I'm getting returned is Post. Every post returns as Post. I'm not really doing anything special in the way of custom post types, just Video and Standard.

Here's my loop:

 <?php
    get_header();

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 

    printf( __( 'The post type is: %s', 'textdomain' ), get_post_type($post->ID) );

        if ( 'Video' == get_post_type() ) { ?>

        <div id="the_post">
        <div id="post_stuff">
        <div id="the_category"><?php the_category(); ?></div>
        <div id="the_title"><?php the_title(); ?></div>
        <div id="the_excerpt"><?php the_excerpt(); ?></div>

        <div id="the_links">
        <?php $youtubelink = get_post_meta( $post->ID, 'youtube_link', true ); ?>
        <?php $youtubeembed = get_post_meta( $post->ID, 'youtube_embed', true ); ?>

        <?php
        if ($youtubeembed) { ?>
        <div id="front_youtube_embed"><?php $post_meta_value = get_post_meta( $post->ID, 'youtube_embed', true ); print ($post_meta_value); ?></div>
        <?php } ?>

        <?php
        if ($youtubelink) { ?>
        <div id="front_youtube_button"><a href=" <?php print ($youtubelink); ?> ">YouTube</a></div>
        <?php } ?>

        </div>

        </div>
        </div>

    <?php } else { ?>

        <div id="the_post">
        <div id="the_thumbnail"><?php the_post_thumbnail(); ?></div>

        <div id="post_stuff">
        <div id="the_category"><?php the_category(); ?></div>
        <div id="the_title"><?php the_title(); ?></div>
        <div id="the_excerpt"><?php the_excerpt();?></div>

        <div id="the_links">
        <?php $spreakerlink = get_post_meta( $post->ID, 'spreaker_link', true ); ?>
        <?php $soundcloudlink = get_post_meta( $post->ID, 'soundcloud_link', true ); ?>
        <?php $youtubelink = get_post_meta( $post->ID, 'youtube_link', true ); ?>

        <?php
        if ($spreakerlink) { ?>
        <div id="front_spreaker_button"><a href=" <?php print ($spreakerlink); ?> ">Spreaker</a></div>
        <?php } ?>

        <?php
        if ($soundcloudlink) { ?>
        <div id="front_soundcloud_button"><a href=" <?php print ($soundcloudlink); ?> ">Soundcloud</a></div>
        <?php } ?>

        <?php
        if ($youtubelink) { ?>
        <div id="front_youtube_button"><a href=" <?php print ($youtubelink); ?> ">YouTube</a></div>
        <?php } ?>

        </div>

        </div>
        </div>
        <?php  
    }

    endwhile; else : ?>
    <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<?php get_footer(); ?>

and here's my functions.php just in case:

<?php


if ( ! function_exists( 'theme_setup' ) ) :

function theme_setup() {

    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'post-thumbnails' );
    register_nav_menus( array(
        'primary'   => __( 'Primary Menu', 'myfirsttheme' )
    ) );

    add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) );
}


endif;
add_action( 'after_setup_theme', 'theme_setup' );

function my_style() {
    wp_enqueue_style(  'style', get_stylesheet_uri()); 
}

add_action( 'wp_enqueue_scripts', 'my_style' );


?>

Sorry to bother you all with this, but it's driving me nuts and I know I've missed something obvious.

I want to display different post types, differently, within the loop. I'm trying to get the post type with get_post_type() and using an if. The trouble is, all I'm getting returned is Post. Every post returns as Post. I'm not really doing anything special in the way of custom post types, just Video and Standard.

Here's my loop:

 <?php
    get_header();

    if ( have_posts() ) : while ( have_posts() ) : the_post(); 

    printf( __( 'The post type is: %s', 'textdomain' ), get_post_type($post->ID) );

        if ( 'Video' == get_post_type() ) { ?>

        <div id="the_post">
        <div id="post_stuff">
        <div id="the_category"><?php the_category(); ?></div>
        <div id="the_title"><?php the_title(); ?></div>
        <div id="the_excerpt"><?php the_excerpt(); ?></div>

        <div id="the_links">
        <?php $youtubelink = get_post_meta( $post->ID, 'youtube_link', true ); ?>
        <?php $youtubeembed = get_post_meta( $post->ID, 'youtube_embed', true ); ?>

        <?php
        if ($youtubeembed) { ?>
        <div id="front_youtube_embed"><?php $post_meta_value = get_post_meta( $post->ID, 'youtube_embed', true ); print ($post_meta_value); ?></div>
        <?php } ?>

        <?php
        if ($youtubelink) { ?>
        <div id="front_youtube_button"><a href=" <?php print ($youtubelink); ?> ">YouTube</a></div>
        <?php } ?>

        </div>

        </div>
        </div>

    <?php } else { ?>

        <div id="the_post">
        <div id="the_thumbnail"><?php the_post_thumbnail(); ?></div>

        <div id="post_stuff">
        <div id="the_category"><?php the_category(); ?></div>
        <div id="the_title"><?php the_title(); ?></div>
        <div id="the_excerpt"><?php the_excerpt();?></div>

        <div id="the_links">
        <?php $spreakerlink = get_post_meta( $post->ID, 'spreaker_link', true ); ?>
        <?php $soundcloudlink = get_post_meta( $post->ID, 'soundcloud_link', true ); ?>
        <?php $youtubelink = get_post_meta( $post->ID, 'youtube_link', true ); ?>

        <?php
        if ($spreakerlink) { ?>
        <div id="front_spreaker_button"><a href=" <?php print ($spreakerlink); ?> ">Spreaker</a></div>
        <?php } ?>

        <?php
        if ($soundcloudlink) { ?>
        <div id="front_soundcloud_button"><a href=" <?php print ($soundcloudlink); ?> ">Soundcloud</a></div>
        <?php } ?>

        <?php
        if ($youtubelink) { ?>
        <div id="front_youtube_button"><a href=" <?php print ($youtubelink); ?> ">YouTube</a></div>
        <?php } ?>

        </div>

        </div>
        </div>
        <?php  
    }

    endwhile; else : ?>
    <p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

<?php get_footer(); ?>

and here's my functions.php just in case:

<?php


if ( ! function_exists( 'theme_setup' ) ) :

function theme_setup() {

    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'post-thumbnails' );
    register_nav_menus( array(
        'primary'   => __( 'Primary Menu', 'myfirsttheme' )
    ) );

    add_theme_support( 'post-formats', array ( 'aside', 'gallery', 'quote', 'image', 'video' ) );
}


endif;
add_action( 'after_setup_theme', 'theme_setup' );

function my_style() {
    wp_enqueue_style(  'style', get_stylesheet_uri()); 
}

add_action( 'wp_enqueue_scripts', 'my_style' );


?>
Share asked Feb 22, 2019 at 18:41 Richard SmithRichard Smith 256 bronze badges
 | 

1 Answer 1

Reset to default 2

Post type and post format are different. Try get_post_format().

In your code, this would be:

if ( 'video' === get_post_format() ) {
...
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748875909a314451.html

最新回复(0)