0

Hi everyone,

I am starting now to develoo my first wordpress Theme and please, I need your help with code.

With this code I created my post type Team, the problem is to show them, I do not know how to do it. If it's possible, I wish to create some easy option for users in Bakery page builder to add it in a row with the option for slider. How can I do it ?

Thanks for any information

function create_post_type() {

    register_post_type( 'Team',
        array(
        'labels' => array(
            'name' => __( 'Team' ),
            'singular_name' => __( 'Team' ),
            'add_new' => __( 'Add Member' ),
            'add_new_item' => __( 'Add New Member' ),
            'featured_image' => __( 'Add Photo' ),
            'edit_item' => __( 'Edit Member' )
        ),
        'public' => true,
        'menu_icon' => 'dashicons-universal-access',
        'has_archive' => true,
        'rewrite' => array('slug' => 'Team'),
        'supports' => array('title', 'editor','custom-fields', 'thumbnail'),
        )
    );
}
add_action( 'init', 'create_post_type' );
4
  • Where you want to display this post_type entries? Jul 3 '20 at 12:58
  • In the home page for example, but with option for user put it anywhere.
    – Alander
    Jul 3 '20 at 17:53
  • You can create a shortcode for it. And can call anywhere on the website, you just need to call it. I'll share more detail in comment section. Jul 4 '20 at 18:03
  • Check Answer section. Jul 4 '20 at 18:32
1

As you said in a comment that you want to make it dynamic and wants to call anywhere of the website. Then in WP the best option is to use the shortcode. You can create a short code and call it where ever you want. One thing you can update it's design as per your need, I am sharing a way to create a short code for the CPT.

/**
 * Register all shortcodes
 *
 * @return null
 */
function register_shortcodes() {
    add_shortcode( 'produtos', 'shortcode_mostra_produtos' );
}
add_action( 'init', 'register_shortcodes' );
/**
 * Produtos Shortcode Callback
 * 
 * @param Array $atts
 *
 * @return string
 */
function shortcode_mostra_produtos( $atts ) {
    global $wp_query,$post;
    $atts = shortcode_atts( array(
        'line' => ''
    ), $atts );

    $loop = new WP_Query( array(
        'posts_per_page'    => 200,
        'post_type'         => 'produtos',
        'orderby'           => 'menu_order title',
        'order'             => 'ASC',
        'tax_query'         => array( array(
            'taxonomy'  => 'linhas',
            'field'     => 'slug',
            'terms'     => array( sanitize_title( $atts['line'] ) )
        ) )
    ) );

    if( ! $loop->have_posts() ) {
        return false;
    }

    while( $loop->have_posts() ) {
        $loop->the_post();
        echo the_title();
    }

    wp_reset_postdata();
}

I have created a shortcode for the products to display the product list.

Now you can add [produtos] on the wp backend page/post and after saving this will display the produts title (as i only echo the title) on the front end.

For more reference about the shortcode API you can follow this article SHORTCODE API

Try this. I think this will help you. Let me know if you will face any issue here.

Enjoy!!:)

2
  • Thank you so much Arpita. I am going to do it !
    – Alander
    Jul 6 '20 at 16:42
  • Let me know Alander, If you face any issue. Jul 7 '20 at 13:34
0

You can use WP_Query or get_posts to achieve the result. Here I'm providing you an example using WP_Query of how you can achieve what you need.

$args = array(  
    'post_type' => 'Team',
    'post_status' => 'publish',
    'posts_per_page' => -1, // -1 will return all entry
    'orderby' => 'title', 
    'order' => 'ASC',
);

$loop = new WP_Query( $args ); 

while ( $loop->have_posts() ) : $loop->the_post(); 
    $featured_img = wp_get_attachment_image_src( $post->ID );
    echo the_title(); // Print the title
    if ( $feature_img ) {
       //Print image if available
       echo '<img src="'.$featured_img['url'].'" width="'.$featured_img['width'].'" height="'.$featured_img['height'].'" />';
    }
    the_excerpt(); 
endwhile;

wp_reset_postdata(); 

You can study more about WP_Query here. Also read about get_posts() here. Have fun!

1
  • Thank you Sabbir. I will study what you said. Regards
    – Alander
    Jul 3 '20 at 17:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.