Video walkthrough
<?php
add_shortcode(
'latest-agenda-document',
function(){
ob_start();
$posts = get_posts([
'post_type' => 'document',
'posts_per_page' => 1,
'orderby' => 'date',
'order' => 'DESC',
'tax_query' => [
[
'taxonomy' => 'document_category',
'field' => 'slug',
'terms' => 'agenda',
],
],
]);
printf(
'<p><a href="%s">%s</a></p>',
get_permalink( $posts[0]->ID ),
get_the_title( $posts[0]->ID )
);
return ob_get_clean();
}
);
Thread Starter
Sarah H
(@sarahrbcom)
Thank you so much for the response Paul, the video walkthrough is very helpful!
I’ve added this to the functions.php of my child theme and tested it by adding it to a few blog posts, but it seems to be returning the title and permalink of whatever post its in. For example:
http://ncrcd.rb-com.com/uncategorized/hello-world/
http://ncrcd.rb-com.com/uncategorized/another-sample-post/
I’ve checked the code and it all matches, could there be something in my POD setup that’s creating this? Any thoughts? Much appreciated.
–Sarah
Hi @sarahrbcom
I cannot access the links you’ve posted.
The code Paul posted should work fine as it specifically targets the document
post type, not other post types.
Cheers, Jory
Thread Starter
Sarah H
(@sarahrbcom)
Hi Jory,
Thanks for the response. Can you be more specific as to why you can’t access the links? Do you see an error message? The site is discouraging search engines but is otherwise entirely public. I can open the links in an incognito window, so logins and cookies are not an issue. It currently has no SSL, is that the issue for you?
I understand how the code targets the document
CPT, which is why I’m confused as to the results I’m seeing.
Can you view this screenshot? https://www.dropbox.com/s/zkdwx7mrofch3ie/shortcode-test-1.png?dl=0
I added class="shortcode-test"
to the <p>
tag to confirm that the shortcode was indeed printing that line, but the string it’s returning does not seem to be the correct permalink and title.
Thread Starter
Sarah H
(@sarahrbcom)
Came back to this with fresh eyes and picked it apart, and discovered that the code above uses agenda
as the term when it’s actually agendas
. With that change it works now! I’ve styled it as a button with generic text and it’s working great now. I’m sure this will be useful in the future for other projects, thanks again.