Custom Post Type on profile pages

Hello,

I want to insert my Custom Post Type (that has 1 taxonomy at the moment) on a profile page of the "Ultimate Member" Plugin. How can I show the CPTs in general? I can copy and paste shortcodes for Custom Fields, but I didn´t find codes for the complete post types.

I would either paste a shortcode inside the profile-template in the WP-Backend, or paste the needed php-code for the CPT in the profile.php of the plugin.

Can you tell me how I can solve this problem?

Greetings,
Marvin

  • Predrag Dubajic

    Hey Marvin,

    Hope you're doing well today :slight_smile:

    You can use this code bellow to create new shortcode that will show posts from your custom post type:

    // Add Shortcode
    function custom_shortcode() {
    
    	$args = array( 'post_type' => 'my_post_type', );
    	$loop = new WP_Query( $args );
    	while ( $loop->have_posts() ) : $loop->the_post();
    		the_title();
    		echo '<div class="entry-content">';
    		the_content();
    		echo '</div>';
    	endwhile;
    }
    add_shortcode( 'query_my_posts', 'custom_shortcode' );

    You will need to change my_post_type from the code to the name of your post type and add this code to your themes functions.php file. It is best to use child theme for these changes, that way your code will still be there after theme update.
    You can also use mu-plugins but you will need to add <?php before the code I posted.

    Once you have saved those changes you can add this shortocode in page/post content:
    [query_my_posts]

    Hope this helps.

    Best regards,
    Predrag

    • Marvin

      Hello Predrag,

      thanks for the quick reply. This really helps.

      But it shows the complete content of all the Contents I created with this Post-Type.

      I´d like to have a list of links like this:
      "Projekt 1"
      "Projekt 2"
      ... etc.

      With just clickable headlings (links) and maybe a set post image.
      Then, when people click that link, they should come to the single page for that project.

      Would I have to create a template, so it creates a "single-projekt.php" and then edit and format it?

  • Predrag Dubajic

    Hi @Medienpark,

    My apologies for the long overdue on this thread.

    The code above was an example what you can do, you will need to make some adjustments depending on what you want to show in loop.
    Here is a bit modified code that should show your featured image and title, without the content:

    // Add Shortcode
    function custom_shortcode() {
    
    	$args = array( 'post_type' => 'my_post_type', );
    	$loop = new WP_Query( $args );
    	while ( $loop->have_posts() ) : $loop->the_post();
    		the_post_thumbnail();
    		echo '<a href="' . the_permalink() . '">' . the_title() . '</a>';
    	endwhile;
    
    }
    add_shortcode( 'query_my_posts', 'custom_shortcode' );

    Best regards,
    Predrag

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.