I am trying to get the post>ID in a shortcode but it’s a little tricky for how I need it.
I am building a one page WP site, and if I do
return get_the_ID();
It displays 1 which is correct, but I need to pull in meta box data from items in CPT’s on the site.
I have the following:
global $post;
ob_start();
extract(
shortcode_atts(
array (
'type' => 'fhpartners',
'posts' => 5,
), $atts5
)
);
$options5 = array(
'post_type' => $type,
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => $posts,
);
$part_img = get_post_meta(get_the_ID(), '_cmb_partner_image', true);
$part_url = get_post_meta(get_the_ID(), '_cmb_partner_url', true);
$query5 = new WP_Query( $options5);
echo '';
if ( $query5->have_posts() ) {
while ( $query5->have_posts() ) : $query5->the_post();
if($url !== '') {
echo '
';
}
echo '
';
if($part_url !== '') {
echo '';
}
endwhile;
wp_reset_postdata();
Is there a way to get the IDs of the individual items in the CPT?
We have used this before and it worked fine for us:
global $post;
$args = array(
'post_type' => 'ublportfolio',
'posts_per_page' => 20
);
$relatedquery = new WP_Query( $relatedargs );
while($relatedquery->have_posts()){
$relatedquery->the_post();
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'medium', false);
$list .= '';
} wp_reset_postdata();
As you can see we have used $post->ID
as above, displays 1, which isn’t what I need, I need the individual CPT item ID’s which are 70, 72, 74 etc
UXBARN
4
Do you mean to get the CPT IDs for this code?:
$part_img = get_post_meta(get_the_ID(), '_cmb_partner_image', true);
$part_url = get_post_meta(get_the_ID(), '_cmb_partner_url', true);
If so, try putting the code inside the loop, below the following line:
while ( $query5->have_posts() ) : $query5->the_post();
arghhhhhhhhhh something so simple… I need a break haha
Here you go. I fixed a lot of the rest of your code for you too.
function my_shortcode( $attr ) {
$attr = shortcode_atts(
array(
'type' => 'fhpartners',
'posts' => 5,
),
$attr,
'shortcode-name'
);
$part_img = get_post_meta( get_the_ID(), '_cmb_partner_image', true );
$part_url = get_post_meta( get_the_ID(), '_cmb_parner_url', true );
$loop = new WP_Query(
array(
'post_type' => $attr['type'],,
'order' => 'ASC',
'orderby' => 'menu_order',
'posts_per_page' => $attr['posts'],
)
);
$out = '';
if ( $loop->have_posts() ) {
$out .= '<div class="row-fluid ParnersList">';
while ( $loop->have_posts() ) {
$loop->the_post();
/* This what you want. */
$current_post_id = get_the_ID();
$image = sprintf( '<img src="%s" alt="logo" />', esc_url( $part_img ) );
if ( !empty( $part_url ) )
$out .= sprintf( '<a href="%s" target="_blank">%s</a>', esc_url( $part_url ), $image );
else
$out .= $image;
} // endwhile
$out .= '</div>';
} // endif
wp_reset_postdata();
return $out;
}