How to include HTML and [shortcode] inside PHP? - php

I have a wordpress website and I want to display this at the end of every blog post:
<h5 style="margin-bottom: 15px;margin-top:35px;"><em><strong>Text:</strong></em>
[shareaholic app="share_buttons" id="5374371"]
If I add this to single.php which shows the pages I want to modify, the shortcode won't work.
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
/* Featured Image */
if(has_post_thumbnail()) { ?>
<p class="post-thumbnail">
<?php the_post_thumbnail('full'); ?>
</p>
<?php }
the_content();
the_tags('<p>'.__('Tags:', 'sntheme'),', ','</p>');
/* Show post page links */
wp_link_pages( array( 'before' => '<p>' . __( 'Pages:', 'sntheme' ), 'after' => '</p>' ) );
?>
<h5 style="margin-bottom: 15px;margin-top:35px;"><em><strong>Text:</strong></em>
[shareaholic app="share_buttons" id="5374371"]
</article><!-- end post -->
Is there a workaround for this?

Yes - http://wordpress.org/plugins/shareaholic/installation/
<?php echo do_shortcode ('[shareaholic app="share_buttons" id="5374371"]'); ?>

Related

Rift Wordpress theme, sidebar on single posts not showing

I have http://spainthephilippines.com/ where I use Rift Theme. Im having problem with showing the sidebar on single post page. On my index page the sidebar is showing, on the 404 page the sidebar is showing. It is working anywhere I use it just not in signle post page. I was looking 2 hours for answer but I cant find it. The theme is on version 1.0, so is it possible that to be the problem? Then why the sidebar shows on most pages? Anyone can help? Tell me if you need any code.
<?php global $theme; get_header(); ?>
<div id="main">
<?php $theme->hook('main_before'); ?>
<div id="content">
<?php $theme->hook('content_before'); ?>
<?php
if (have_posts()) : while (have_posts()) : the_post();
/**
* Find the post formatting for the single post (full post view) in the post-single.php file
*/
get_template_part('post', 'single');
endwhile;
else :
get_template_part('post', 'noresults');
endif;
?>
<?php $theme->hook('content_after'); ?>
</div><!-- #content -->
<?php get_sidebars(); ?>
<?php $theme->hook('main_after'); ?>
</div><!-- #main -->
EDIT
Here is the sidebar:
<?php global $theme; ?>
<div id="sidebar-primary">
<?php
if(!dynamic_sidebar('sidebar_primary')) {
/**
* The primary sidebar widget area. Manage the widgets from: wp-admin -> Appearance -> Widgets
*/
$theme->hook('sidebar_primary');
}
$theme->hook("sidebar_primary_after");
?>
And also the post-single.php:
<?php global $theme; ?>
<div <?php post_class('post post-single clearfix'); ?> id="post-<?php the_ID(); ?>">
<h2 class="title"><?php the_title(); ?></h2>
<div class="postmeta-primary">
<span class="meta_date"><?php echo get_the_date(); ?></span>
<span class="meta_categories"><?php the_category(', '); ?></span>
<?php if(comments_open( get_the_ID() )) {
?> <span class="meta_comments"><?php comments_popup_link( __( 'No comments', 'themater' ), __( '1 Comment', 'themater' ), __( '% Comments', 'themater' ) ); ?></span><?php
}
if(is_user_logged_in()) {
?> <span class="meta_edit"><?php edit_post_link(); ?></span><?php
} ?>
</div>
<div class="entry clearfix">
<?php
if(has_post_thumbnail()) {
the_post_thumbnail(
array($theme->get_option('featured_image_width_single'), $theme->get_option('featured_image_height_single')),
array("class" => $theme->get_option('featured_image_position_single') . " featured_image")
);
}
?>
<?php
the_content('');
wp_link_pages( array( 'before' => '<p><strong>' . __( 'Pages:', 'themater' ) . '</strong>', 'after' => '</p>' ) );
?>
<?php if(get_the_tags()) {
?><div class="postmeta-secondary"><span class="meta_tags"><?php the_tags('', ', ', ''); ?></span></div><?php
}
?>
<!-- Post ID <?php the_ID(); ?> wtf-->
<?php
if(comments_open( get_the_ID() )) {
comments_template('', true);
}
?>

Trouble placing wordpress image caption and content on single post page

