Insert a post into the template in wordpress - wordpress

I inserted a post by the ID of it into the a template (footer.php) but I only get the raw text from the post. The html tags and everything else is missing. I tried it with this
<?php
$post_id = 32;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo $queried_post->post_content;
?>
and also a with a shortcode and plugin. Same result.
I need the html tags and everything because the site is build with a site builder (elementor) and it has some progressbars inside.
Any Ideas? Is this even possible?

You need to apply the filters, here's how your updated code looks:
<?php
$post_id = 32;
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
echo $title;
echo apply_filters('the_content', $queried_post->post_content);
?>

Related

Audio player in excerpt wordpress

I'm trying to display the audio player in the post excerpt in Wordpress. On this website I found the following code that I added to functions.php:
/**
* Create an excerpt that includes the audio for podcasts.
* #return string The excerpt HTML
*/
function wp_podcast_excerpt() {
// Gets the post content
$content = get_the_content();
// Find the position of the audio shortcode in the content
$audiopos = strpos($content,'[audio');
if($audiopos) {
// The excerpt is all the text up to and including the audio tag.
$excerpt = substr($content, 0, strpos($content,'[/audio]'));
// Apply wordpress filters.
$excerpt = apply_filters('the_content', $excerpt);
// Strip out images.
$excerpt = preg_replace("/<img[^>]+\>/i", "", $excerpt);
echo $excerpt;
} else {
the_excerpt();
}
}
In the loop I have included:
<?php wp_podcast_excerpt(); ?>
This only shows the text and nothing else. What am I doing wrong?
Your comment:
The excerpt is all the text up to and including the audio tag.
Is incorrect. At that point in time, $excerpt contains the text up to, but excluding the closing audio tag. I'd imagine you'd like to have this included in $excerpt, so that it is properly parsed by WordPress filters.
You could do all manor of things to ensure that it's included when applying filters. One very straight-forward way would be to increase substrs third parameter by an amount that equals the length of the string [/audio]:
$closing_audio_tag = '[/audio]';
// The excerpt is all the text up to and including the audio tag.
$excerpt = substr(
$content,
0,
strpos($content, $closing_audio_tag) + strlen($closing_audio_tag)
);
I got the result I wanted. Here is the solutions.
Add this to the functions.php:
<?php add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode'); ?>
Then, inside the loop add:
<?php echo do_shortcode("[audio]"); ?>
All together it look like this:
<?php
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<?php the_post_thumbnail(); ?>
<?php the_title() ?>
<?php the_excerpt() ?>
<?php echo do_shortcode("[audio]"); ?>
<?php endwhile; ?>

Advanced Custom Fields code displayed as plaintext when written in Code Snippets plugin. How to fix?

On Wordpress, I’m trying to add a link to single product page using Code Snippets with Advanced Custom Fields. Instead of a link, my code displays as plaintext.
I have tried this code:
function product_datasheet_below_summary() { ?>
$link = get_field('datasheet');
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class="button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php
};
add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );
This doesn’t work. I was hoping for a link to the Datasheet, but it simply prints, in plaintext:
$link = get_field(‘datasheet’); if( $link ): $link_url =
$link[‘url’]; $link_title = $link[‘title’]; $link_target =
$link[‘target’] ? $link[‘target’] : ‘_self’; ?>
followed by a generic square button link.
What am I doing wrong here? Thanks very much for your help.
Thanks for your advice. Instead of using Code Snippets I just created a child theme and edited the relevant .php file, adding the following:
`
if( $link ):
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
?>
<a class= "button" id="datasheet-button" href="<?php echo esc_url($link_url); ?>" target="<?php echo esc_attr($link_target); ?>"><?php echo esc_html($link_title); ?></a>
<?php endif; ?>`
You're getting plaintext after the first ?> because that's a php ending tag, and the Code Snippets plugin doesn't allow for multiple php statements and is simply crashing and dumping plain text rather than executing code.
You need to rewrite the whole function as one php statement and echo all the button html, along with the php variables delimited in the html with .'s. A simple example:
<?php
$var = "Hello World";
echo "<p>The value of the variable is : " . $var . "</p>";
?>
And you may need to use the more standard ACF get field construct, too:
$value = get_field( "text_field" );
Search SE for more examples of echoing html in php.
Your function is a bit all over the place, I have cleaned it up to work in the output you want it to using an object instead of echoing out multiple parts of the button code. This in my opinion is easier to manage and looks nicer as it keeps the HTML and PHP code as separate as possible:
function product_datasheet_below_summary() {
$link = get_field('datasheet');
if( $link ){
$link_url = $link['url'];
$link_title = $link['title'];
$link_target = $link['target'] ? $link['target'] : '_self';
} ob_start();?>
<?php if($link):?>
<a class="button" href="<?php echo $link_url;?>" target="<?php echo $link_target;?>"><?php echo $link_title;?></a>
<?php endif;
return ob_get_clean();
} add_action( 'ocean_after_single_product_meta', 'product_datasheet_below_summary', 5 );?>

