Robin
Robin

Reputation: 485

How to display other fields from custom post type and also show featured thumbnail option?

'I am able to display title and main contain from Wordpress custom post plugin i made and able to loop it with title and content but not able to get other fields to display like company and other details .

I am also not able to show featured thumbnail option although i have mentioned it on my plugin

my custom plugin

add_action('init', 'register_portfolio_post_type');
function register_portfolio_post_type(){

    $labels = array(
        'name' => ('portfolios') , 
        'singular_name' => ('portfolio'), 
        'add_new' => ('Add New') , 
        'add_new_item' => ('Add New Portfolio') ,  
        'edit_item' => ('Edit Portfolio') , 
        'new_item' => ('New Portfolio'), 
        'view_item' => ('View Portfolio'), 
        'search_items' => ('Search Portfolio'), 
        'not_found' => ('No Portfolio found'), 
        'not_found_in_trash' => ('No Portfolio found in trash'), 
        'parent_item_colon' => ('Parent Portfolio'),
        'menu_name' => ('Portfolio')
        );

    $args = array(
        'labels' => $labels,
        'hierarchical' => true,
        'description' => 'Portfolio works',
        'support' => array('title', 'editor','thumbnail', 'custom-fields','trackbacks','post-formats', 'page-attributes', 'comments'),
        'taxonomies' => array('category'),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 5,
        //'menu_icon' => '',
        'show_in_nav_menus' => true,
        'publicly_queryable' => true,
        'exclude_from_search' => false,
        'has_archieve' => true,
        'query_var' => true,
        'can_export' => true,
        'rewrite' => true,
        'capability_type' => 'post',
        'register_meta_box_cb' => 'portfolio_meta_boxes',
        );

    register_post_type( 'portfolio', $args);
}





add_filter( 'pre_get_posts', 'alphabetize_portfolios' );

function alphabetize_portfolios( $query ) {
    if ( is_post_type_archive('portfolio') ) {
        $query->set( 'orderby', 'title' );
        $query->set( 'order', 'ASC' );
        $query->set( 'nopaging', true );
    }
    return $query;
}
add_action( 'save_post', 'save_portfolio_meta_data' );
function portfolio_meta_boxes() {
    add_meta_box( 'portfolio_url_meta', __('Portfolio URL'), 'portfolio_url_meta_box', 'portfolio', 'normal', 'high' );
    add_meta_box( 'portfolio_company_meta', __('Company'), 'portfolio_company_meta_box', 'portfolio', 'normal', 'high' );
}

function portfolio_url_meta_box() {
    if ( function_exists('wp_nonce_field') ) 
        wp_nonce_field('portfolio_url_nonce', '_portfolio_url_nonce'); 
?>
    <p><label for="_portfolio_url">Portfolio URL</label> 
    <input type="text" name="_portfolio_url" 
        value="<?php echo esc_html( get_post_meta( get_the_ID(), '_portfolio_url', true ), 1 ); ?>" /></p>

<?php
}

