Ecommerce Shopify WordPress Discussion

menu item linked to the most recent element of a cpt

I work with wordpress and the Divi theme. I have a CPT named Publications. I will have one item of this CPT par year (from 2013). "Publications" is one item of the principal menu. Is there a solution to build a link to the last item of the publications's CPT ? Caroline
Use a custom query to retrieve the latest publication from your CPT. You can use the WP_Query class for this purpose. $args = array( 'post_type' => 'publications', 'posts_per_page' => 1, 'orderby' => 'date', 'order' => 'DESC', ); $latest_publication = new WP_Query($args); if ($latest_publication->have_posts()) { while ($latest_publication->have_posts()) { $latest_publication->the_post(); // You are now inside the loop for the latest publication. // You can retrieve information or customize the link here. } } wp_reset_postdata(); // Reset post data after the custom query. You can use the get_permalink() function to get the permalink of the latest publication and create a link. Integrate into WordPress Menu: To integrate this into your WordPress menu, you might need to use a custom link. You can add a custom link to your WordPress menu and use a placeholder URL (e.g., #latest-publication). Then, use JavaScript or jQuery to update the link dynamically on the frontend. <script> // Add this script to your theme (you might need to enqueue it properly). document.addEventListener("DOMContentLoaded", function () { // Assuming you've used the URL placeholder #latest-publication. var latestPublicationLink = document.querySelector('a[href="#latest- publication"]'); // Update the href attribute with the actual link. latestPublicationLink.href = "<?php echo esc_url($latest_publication_permalink); ?>"; }); </script> Make sure to enqueue the script properly in your theme's functions.php file or use a dedicated script file. This approach allows you to dynamically generate the link to the latest publication and integrate it into your WordPress menu. Adjust the code according to your theme structure and requirements.

February 26, 2024

TurboCommerce make the better internet purchasing globaly

Turbo Multi-language Translator

Make the better internet purchasing globaly

Turbosify SEO Speed Booster

5.0 (7) Free plan available
Get better conversions by improving store loading speed Installed

Turbo Multi-language Chat - AI Customer service in one hand

TurboCommerce make the better internet purchasing globaly
Our products

The help you need, when you need it

App by Turbo Engine

3 apps • 5.0 average rating

Turbosify Speed Booster

5.0 (7)
Get better conversions by optimizing shopify store Google page speed Installed

Turbosify Translator for Wordpress Woocommerce

5.0 (74) Free Wordpress Woocommerce Plugin
Translate your wordpress website to multiple language within 1 click, no configuration needed, no No technical required

Grow your business here

Whether you want to sell products down the street or around the world, we have all the tools you need.