The_excerpt showing before the div - php

i've created latest post container, which i'm trying to create in a shortcode, however when i add the excerpt into the div it is not being shown as it would normally. it is being pushed before the div. The test string after the excerpt is being placed the correct place inside in the div. How come the excerpt is being pushed to the top?
if($insta->have_posts()) :
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
$post_excerpt = the_excerpt();
$permalink = get_permalink();
$output = '<div class="betting-item">'
. '<div class="betting-thumb">'
. '<a href=" '. $permalink .' ">'
. '<img class="thumbnail" src="'. $thumb_url[0] .'" alt="0" />" </a>'
. '</div>'
. '<div class="betting-description">'
. '<h6>Instalocket</h6>'
. '<div class="meta">November 18, 2014 • No Comments</div>'
. '<div>' . $post_excerpt[0] . ' test</div>'
. '</div>'
. '</div>';
endwhile;
endif;

That is happening because the_excerpt() will print the except there and then (so before the div), you need to use get_the_excerpt () which will allow you to print it out at a later stage (in the div).
EDIT:
This should work and only print the excerpt on the first loop:
if($insta->have_posts()) :
$count = 0;
while($insta->have_posts()) :
$insta->the_post();
$thumb_id = get_post_thumbnail_id();
$thumb_url = wp_get_attachment_image_src($thumb_id,'thumbnail-size', true);
if($count == 0)
{
$post_excerpt = the_excerpt();
$count = 1;
}
else
{
$post_excerpt = '';
}
$permalink = get_permalink();

Related

How do I call post objects (products) custom fields using shortcodes on posts

I'm trying to show 'featured products' on blog posts. These featured products are to be selected with custom fields post objects on the back end for each post.
I have written up what I think the PHP should be - where am I going wrong? When I try to use the shortcode no code appears (but the shortcode text doesn't show so it's definitely added). Thanks :)
<?php
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){
$html = '';
$instruments = get_field('featuredprod');
if( $instruments ):
$html .= '<div class="featuredproducts">';
$html .= '<h2 style="font-size:18px; font-family:poppins;">Featured in this video</h2>';
foreach( $instruments as $instruments ):
$permalink = get_permalink( $instruments->ID );
$title = get_the_title( $instruments->ID );
$product = wc_get_product( $instruments->ID );
$price = $product->get_price();
$featured_img_url = get_the_post_thumbnail_url($instruments->ID, 'full');
$html .= '<div class="featuredproduct">';
$html .= '<img class="featuredproductimg" src="' . $featured_img_url . '">';
$html .= '<div class="proddetails">';
$html .= '<a class="producttitle" href="' . $permalink . '"><?php echo esc_html( $title ); ?></a>';
$html .= '<br><span class="productprice">£' . $price . '</span>';
$html .= '</div>';
$html .= '</div>';
endforeach;
$html .= '</div>';
endif;
}
You have built your HTML in the $html variable but then you don’t do anything with it. The shortcode doesn’t automatically know that you want to display the $html variable so you need to return ( or echo) it at the end before the function finishes:
add_shortcode('featuredproducts' , 'printfeaturedprod');
function printfeaturedprod(){
$html = '';
/* your code here... */
return $html;
}
Reference: See the add_shortcode WP documentation

Permalink of specific post ID