Working on trying to get single post image and caption above post content in a custom theme.
Can I get the image caption below the image and remaining content below a new div?
Current code:
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
</header>
<div class="entry-content news-item-copy">
<?php
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
the_post_thumbnail();
if(!empty($get_description)){//If description is not empty show the div
echo '<div class="image-captions">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
}
?>
<div class="news-sharing">
<a class="socialite twitter-share" href="" target="_blank" data-via="" data-text="New Website Launch — " data-url="" data-count="horizontal">Share on Twitter</a>
<a class="facebook-like socialite" data-href="" target="_blank" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false">Like on Facebook</a>
</div>
<?php the_excerpt(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
This works in displaying the post image and excerpt but not the image with caption and not the full post text.
Replacing this:
<?php the_excerpt(); ?>
with:
<?php the_content(); ?>
returns the full post content including the image again.
Try this, there is nothing special just getting the post object and then its values. post_excerpt are also known as captions.
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
</header>
<div class="entry-content news-item-copy">
<?php
if(has_post_thumbnail())
{
the_post_thumbnail();
$thumbnail_id = get_post_thumbnail_id(get_the_ID());
$thumbnail_data = get_post($thumbnail_id);
$caption = $thumbnail_data->post_excerpt;
if(!empty($caption)){//If description is not empty show the div
echo '<div class="image-captions">' . $caption . '</div>';
}
}
?>
<div class="news-sharing">
<a class="socialite twitter-share" href="" target="_blank" data-via="" data-text="New Website Launch — " data-url="" data-count="horizontal">Share on Twitter</a>
<a class="facebook-like socialite" data-href="" target="_blank" data-send="false" data-layout="button_count" data-width="450" data-show-faces="false">Like on Facebook</a>
</div>
<?php
echo the_content();
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
'after' => '</div>',
) );
?>
</div><!-- .entry-content -->
</article><!-- #post-## -->
After this you don't need to add the featured/thumbnail image to the post content and it will not appear again in content. I hope this makes sense now.

Wordpress - get post content in new div

I am trying to use a post image/thumbnail in one div and the content in a new div on a custom layout. Having trouble trying to get the content after the image to show.
This works in getting my post title and image into the desired layout but is not showing the image caption–
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php the_title( '<h3 class="title no-underline">', '</h3>' ); ?>
</header><!-- .entry-header -->
<div class="news-images">
<div id="news-swiper" class="swiper-container">
<div class="swiper-wrapper">
<div class="swiper-slide" data-caption="">
<div class="entry-content news-item-copy">
<?php
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
the_post_thumbnail();
if(!empty($get_description)){//If description is not empty show the div
echo '<div class="image-captions">' . get_post(get_post_thumbnail_id())->post_excerpt . '</div>';
}
?>
</div>
</div>
</div>
</div>
</div>
<div class="news-sharing">
<?php wpsocialite_markup(); ?>
</div>
This is where I am trying to get the remaining post content and having issues.
<div class="news-item-copy">
<?php the_excerpt(); ?>
<?php
wp_link_pages( array(
'before' => '<div class="page-links">' . __( 'Pages:', 'themeName' ),
'after' => '</div>',
) );
?>
</div>
</article>
I've used <?php the_excerpt(); ?> just to place the content in it's desired location but obviously because it's an excerpt it's not showing the full content. The idea was to place the featured image/thumbnail with caption on top, the social sharing in the middle and the content last.
I think you want to replace the_excerpt() with the_content()
http://codex.wordpress.org/Function_Reference/the_content
You re passing thumbnail id inside get_post,you have to pass post id not thumbnail id in that function
$get_description = get_post(get_post_thumbnail_id())->post_excerpt;
Or simply echo the_content(); it will print post content & if you want to display some limited content
then use
substr(the_content(),0,150) it will display only 150 character

Conditional Classes for Wordpress loop posts

