Site logo

Archived topic

How Can I Add Shortcode to CustomPosts Using Elements > Hooks?

3 replies · Started by Ken on July 23, 2021

Viewing posts 1–4 of 4

Hi, recently my coach created 5-Custom Post Types (CPT).
I'd like to be able to add a table using shortcode to the following CPTs:

ten_product
list_post
five_product

I pasted a copy of my functions.php for the child theme where the CPTs are.

Question:
Is there a way I can add shortcode to the above mentioned CTPs?
If so..
Can you be so kind as to tell/show me what I need to add, and where I should place the code...assuming it's possible.

Thank you. Standing by.
***functions. php***

<?php
/**
 * GeneratePress child theme functions and definitions.
 *
 * Add your custom PHP in this file.
 * Only edit this file if you have direct access to it on your server (to fix errors if they happen).
 */
 
add_filter( 'generate_blog_columns', function( $columns ) {
    if ( in_array(get_post_type(), array('list_post','one_product','ten_product','five_product','how_to')) && ! is_singular() ) {
        return true;
    }

    return $columns;
} ); 

add_filter( 'generate_blog_get_column_count','tu_search_column_count' );
function tu_search_column_count( $count ) {
     if ( is_post_type_archive( array('list_post','one_product','ten_product','five_product','how_to') ) )  {
        return 33;
    }

    return $count;
}

add_filter( 'generate_blog_masonry','tu_portfolio_masonry' );
function tu_portfolio_masonry( $masonry ) {
    if ( is_post_type_archive( 'list_post','one_product','ten_product','five_product','how_to' ) ) {
        return 'true';
    }

    return $masonry;
}

function my_cptui_add_post_types_to_archives( $query ) {
	// We do not want unintended consequences - also modified to include home page.
	if ( is_admin() || ! $query->is_main_query() ) {
		return;    
	}

	if ( is_category() || is_tag() || is_home() && empty( $query->query_vars['suppress_filters'] ) ) {
		$cptui_post_types = cptui_get_post_type_slugs();

		$query->set(
			'post_type',
			array_merge(
				array( 'post' ),
				$cptui_post_types
			)
		);
	}
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_archives' );

add_filter('widget_posts_args', 'widget_posts_args_add_custom_type'); 
function widget_posts_args_add_custom_type($params) {
   $params['post_type'] = array('post','one_product','ten_product','five_product','list_post','how_to');
   return $params;
}

function my_cptui_add_post_types_to_rss( $query ) {
	// We do not want unintended consequences.
	if ( ! $query->is_feed() ) {
		return;    
	}

	$cptui_post_types = cptui_get_post_type_slugs();

	$query->set(
		'post_type',
		array_merge(
			array( 'post' ),
			$cptui_post_types
		)
	);
}
add_filter( 'pre_get_posts', 'my_cptui_add_post_types_to_rss' );

Hi there,

using the Hook Element - you simply need to add your [shortcode] to the text field.
Select the Hook for where you want it displayed in the template. Assuming your templates were made from the GP templates, then you can use any of the hooks show here:

https://docs.generatepress.com/article/hooks-visual-guide/#single-post

e.g before_main_content is above the post content.

Check the Execute Shortcodes option.

Then set the Display Rules to: 'your post type' > All Posts

HI David! YEOWZA! Wow! It works!!! \(*<>*)/
I was able to apply the short code to a specific place in a specific custom post.

Man this blows me away. I'll share this info with my coach, though unfortunately she's using Kadence. I'm trying to convince to move to GP. LOL!

Thank you soooo much for your help. It's custom here in Asia to show thanks and appreciation
with a bow... m(_ _)m

I'm closing this ticket with a BIG SMILE, hope you are too. \(*<>*)/

Glad to be of help! Yep = big smile too :)

This archived topic is closed to new replies.