Below the code I have on my page tot display the permalink of q specific post in Wordpress. It works, but I have the feeling it can be easier. Can somebody explain how?
$post_id = 26; // post id
$queried_post = get_post($post_id);
$title = $queried_post->post_title;
$content = $queried_post->post_content;
$perma = get_permalink($post_id);
if ( has_post_thumbnail() ) {
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),’thumbnail’ );
}
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo $content;
Well your code seems ok. However there are some improvements that can be made.
For example, when you have the $queried_post object, you don't need to create additional variables for content and title. You can use this object properties to get the values.
Also you can use wordpress get_the_post_thumbnail to show featured image.
Some formatting and it's almost perfect.
$post_id = 26; // post id
$queried_post = get_post($post_id);
echo '<a href="' . get_permalink( $post_id ) . '" title="' . $queried_post->post_title . '">';
echo $queried_post->post_title;
echo '</a>';
if ( has_post_thumbnail( $post_id ) ) {
echo get_the_post_thumbnail( $post_id, 'full', array('width' => '100%') );
}
echo $queried_post->post_content;
try this code
$post_id = 26;
if ( has_post_thumbnail($post_id) )
{
$image_src = wp_get_attachment_image_src( get_post_thumbnail_id($post_id),’thumbnail’ );
}
echo '<a href="' . get_the_permalink($post_id) . '" title="' .get_the_title($post_id). '">';
echo get_the_title($post_id);
echo '</a>';
echo '<img width="100%" src="' . $image_src[0] . '">';
echo get_the_content($post_id);
In response to your comment, if you want to simplify this line:
echo '<a href="' . $perma . '" title="' . $title . '">';
echo $title;
echo '</a>';
You could do:
echo '' . $title . '';
So replacing multiple echo; with the "." to 'continue' without breaks.

Different "Read more" in different excerpts on the same page