function portfolio_company_meta_box() { 
    global $post; 
    if ( function_exists('wp_nonce_field') ) wp_nonce_field('portfolio_company_nonce', '_portfolio_company_nonce'); 
?> 
    <p><label for="_portfolio_company">Company</label> 
    <input type="text" name="_portfolio_company" 
        value="<?php echo esc_html( get_post_meta( get_the_ID(), '_portfolio_company', true ), 1 ); ?>" /></p>
    <p><label for="_portfolio_company_country">Country</label> 
    <input type="text" name="_portfolio_company_country" 
        value="<?php echo esc_html( get_post_meta( get_the_ID(), '_portfolio_company_country', true ), 1 ); ?>" /></p>


<?php
}
function save_portfolio_meta_data( $post_id ) {
    // ignore autosaves
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
        return $post_id;

    // check post type
    if ( 'portfolio' != $_POST['post_type'] )
        return $post_id;

    // check capabilites
    if ( 'portfolio' == $_POST['post_type'] && !current_user_can( 'edit_post', $post_id ) )
        return $post_id;

    // check nonces
    check_admin_referer( 'portfolio_url_nonce', '_portfolio_url_nonce' );
    check_admin_referer( 'portfolio_company_nonce', '_portfolio_company_nonce' );

    // Still here? Then save the fields
    if ( empty( $_POST['_portfolio_url'] ) ) {
        $storedcode = get_post_meta( $post_id, '_portfolio_url', true );
        delete_post_meta( $post_id, '_portfolio_url', $storedurl );
    }
    else 
        update_post_meta( $post_id, '_portfolio_url', $_POST['_portfolio_url'] );

    if ( empty( $_POST['_portfolio_company'] ) ) {
        $storedname = get_post_meta( $post_id, '_portfolio_company', true );
        delete_post_meta( $post_id, '_portfolio_company', $storedcompany );
    }
    else 
        update_post_meta( $post_id, '_portfolio_company', $_POST['_portfolio_company'] );

    if ( empty( $_POST['_portfolio_company_country'] ) ) {
        $storedemail = get_post_meta( $post_id, '_portfolio_company_country', true );
        delete_post_meta( $post_id, '_portfolio_company_country', $storedcountry );
    }
    else 
        update_post_meta( $post_id, '_portfolio_company_country', $_POST['_portfolio_company_country'] );



}
?>

**single-portfolio.php**

<div class="row portfolio">
          <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 portfoliobox">
            <div class="thumbnail">
            <a href="#">
            <img src="<?php echo get_template_directory_uri(); ?>/img/responsive-webness.png" class="img-responsive" alt="" />
            </a>

            <p><?php the_title(); ?></p>
            </div><!-- .thumbnail -->
          </div>

          <div class="col-lg-8 col-md-8 col-sm-6 col-xs-12 ">

            <div class="thumbnail">
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <p><?php the_content(); ?></p>
        <?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
            <p><?php the_title(); ?></p>
            </div><!-- .thumbnail -->
          </div>

page-portfolio.php

<div class="row portfolio">
          <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 portfoliobox">
            <div class="thumbnail">
            <a href="#">
            <img src="<?php echo get_template_directory_uri(); ?>/img/responsive-webness.png" class="img-responsive" alt="" />
            </a>

            <p><?php the_title(); ?></p>
            </div><!-- .thumbnail -->
          </div>

          <div class="col-lg-8 col-md-8 col-sm-6 col-xs-12 ">

            <div class="thumbnail">
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <p><?php the_content(); ?></p>
        <?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
            <p><?php the_title(); ?></p>
            </div><!-- .thumbnail -->
          </div>

single-portfolio.php

<div class="row portfolio">
          <div class="col-lg-4 col-md-4 col-sm-6 col-xs-12 portfoliobox">
            <div class="thumbnail">
            <a href="#">
            <img src="<?php echo get_template_directory_uri(); ?>/img/responsive-webness.png" class="img-responsive" alt="" />
            </a>

            <p><?php the_title(); ?></p>
            </div><!-- .thumbnail -->
          </div>

          <div class="col-lg-8 col-md-8 col-sm-6 col-xs-12 ">

            <div class="thumbnail">
      <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
        <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
        <p><?php the_content(); ?></p>
        <?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
            <p><?php the_title(); ?></p>
            </div><!-- .thumbnail -->
          </div>

Upvotes: 0

Views: 150

Answers (2)

Robin
Robin

Reputation: 485

I found the right way to do it. It solved my problem. to get other fields to display like company url

<p>Website: <?php echo get_post_meta($post->ID, '_portfolio_url', true); ?></p>

to show featured thumbnail option

'support' => array('title', 'editor','thumbnail', 'custom-fields','trackbacks','post-formats', 'page-attributes', 'comments'),

should be

'supports' => array('title', 'editor','thumbnail', 'custom-fields','trackbacks','post-formats', 'page-attributes', 'comments'),

mistake was support which should be supports.

Upvotes: 0

mginn
mginn

Reputation: 16114

Have you looked at this documentation? It looks like you need to use the_meta() or another one of the supported tags.

Upvotes: 0

Related Questions