Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

KarmaCS on "Altering post display shortcode"

$
0
0

I am currently using the Extensio theme on my site. I had a blog shortcode that displays posts on a page, but only offers a couple of variables.

I would like to be able to specify a post category, or multiple post categories to all ow me to separate my sales messages from the educational portion of my website. I want to display my sales messages only on the Current Specials page.

Is there an easy way to alter the code below to make this possible? Any help would be appreciated.

Thanks,
KarmaCS

[latestposts title="Current Specials" count="4" mainpost_contentlength="190" otherposts_contentlength="190"]

The code behind it is:

//[latestposts title="latest from the blog" count="4" mainpost_contentlength="80" otherposts_contentlength="190"]
function theme_latestposts($atts, $content=null){
    extract(shortcode_atts(array(
		"title" => "",
		"count" => "4",
		"mainpost_contentlength" => "80",
		"otherposts_contentlength" => "190",
    ), $atts));

	global $wp_query, $post;

	$type = 'post';
	$args=array(
		'post_type' => $type,
		'post_status' => 'publish',
		'posts_per_page' => $count,
		'sort_column' => 'menu_order',
		'order' => 'desc'
	);

	$temp = $wp_query;  // assign original query to temp variable for later use
	$wp_query = null;
	$wp_query = new WP_Query($args); 

	$i = 0;
	$blog_items_output = '';
	if ($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post();							

		// get full image from featured image if was not see full image url in blog post
		$get_custom_options = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '', false );
		$image_preview_url = $get_custom_options[0];
		$blog_image_thumb = get_the_post_thumbnail($post->ID, 'portfolio_3', array('alt' => the_title_attribute('echo=0')));

		/*$custom = get_post_custom($post->ID);
		$blog_video_url = $custom["blog_video_url"][0];
		if ($blog_video_url) $image_preview_url = $blog_video_url;*/

		$blog_content_excerpt_length = 78;
		if ($post->post_excerpt) { $post_description = $post->post_excerpt; } else { $post_description = $post->post_content; }
		if ( strlen($post_description) > $blog_content_excerpt_length ) {
			$post_description = substr($post_description, 0, $blog_content_excerpt_length).'...';
		}

		$write_comments = '0 '.__('Comments','extensio');
		$num_comments = 0;
		$num_comments = get_comments_number($post->ID);
		if ( comments_open() ) {
		if($num_comments == 0) {
		  $comments ='0 '.__('Comments','extensio');
			} elseif($num_comments > 1) {
			  $comments = $num_comments.__(' Comments','extensio');
			} else {
			   $comments ='1 '.__('Comment','extensio');
			}
			$write_comments = $comments;
		}

		$i++;

		if ($i == 1) {
			$content_length = $mainpost_contentlength;
		} else { $content_length = $otherposts_contentlength; }

		//$content = $post->post_content;
		if (!$excerpt = trim($post->post_excerpt)) {
			$excerpt = $post->post_content;
			$excerpt = strip_shortcodes( $excerpt );
			$excerpt = apply_filters('the_content', $excerpt);
			$excerpt = str_replace(']]>', ']]>', $excerpt);
			$excerpt = strip_tags($excerpt);
			$excerpt_length = apply_filters('excerpt_length', 55);
			$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');

			$words = preg_split("/[\n\r\t ]+/", $excerpt, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
			if ( count($words) > $excerpt_length ) {
				array_pop($words);
				$excerpt = implode(' ', $words);
				$excerpt = $excerpt . $excerpt_more;
			} else {
				$excerpt = implode(' ', $words);
			}
		}
		$content = $excerpt;

		//$content = apply_filters('the_content', $content);
		$excerpt = str_replace(']]>', ']]>', $content);
		if ( strlen( $excerpt ) > $content_length ) {
			$subex = substr( $excerpt, 0, $content_length - 5 );
			$exwords = explode( ' ', $subex );
			$excut = - ( strlen( $exwords[ count( $exwords ) - 1 ] ) );
			if ( $excut < 0 ) {
				$content = substr( $subex, 0, $excut ).' ...';
			} else {
				$content = $subex.' ...';
			}
		} else {
			$content = $excerpt;
		}
		$content = str_replace('<p>','',$content);
		$content = str_replace('</p>','',$content);

		$article_post_class = ' style="margin-bottom:25px;"';

		if ($i == 1) {
			$blog_items_output .= '
				<article class="post"'.$article_post_class.'>
					<figure class="visual">'.$blog_image_thumb.'</figure>
					<h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>
					<p>'.$content.'</p>
					<nav class="nav-blog">
						<ul>
							<li><a href="'.get_permalink().'">'.__('Read more','extensio').'</a></li>
							<li><a href="'.get_permalink().'#comments">'.$write_comments.'</a></li>
						</ul>
					</nav>
				</article>
				<div class="container">
			';
		} else {
			$article_post_class = ($i == $count) ? ' style="margin-bottom:25px;"' : '';
			$blog_items_output .= '
					<article class="post"'.$article_post_class.'>
						<h3><a href="'.get_permalink().'">'.get_the_title().'</a></h3>
						<p>'.do_shortcode($content).'</p>
						<nav class="nav-blog">
							<ul>
								<li><a href="'.get_permalink().'">'.__('Read more','extensio').'</a></li>
								<li><a href="'.get_permalink().'#comments">'.$write_comments.'</a></li>
							</ul>
						</nav>
					</article>
			';
		}

	endwhile;
	endif;	

	$wp_query = null;
	$wp_query = $temp;

	$title = ($title) ? '<h3>'.$title.'</h3>' : '';

	return '
		<section class="latest-blog">
			<div class="headline center solid">
				'.$title.'
			</div>
			<div class="area">
				'.$blog_items_output.'
				</div>
			</div>
		</section>
	';
}
add_shortcode('latestposts', 'theme_latestposts');

Viewing all articles
Browse latest Browse all 8245

Trending Articles