The css is already declared all I have to do is add some div classes. My theme uses an almost empty index.php with no html but uses the get_template_part functions and calls various template parts for post types, so it has several loop{template-name}.php files. I have copied the one for the blog index and made a new loop with modifications. The index for my blog, calls the appropriate template with the modifications - hooray. However I have to be able to conditionally add div classes to make the conversion work completely.
As the index.php for the blog runs through the loop I would like to be able to conditionally add one div class to the 1st returned post, and another div class to the 2nd and if the last post is odd then add a 3rd div class.
Here is my loop-template4.php file.
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<div class="one"> /*I added this line here and it works for not found.
<div id="post-0" class="post error404 not-found">
<h1 class="entry-title"><?php _e( 'Not Found', 'smpl' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found
for the requested archive. Perhaps searching will help find
a related post.', 'smpl' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
</div><!-- #post-0 -->
<?php endif; ?>
<?php
<?php while ( have_posts() ) : the_post(); ?>
/**
* I need to conditionally add one of 3 div classes here:
* odd `<div class="one_half">`,
* even `<div class="one_half last">`or
* odd and last, `<div class="one">`
*/
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<h2 class="entry-title">
<a href="<?php the_permalink(); ?>" title="
<?php printf( esc_attr__(
'Permalink to %s', 'smpl' ), the_title_attribute( 'echo=0' ) );
?>" rel="bookmark"><?php the_title(); ?></a>
</h2>
<?php do_action('skeleton_post_meta'); ?>
<?php if ( is_archive() || is_search() ) : // Only display excerpts for
archives and search. ?>
<div class="entry-summary clearfix">
<?php if (ot_get_option('show_post_thumbnails') && has_post_thumbn
nil()){
echo '<div class="alignleft">';
skeleton_thumbnailer('fourthree');
echo '</div>';
}?>
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php
if (ot_get_option('show_post_thumbnails') && has_post_thumbnail()) {
echo '<div class="alignleft">';
skeleton_thumbnailer('thumbnail');
echo '</div>';
}?>
<?php //the_content( __(
'Continue reading <span class="meta-nav">→</span>', 'smpl' ) );
do_action('skeleton_content');
?>
<div class="clear"></div>
<?php wp_link_pages( array( 'before' => '<div class="page-link">' .
__( 'Pages:', 'smpl' ), 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
</div> <!-- conditional div class -->
</div><!-- #post-## -->
<?php comments_template( '', true ); ?>
<?php endwhile; // End the loop. Whew. ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) {
do_action('skeleton_page_navi');
}?>
So the Question:
Is it possible, as the loop hasn't looped yet, and I need to conditionally add the div class based on the number of results from the loop?
As far as I know, there's no way to know when the last post in the loop is being displayed, so first we need to add some code right before your line `while (have_posts()) : the post(). For context, I've copied your entire block of code here, and have added comments to show where the new code is:
<?php /* If there are no posts to display, such as an empty archive page */ ?>
<?php if ( ! have_posts() ) : ?>
<div class="one"> /*I added this line here and it works for not found.
<div id="post-0" class="post error404 not-found">
<h1 class="entry-title"><?php _e( 'Not Found', 'smpl' ); ?></h1>
<div class="entry-content">
<p><?php _e( 'Apologies, but no results were found
for the requested archive. Perhaps searching will help find
a related post.', 'smpl' ); ?></p>
<?php get_search_form(); ?>
</div><!-- .entry-content -->
</div>
</div><!-- #post-0 -->
<?php endif; ?>
<?php
/**
* BEGIN: Additional code to add conditional classes
*/
// How many posts are going to be displayed? Let's count here:
$total = 0;
while ( have_posts() ) : the_post();
$total++;
endwhile;
// Reset the loop to actually start displaying them
rewind_posts();
// Add this line before your loop to prevent notices being thrown
$count = 1;
while ( have_posts() ) : the_post(); ?>
<?php
// Add some conditions to determine the class
if ($count % 2) {
// If the post displayed is an odd number
$class = 'one_half last';
} else {
// If the post displayed is an even number
$class = 'one_half';
}
if ($count == $total && ($count % 2)) {
// If the post is the last post, and it is odd
$class = 'one';
}
// Finally, increment the count
$count++;
?>
<?php // If you need an extra div, just put it in like so... ?>
<div class="<?php echo $class; ?>">
<?php
// If you need the existing div to simply have the class added,
// Then we pass it into the post_class() function
?>
<div id="post-<?php the_ID(); ?>" <?php post_class($class); ?>>
<h2 class="entry-title">

Link Wordpress post images to post

I'm developing a wordpress theme for my blog and I'm having some difficulty.
When I make a new post, the first image of that post is displayed on the homepage. When the image is clicked, it links to the image file and shows the full size. I would like for it to automatically link to the post from which it came. I understand that you can choose what the image links to upon upload or in the editor. I would like for the images to link to their original post automatically, since I have other people writing on my blog, and I don't want them to have to do this each time.
As I understand it the content.php file controls the post format in this case. Here is the file from my theme. Or is it possible to use a function?
<?php
/**
* The default template for displaying content single/search/archive
*
* #package test
* #since test 0.1.0
*/
?>
<!-- START: content.php -->
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header">
<?php if ( is_single() ) : ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<?php else : ?>
<?php the_post_thumbnail(); ?>
<h1 class="entry-title">
<?php the_title(); ?>
</h1>
<?php endif; // is_single() ?>
</header><!-- .entry-header -->
<?php if ( 'post' == get_post_type() ) : ?>
<div class="entry-meta">
<?php posted_on(); ?>
<?php if ( is_sticky() && is_home() && ! is_paged() ) : ?>
<span class="label radius secondary"><?php _ex( 'Featured', 'Post format title', 'test' ); ?></span>
<?php endif; ?>
</div><!-- .entry-meta -->
<?php endif; ?>
<?php if ( is_search() ) : // Only display Excerpts for Search ?>
<div class="entry-summary">
<?php the_excerpt(); ?>
</div><!-- .entry-summary -->
<?php else : ?>
<div class="entry-content">
<?php the_content( __( 'Continue reading <span class="meta-nav">→</span>', 'test' ) ); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'test' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<?php endif; ?>
<footer class="entry-meta">
<?php if ( 'post' == get_post_type() ) : ?>
<?php get_template_part('entry-meta', get_post_format() ); ?>
<?php endif; ?>
</footer><!-- #entry-meta -->
</article><!-- #post-<?php the_ID(); ?> -->
<!-- END: content.php -->
https://codex.wordpress.org/Function_Reference/the_post_thumbnail
example 1. To link Post Thumbnails to the Post Permalink in a specific loop, use the following within your Theme's template files:
<?php if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
<?php the_post_thumbnail(); ?>
</a>
<?php endif; ?>

Categories