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; ?>
Related
I created custom post status "blocked" via register_post_status (like this register_post_status not showing post_status in status dropdown).
Now I want to replace content on posts with this status via functions.php.
I tried something like this
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return 'This post is blocked';
}
}
add_action('the_post','block_by_status');
But it doesn't work, please somebody help?
So for the content you can do something as simple as:
add_filter('the_content', 'block_by_status');
and to hide the title you can use:
add_filter( 'the_title', 'block_by_status', 10, 2 );
If you want to go further than that, I suggest editing the template in the theme folder.
For pages you need to edit page.php and for posts you need to edit posts.php or singular.php, depends on the theme you are using.
So you can do something like this:
In the functions.php file:
function block_by_status ($post_object) {
global $wp_query;
if( 'blocked' == $wp_query->post->post_status ) {
return true;
}
}
and then in the files I referenced above (Depends on your theme):
<?php if (!block_by_status()) { ?>
<?php while (have_posts()) : the_post(); ?>
<h1><?php the_title() ?></h1>
<?php the_content() ?>
<?php get_sidebar() ?>
<?php endwhile; ?>
<?php } else {
echo 'This post is blocked';
} ?>
So you use your function as a boolean and just hide what you dont want users to see.
I'm working in one theme, and i trying to sumit to wordpress theme page, but i have the follow massages:
REQUIRED: This theme doesn't seem to display tags. Modify it to display tags in appropriate locations.
So i found that i don't have the_tags function but i have my own code with the wp_get_post_tags because i make my own html, so i dont know how to fix this problem.
This is my function
function basico_create_link($array_object_terms, $type) {
$link = '';
foreach ($array_object_terms as $object_terms) {
$link.='<a href="';
if ($type == 'category') {
$link.=get_tag_link($object_terms->term_id);
} else if ($type == 'tag') {
$link.= get_category_link($object_terms->term_id);
}
$link.='" > ' . $object_terms->name . '</a>,';
}
return substr($link, 0, -1);
}
And i used this way
basico_create_link(wp_get_post_tags(get_the_ID(), array('fields' => 'all')), 'tag');
Function the_tags() should be somewhere within The Loop. You can put it in your theme's index.php, although I myself would rather opt for single.php (since the tags are needed in blogposts).
Therefore, I start the loop, add some other content as needed and then I put the functions needed to get a blogpost properly displayed. In general, it could go like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<article>
<!--some additional html/php content as needed -->
<h1><?php the_title(); ?></h1>
<p><?php the_content(); ?></p>
<?php the_category(' '); ?> || <?php the_tags(); ?>
<!--et cetera-->
I am trying to get a custom single page to spit out the title and content of the post, the title works fine, the content of the post doesn't seem to want to come through. I don't work much in wordpress so I am in the dark here, can someone tell me how to fix this? Here is my single-news.php code:
<?php get_header(); ?>
<div class="decade1">
<?php
echo get_the_title().'<br/>'; //Output titles of queried posts
echo get_the_content().'<br/>';
?>
</div>
<?php get_footer(); ?>
Thanks!!
You need the WordPress Loop before using tags like get_the_title().
<?php get_header(); ?>
<div class="decade1">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
echo get_the_title() . '<br/>';
echo get_the_content() . '<br/>';
} // end while
} // end if
?>
</div>
<?php get_footer(); ?>
get_the_content() must be use in loop.
syntax:
get_the_content( $more_link_text, $stripteaser )
where $more_link_text and $stripteaser are optional.
read about it
and get_the_title() will output title only when it is in loop, otherwise you have to provide post id to display title.
get_the_title($post_id)
I want to show latest ten post of my security category with date and post title and the post thumbnail.
To show the pic I have faced with problem. According to this article
http://www.wpbeginner.com/beginners-guide/how-to-add-featured-image-or-post-thumbnails-in-wordpress/
when we want to showing the thumbnail first of all we should have to copy the following code to the function.php file
add_theme_support( 'post-thumbnails' );
and using the
<?php the_post_thumbnail(); ?>
To show pic of the post, I use this code into the loop but it doesn’t work. my code is here:
<?php query_posts('securitysoft=CATEGORYNAME&showposts=10');
while ( have_posts() ) : the_post(); ?>
<br/>
<?php
php the_post_thumbnail();
?>
<br/>
<?php the_time(__('j/F/ Y','kubrick')) ?>
<br/>
<?php the_title();?>
<?php endwhile; ?>
The main problem is that the picture does not show. To prove this I am taking post Id of the post that has thumbnail(292) like this :
<?php if ( has_post_thumbnail(292))
{
echo "<script type='text/javascript'>alert('yes')</script>";
has_post_thumbnail(292);
}
else
{
echo "<script type='text/javascript'>alert('no')</script>";
has_post_thumbnail(292);
}
?>
note:292 is the post id
The result of above code is no.
I have replaced php the_post_thumbnail(); with each one of following code but does not work:
get_the_post_thumbnail($post->ID);
echo get_the_post_thumbnail($post->ID);
get_the_post_thumbnail($post_id, 'thumbnail');
echo get_the_post_thumbnail($post_id, 'thumbnail');
<?php echo get_the_post_thumbnail($post_id, 'thumbnail', array('class' => 'alignleft')); ?>
And this is my function.php :
<?php
add_theme_support( 'post-thumbnails' );
if ( function_exists('register_sidebar') )
register_sidebar();
?>
do you set the feature image!! remember if you don't set the feature image. the image won't display. because this command that you have used only take the thumbnail. to set the feature image go to the feature image panel and click on the Set featured image link.
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 -->