Wordpress get_the_post_thumbnail only works for current post ID

I'm working on creating a 'Related Articles' feature on my Wordpress site. It uses custom fields where the post author can paste links to related posts that will be displayed at the bottom of the current post. I want to display a thumbnail and a link for each related post.
Problem: get_the_post_thumbnail only works when I provide the ID for the current post. Does anyone have any idea what might be going on here? Here's the code I'm using to generate a related post. I've confirmed that $url, $id, and $title all output as expected.
<?php $url = get_field('url_01'); ?>
<?php $id = url_to_postid($url); ?>
<?php $thumb = get_the_post_thumbnail($id, 'yarpp-thumbnail'); ?>
<?php $title = get_the_title($id); ?>
<?php echo $thumb ?>
<?php echo $title; ?>

Load page content and blog post index content (WordPress)

I have a question about Wordpress' template structure and querying posts.
I have my templates setup where the (for example) archive-$posttype.php is built like:
get_header();
$args = 'page_id=18'; //Client Uses page
query_posts($args); the_post();
get_template_part( 'sub-header' );
// Reset Query
wp_reset_query();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>...
I do this to set my default $post variable for my sub-header.php file which prints out content from that page:
<div id="subheader">
<h1><?php echo get_post_meta($post->ID, 'header_title', true)?></h1>
<?php echo get_post_meta($post->ID, 'header_description', true)?>...
However, using this method on the home.php template, doesn't work:
get_header();
$temp_query = $wp_query;
$page_id = 119; //Client Uses page
#$post = get_page( $page_id );
$args = array('page_id' => $page_id);
$post = new WP_Query( $args ); the_post();
get_template_part( 'sub-header' );
wp_reset_postdata();
?>
<div class="content">
<?php get_template_part( 'loop' ); ?>
<?php get_sidebar( 'news' ); ?>
</div><!--.content -->
<?php get_footer(); ?>
I'm curious why this works on one template and not on the home template. AND, am I going about this the wrong way? What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on.
Thanks!
Not sure I understand your problem exactly but if you are doing what I think, having a chunk of text from a specific page above another loop that pulls in posts of some sort or another I would use the template naming structure.
<?php /* Template Name: My Template */ ?>
This will allow you to use the standard loop to get content from whatever page the clients sets the template to (avoiding the static ids you are using).
Your home page bug could be due to it not being set as the posts page in the reading settings. As I understand it if a page is not set as the posts page it will act like a normal page unless you rewrite the query specifically.
"What's the correct way to have page content in the sub-header template that in most cases is ACTUALLY related to that current page the user is on."
Rather than trying to tie together two unrelated pages/posts, it might be easier to use a custom field tool. My current favorite is Advanced Custom Fields.
With ACF, you can add supplemental fields (image, wysiwyg, file upload, 14 total field types) to your posts and pages, then easily pull the custom data into your template. It's very well documented and extremely easy to use.
So, I changed the way my sub-header.php template works. Basically some basic checking on what type of page/post was being set/called then dynamically pulled the relevant page info.
<?php
if (is_page()) :
$header_title = get_post_meta($post->ID, 'header_title', true);
$video_id = get_post_meta($post->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($post->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($post->ID, 'header_description', true);
elseif (is_home()) :
$page_id = 119; // News page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
elseif (is_archive()) :
$page_id = 18; // Client Uses page
$page = get_page( $page_id );
$header_title = get_post_meta($page->ID, 'header_title', true);
$video_id = get_post_meta($page->ID, 'youtube_video_id', true);
$thumbnail = get_the_post_thumbnail($page->ID, 'post-thumbnail', array('class'=>'visual-element'));
$description = get_post_meta($page->ID, 'header_description', true);
endif;
?>
<div id="subheader">
<h1><?php echo $header_title; ?></h1>
<?php if ( $video_id ) : ?>
<iframe class="visual-element" width="300" height="200" src="http://www.youtube.com/embed/<?php echo $video_id;?>?rel=0" frameborder="0" allowfullscreen></iframe>
<?php elseif ($thumbnail) : ?>
<?php echo $thumbnail; ?>
<?php endif; ?>
<?php echo $description; ?>
</div><!-- #subheader -->

Wordpress get_the_ID() returns nothing but get_the_title() returns title?

I'm struggling with wordpress templates. In one of my theme templates I have the following lines
<?php
echo get_the_ID(); // displays nothing
echo get_the_title(); // displays "Hello World! this is title"
?>
Why does the get_the_ID() return nothing ? I want the page id of the current page I'm on.
This works within the loop:
<?php $this_page_id = get_the_ID(); echo $this_page_id ; ?>
Outside of the loop:
<?php $this_page_id = $wp_query->post->ID; echo $this_page_id ;?>
if you are using wp_query just add this
<?php echo $wp_query->post->ID; ?>

Resources