How to set a function to show up a content based on particular WordPress page using functions.php?
I do not like pure inserted PHP code inside a theme. An example: IF PAGE==XXX ECHO CONTENTYYY
mhansen
2:24 pm on Apr 3, 2023 (gmt 0)
We do this on select post/pages with a shortcode from a plugin - not sure if that helps much, but it does keep you out of the theme files.
Basically, we use the WP editor to craft a post which is sued as the content we want included in other post/pages. On the pages where it is to be displayed, we use the shortcode to display it.
The plugin is: RPS Include Content
TessCompton
9:55 am on Sep 14, 2023 (gmt 0)
You can achieve this by using conditional statements in your WordPress theme's functions.php file. Here's an example of how to display content based on a specific WordPress page:
php
function custom_page_content() { // Check if we are on the desired page (replace 'XXX' with the page ID or slug) if (is_page('XXX')) { // Content to display on the specific page echo "CONTENTYYY"; } } add_action('wp_footer', 'custom_page_content');
In this example:
Replace 'XXX' with the actual page ID or slug where you want to display the content.
The custom_page_content function checks if the current page matches the specified page. If it does, it echoes the content.
We hook this function to the wp_footer action, which ensures the content is added to the page's footer.
Remember to replace 'XXX' with the correct identifier for your target page. This way, you can conditionally display content without adding PHP code directly to your theme's template files.