$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>loop - get_post_type is always post|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

loop - get_post_type is always post

matteradmin11PV0评论

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() ) {
...

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far