-
George
I have created a plugin that loads the post content of a custom post type
portfolio
inside a modal. I have created a content template for that custom post type that includespost_content
and also a shortcode that displays a slider with images from an ACF repeater field. The shortcode is set up infunctions.php
and included inside the content template but as it’s not part ofthe_content
, it’s not being displayed inside the modal.Is there a way to load content that includes data from the content template instead of just
the_content
? Not sure if I am clear enough.Here is the plugin PHP:
function custom_post_modal_scripts() { // Check if the current page is the archive page of the "portfolio" custom post type if (is_post_type_archive('portfolio')) { // Enqueue styles and scripts only for the "portfolio" custom post type archive wp_enqueue_style( 'custom-post-modal-style', plugins_url( 'custom-modal-style.css', __FILE__ ) ); wp_enqueue_script( 'custom-post-modal-script', plugins_url( 'custom-modal-script.js', __FILE__ ), array(), null, true ); // Localize the ajaxurl variable wp_localize_script( 'custom-post-modal-script', 'custom_post_modal_ajax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) ); } } add_action( 'wp_enqueue_scripts', 'custom_post_modal_scripts' ); function custom_post_modal_markup() { echo '<div id="custom-post-modal" class="custom-post-modal"> <div class="custom-post-modal-content"> <span class="custom-post-modal-close">×</span> <div id="custom-post-modal-content"></div> </div> </div>'; } add_action('wp_footer', 'custom_post_modal_markup'); // Place the loading spinner outside the modal markup function custom_post_modal_loading_spinner() { echo '<div id="loading-indicator" class="loading-spinner-wrapper"> <div class="loading-spinner"></div></div>'; } add_action('wp_footer', 'custom_post_modal_loading_spinner'); function fetch_custom_post_content() { $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; $post = get_post( $post_id ); if ( $post ) { $post_title = get_the_title( $post_id ); $post_content = apply_filters( 'the_content', $post->post_content ); // Output both the title and content echo '<h2>' . $post_title . '</h2>'; echo $post_content; } else { echo 'Post not found for ID: ' . $post_id; } die(); } add_action( 'wp_ajax_fetch_custom_post_content', 'fetch_custom_post_content' ); add_action( 'wp_ajax_nopriv_fetch_custom_post_content', 'fetch_custom_post_content' );
-
Hi there,
whats inside the Content Template that contains data ?
-
George
Hey David.
A flex container with two inner containers, one containing the slider shortcode and the other a GPP dynamic block set to display the content.
-
So this:
that includes data from the content template
What do you mean by that ? Whats the data ?
Perhaps it might be easier to explain whats not working 🙂
-
George
Hey David, I’ve attached a video if you want to have a look!
-
Very nice 🙂
Not sure the best approach on this;
instead of calling the single post inside the modal you would need to call the template which in turn returns the content. -
George
I have a suspicion that the
generate_do_template_part
filter might have something to do with it. Is there some kind of documentation I could have a look at? -
The function can be seen here:
Apart from the before and after action hooks it adds a filter for the template part to be loaded.
So when the
generate_do_template_part
function is called eg.It calls the template part as specified eg.
content-single
-
George
It calls the template part as specified eg.
content-single
So what would it be for a single CPT called
portfolio-new
? -
Depends, for example if you had a content template for the single CPT it would need to be named:
content-portfolio-new`
But…. what happens when you use a GP Block Element Content Template it disables the content template and Hooks in your element into the after hook.
-
George
But…. what happens when you use a GP Block Element Content Template it disables the content template and Hooks in your element into the after hook.
You mean the
generate_after_do_template_part
hook?Is is possible to insert that hook somewhere else with PHP?
-
Thats correct, the GP Element is hooked into:
generate_after_do_template_part
I am not sure what you mean 🙂
How is the Modal outputting the Post ? -
George
The answer in on my first post.
-
George
Towards the end of the code block.
function fetch_custom_post_content() { $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; $post = get_post( $post_id ); if ( $post ) { $post_title = get_the_title( $post_id ); $post_content = apply_filters( 'the_content', $post->post_content ); // Output both the title and content echo '<h2>' . $post_title . '</h2>'; echo $post_content; } else { echo 'Post not found for ID: ' . $post_id; } die(); } add_action( 'wp_ajax_fetch_custom_post_content', 'fetch_custom_post_content' ); add_action( 'wp_ajax_nopriv_fetch_custom_post_content', 'fetch_custom_post_content' );
-
Have you tried including your Shortcode in your fetch custom post function eg.. after the
echo $post_content;
? -
George
Yeah, I tried this:
function fetch_custom_post_content() { $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; $post = get_post( $post_id ); if ( $post ) { $post_title = get_the_title( $post_id ); $post_content = apply_filters( 'the_content', $post->post_content ); // Add your shortcode here $shortcode = '[portfolio_slider]'; // Concatenate the shortcode to the post content $post_content .= do_shortcode( $shortcode ); // Output both the title and content echo '<h2>' . $post_title . '</h2>'; echo $post_content; } else { echo 'Post not found for ID: ' . $post_id; } die(); } add_action( 'wp_ajax_fetch_custom_post_content', 'fetch_custom_post_content' ); add_action( 'wp_ajax_nopriv_fetch_custom_post_content', 'fetch_custom_post_content' );
It’s not loading it but I believe the code is correct, it’s just that this particular shortcode is not rendering (I have tried a few simpler ones, eg, to display the current date and it worked). I will probably need to adjust something in order to be able to display the slider shortcode, which is this, by the way:
//Portfolio slider shortcode function portfolio_slider_shortcode() { // Log a message to indicate that the shortcode function is being executed error_log('Portfolio slider shortcode called'); $images = get_field('gallery'); ob_start(); if ($images): ?> <section class="splide" aria-label="Splide Basic HTML Example"> <div class="splide__track"> <ul class="splide__list"> <?php foreach ($images as $image): ?> <li class="splide__slide"> <img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" /> </li> <?php endforeach; ?> </ul> </div> <?php endif; return ob_get_clean(); } add_shortcode('portfolio_slider', 'portfolio_slider_shortcode');
I want to worry about one thing at a time, though.
Is it possible to insert the single portfolio content into that modal? I have created a certain layout as a content template and I want that displayed inside the modal. Is that clear?
- You must be logged in to reply to this topic.