I want two differents excerpts on my homepage, one for the posts inside the loop, the other one for the latest post published, outside the loop.
I managed to add the excerpt on the latest post outside of the loop like this
<?php $postslist = get_posts('numberposts=1&order=DESC&orderby=post_date');
foreach ($postslist as $post) :
setup_postdata($post); ?>
<?php wp_latest_excerpt('wp_latest', 'excerpt_more_latest'); ?>
<?php endforeach; ?>
While the excerpt for the posts inside the loop is
<?php wp_excerpt('wp_index'); ?>
For some reasons I am able to set the excerpt length differently for both of my excerpts, but the "more" stays the same. (both "more" have the class .view-article)
I thought I could create another excerpt with a different "more" function, but it does not work, here are my 2 different excerpts and more functions.
For the length
function wp_index($length)
{
return 21;
}
function wp_latest($length)
{
return 18;
}
For the "more"
function view_article($more)
{
return '... </br><a class="view-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
function latest_view_article($more)
{
return '... </br><a class="view-latest-article" href="' . get_permalink($post->ID) . '">' . __('Continue Reading', '') . '</a>';
}
add_filter('excerpt_more_latest', 'latest_view_article');
add_filter('excerpt_more', 'view_article');
And finally the excerpts
function wp_latest_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more_latest', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
function wp_excerpt($length_callback = '', $more_callback = '')
{
if (function_exists($length_callback)) {
add_filter('excerpt_length', $length_callback);
}
if (function_exists($more_callback)) {
add_filter('excerpt_more', $more_callback);
}
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
$output = '<p>' . $output . '</p>';
echo $output;
}
Is this a wrong way to achieve this or did I make some mistakes in my code ?

Direct a portfolio item to the single project and disable the ajax script wordpress theme

At the moment on my portfolio page you can click on a portfolio item and a ajax container pops up. And in this container you can click on a button to go to the project and read more details.
BUT
I want to disable this ajax container and when you click on a single portfolio item it needs to go straight to the project item page with all the details on it etc.
Now have I been searching in all of the .php files and in the scripts but I just can't find the action when you click on a portfolio item and I'm not really sure with what I need to replace it when found. I hope someone could help me with this one.
Here is the code of the portfolio page:
<div id="portfolio-grid" class="clearfix">
<?php
while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
get_template_part( 'includes/entry', 'portfolio' );
endwhile;
wp_reset_postdata();
?>
</div> <!-- end #portfolio-grid -->
And here is the code of the Ajax container -I think-
function et_show_ajax_project(){
global $wp_embed;
$project_id = (int) $_POST['et_project_id'];
$portfolio_args = array(
'post_type' => 'project',
'p' => $project_id
);
$portfolio_query = new WP_Query( apply_filters( 'et_ajax_portfolio_args', $portfolio_args ) );
while ( $portfolio_query->have_posts() ) : $portfolio_query->the_post();
global $post;
$width = apply_filters( 'et_ajax_media_width', 600 );
$height = apply_filters( 'et_ajax_media_height', 480 );
$media = get_post_meta( $post->ID, '_et_used_images', true );
echo '<div class="et_media">';
if ( $media ){
echo '<div class="flexslider"><ul class="slides">';
foreach( (array) $media as $et_media ){
echo '<li class="slide">';
if ( is_numeric( $et_media ) ) {
$et_fullimage_array = wp_get_attachment_image_src( $et_media, 'full' );
if ( $et_fullimage_array ){
$et_fullimage = $et_fullimage_array[0];
echo '<img src="' . esc_url( et_new_thumb_resize( et_multisite_thumbnail($et_fullimage ), $width, $height, '', true ) ) . '" width="' . esc_attr( $width ) . '" height="' . esc_attr( $height ) . '" />';
}
} else {
$video_embed = $wp_embed->shortcode( '', $et_media );
$video_embed = preg_replace('/<embed /','<embed wmode="transparent" ',$video_embed);
$video_embed = preg_replace('/<\/object>/','<param name="wmode" value="transparent" /></object>',$video_embed);
$video_embed = preg_replace("/height=\"[0-9]*\"/", "height={$height}", $video_embed);
$video_embed = preg_replace("/width=\"[0-9]*\"/", "width={$width}", $video_embed);
echo $video_embed;
}
echo '</li>';
}
echo '</ul></div>';
} else {
$thumb = '';
$classtext = '';
$titletext = get_the_title();
$thumbnail = get_thumbnail($width,$height,$classtext,$titletext,$titletext,false,'Ajaximage');
$thumb = $thumbnail["thumb"];
echo '<a href="'. esc_url( get_permalink() ) . '">';
print_thumbnail($thumb, $thumbnail["use_timthumb"], $titletext, $width, $height, $classtext);
echo '</a>';
}
echo '</div> <!-- end .et_media -->';
echo '<div class="et_media_description">' .
'<h2 class="title">' . '' . get_the_title() . '' . '</h2>' .
truncate_post( 560, false );
echo '</div> <!-- end .et_media_description -->';
echo '<a class="more" href="' . get_permalink() . '">' . __( 'Meer info »', 'Flexible' ) . '</a>';
endwhile;
wp_reset_postdata();
die();
}
You can see the page here: http://bit.ly/10BDVVf
Again thank you!
This is being controlled via javascript in /wp-content/themes/Flexible/js/custom.js. Comment out lines 29-77 and it will link as expected (the return false at the end cancels that standard navigation), but keep in mind that if you edit the theme directly and upgrade it, your changes will be overwritten.

Wordpress post->ID Problem

This is a wordpress question. I am trying to use a bit of code that works just fine on my home page on my inner page templates:
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta($post->ID, 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta($post->ID, 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta($post->ID, 'date_value', true)));
$issetdate = get_post_meta($post->ID, 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';
However, this doesn't seem to work on the inner-pages. All the titles links are being outputted correctly but it won't print the get_post_meta part correctly.
The list items all display something like <li class="cal_event_li list_item_1_1">
I think there is perhaps some issue with the way I have tried to use $post->ID but Im not sure whats going on here. Any ideas?
When you use query_posts you have to call global $post to get the post_meta. If you're only calling one category why don't you just use an archive template?
Also if you're going to use query_posts make sure you reset the query afterwords so plugins, sidebars, etc can still interact with the loop for conditionals etc..
global %post;
query_posts('cat=4');
// The Loop
//more stuff
endwhile;
wp_reset_query();
try replacing $post->ID with the_ID() in the inner pages. something like this
query_posts('cat=4');
// The Loop
echo '<div id="cal_details"><ul>';
while ( have_posts() ) : the_post();
$cal_date_j = date('j', intval(get_post_meta(the_ID(), 'date_value', true)));
$cal_date_n = date('n', intval(get_post_meta(the_ID(), 'date_value', true)));
$my_array[] = date('j, n', intval(get_post_meta(the_ID(), 'date_value', true)));
$issetdate = get_post_meta(the_id(), 'date_value', true);
if (isset($issetdate)) {
echo '<li class="cal_event_li list_item_' . $cal_date_j . '_' . $cal_date_n . '">';
echo '<a href="' . get_permalink() . '">';
the_title();
echo '</a></li>';
}
endwhile;
echo '</ul></div>';

Categories