do_shortcodes in Wordpress - php

I'm having some trouble getting my "WP Private" plugin to work through my TEMPLATE, not a post/page. When searching Google, I have no problems with finding out how to implement a SINGLE shortcode, but I'm not able to find how to implement an opening/closing short tag, which I know is quite common.
Here's my code. I must be having another syntax issue! The opening/closing tags that I used in this case have proven successful on another website I tried. But it's not working in this case.
<?php echo do_shortcode ('[protected]
<!--<h2><?php the_title(); ?></h2>-->
<ul style="border-bottom: 1px solid #d8d8d8" class="<?php echo get_option('minimax_list_layout'); ?>">
<?php
query_posts(array ('post__in' => array( 569)));
if (have_posts()) : while (have_posts()) : the_post();
?>
<li style="border-bottom: 1px solid #d8d8d8; padding-bottom:20px" class="clearfix">
<?php if ( get_option('minimax_list_layout') == 'style-two' ) { ?>
<h3 style="font-family:nobile; font-weight:normal; font-size:1.8em"><a style="text-decoration:none" title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<cite><?php the_time('d M Y') ?> </cite>
<?php if ( has_post_thumbnail() ) { ?>
<a title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_post_thumbnail('thumb_post_wide', 'class=head'); ?></a>
<?php } ?>
<p style="font-family:nobile; font-size:1.15em"><?php echo ShortenText( $post->post_content, 300 ); ?></p>
<a class="detail" href="<?php the_permalink() ?>">Continue reading</a>
<?php } else { ?>
<?php if ( has_post_thumbnail() ) { ?>
<a title="Permanent Link to <?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_post_thumbnail('thumb_post_1'); ?></a>
<?php } ?>
<div class="post-summary">
<h3 style="font-family:nobile; font-weight:normal; font-size:1.8em"><a style="text-decoration:none" title="<?php the_title(); ?>" href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
<!--<cite style="font-style:normal; font-weight:bold; font-family:nobile"><?php the_time('d M Y') ?> </cite>-->
<p style="font-family:nobile; font-size:1.15em"><?php echo ShortenText( $post->post_content, 220 ); ?></p>
<a style="font-family:nobile" class="detail" href="<?php the_permalink() ?>">Continue reading</a>
</div><!-- end post-summary -->
<?php } ?>
</li>
<?php endwhile; ?>
<?php if (show_posts_nav()) : ?>
<div id="post-navigation" class="clearfix">
<span class="previous"><?php next_posts_link('Older Entries') ?></span>
<span class="next"><?php previous_posts_link('Newer Entries') ?></span>
</div>
<?php endif; wp_reset_query(); ?>
<?php else: ?>
<p><?php _e('Sorry, no pages matched your criteria.'); ?></p>
<?php endif; ?>
</ul><!-- end posts-list -->
[/protected]') ?>

You are writing very weird things here. By doing this:
echo do_shortcode ('[protected]
<!--<h2><?php the_title(); ?></h2>-->
you are submitting the php code of the template as a string to the function and then echo it, but it is not going to be interpreted by PHP engine. Moreover, the first single quote will create a syntax error. You have to enable ob (output buffering), run the template code, get the result from the buffer, wrap it in your shortcode and than submit the result to function do_shortcode.
http://www.php.net/ob_start and http://www.php.net/ob_get_clean
ps: and I still think that you do not need it. Why do you need a shortcode here, if you can use "if" statement and just skip the whole section of the code? I do not know what you are doing in your plugin, but if you are trying to hide the part of the template then "if" is the easiest way to implement your idea.
if (check_if_allowed())
{
// your part of template is here
}

Related

Error accessing my widgets.php in wordpress [duplicate]

I have been trying to show the posts of my website in a grid view in one of the category page. For doing so, i implemented the following code:
<div class="row-fluid">
<div class="span6 featured-block">
<?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
<a class="featured_image" href="<?php the_permalink();?> "target="_blank">
<?php if ((function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) {
$image_id = get_post_thumbnail_id();
$image_url = wp_get_attachment_image_src($image_id,'large');
$image_url = $image_url[0];
if(!#getimagesize($image_url)){
$image_url = "/wp-content/uploads/2014/02/logo2.jpg";
}
$blogurl = get_bloginfo('url');
$image_url = str_replace($blogurl, '', $image_url);
?>
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo ($image_url); ?>&w=390&h=216&zc=1&q=100" alt="<?php echo the_title(); ?>" />
<?php }
else { ?>
<img src="<?php echo bloginfo('template_url'); ?>/thumb.php?src=<?php echo "http://www.nepaljapan.com/wp-content/uploads/2014/02/logo2.jpg"; ?>&w=390&h=216&zc=1&q=100" alt="<?php echo the_title(); ?>" />
<?php }?>
</a>
<a href="<?php the_permalink();?>" target="_blank">
<h2><?php the_title(); ?></h2></a>
<p><?php echo excerpt(37); ?>
<?php endwhile; ?>
</div>
</div>
But i am facing the problem with this code.
This gives out the error as
Fatal error: Call to a member function have_posts() on a non-object in /home/npjp/public_html/wp-content/themes/nepaljapan/content-category.php on line 15
The line 15 on my code is
<?php while($block1Featured->have_posts()): $block1Featured->the_post(); ?>
What is the problem? Am i actually doing it correct??
Much confused.
Please help me.
Thank you in advance
Your custom query has some problems. You can use this guid to write custom queries in wordpress:
<?php
$block1Featured= new WP_Query( 'category_name=staff+news' );
if ( $block1Featured->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $block1Featured->have_posts() ) : $block1Featured->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Wordpress custom field - css in loop

I try to insert the value of a custom field in a background-image property ( therefore not in img src="..."').
In my category.php; I can display the custom field linked to each post; but when I put the variable in my style ( inline css ), wordpress always displays the same image.
The code :
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<style type="text/css">
.photo_interview {
background-image: url(<?php echo $photo_interview; ?>);
}
</style>
<div class="photo_interview"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
Any idea ? My page here : http://www.overso.me/category/interview/
Your code should be this:
<?php
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="interview">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<?php $photo_interview = get_field('photo_apercu', $post->ID); ?>
<?php echo $photo_interview; ?>
<?php the_title(); ?>
<!-- You don't need style tags if you only want to set the background image -->
<div style="background-image: url(<?php echo $photo_interview; ?>)"></div>
</a>
</div>
<?php endwhile;
else: ?>
<?php endif; ?>
For what I can see you haven't set the global $post, so get_field do not know which is needed to display. in this case it will be better to use the_ID() to get the current post ID inside the while loop.
Cheers

Wordpress permalink strange behavior: output depends on <div>

This is really strange. the_permalink(); works well into the wordpress loop (Code below). it outputs the right URL- but for some reason in the same loop, without changing query etc, the permalink only outputs the first post.
Here is the code:
<?php echo the_permalink(); ?>//This outputs **right** - http://domain.de/?p=18
<?php echo $link;?>
<span class="share_overlay">
<?php echo the_permalink(); ?>//This outputs **wrong** - http://domain.de/?p=18
<?php echo $link;?>
So what am I doing wrong? There is nothing between... even if I only echo -> the Permalink into .share_overlay I got the same result. Without echo - same result. writing the_permalink outside of .share_overlay - the link is alright.
There is no code - no little piece of code into the function.php - even no JS running that manipulates the DOM.
Here is a bit more of my code, so that if I have n error you can punch my head on the foot ;)
$link was only to see if the result is the same. I can't see an error themes very strange to me.
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<?php $link = get_permalink($post);?>
<article class="post">
<h3><?php the_title(); ?></h3>
<em class="meta">
<span class="icn time" title="Datum"></span><time datetime="<?php the_time('Y-m-d') ?>"><?php the_date(); ?></time>
|
<span class="icn cat" title="Kategorie"></span><span class="categorie">
<?php the_category(' '); ?>
</span>
|
<span class="icn social" title="Teilen"></span><span class="share">Teilen
<?php echo the_permalink(); ?>//right
<?php echo $link;?>//right
<span class="share_overlay">
<?php echo the_permalink(); ?>//wrong
<?php echo $link;?>//wrong
<strong>Teile diesen Inhalt auf:</strong>
<span class="share_content">
<a class="t" target="_blank" href="https://twitter.com/share?url=&text=<?php echo $link; ?>: &hashtags=<?php if(has_tag()){$posttags = get_the_tags();if($posttags){foreach($posttags as $tag){ echo $tag->name . ',';}echo'design';}}?>">Tweet</a>
<a class="g" target="_blank" href="https://plus.google.com/share?url=<?php echo $link; ?>">Google+</a>
<a class="f" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=<?php echo $link; ?>">Facebook</a>
</span>
<span class="close"></span>
</span>
</span>
</em>
<?php echo the_permalink(); ?>//right again?!
<?php echo $link;?>//right again?!
<?php the_excerpt(); ?>
<a class="more" title="weiter lesen" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
</article>
<?php endwhile; else : ?>
<article class="post">
<h3>No entries found here so far.</h3>
<p>No results found.</p>
</article>
<?php endif; ?>
solved It was not a problem of wordpress itself I think.. set wp_reset_query before the loop -> everything works now. I think I have got something messed up before so I have to look after. Thanks for the comments –

Print category of post Wordpress

I have try all the functions related to print categories that the codex provide, but i havent found any way that works for me.
Im trying to print the categories slug for put it inside a class.
I want to print the category of the actual post in div with the class proyect, so then i can use it to filter with isotope.
<!-- feature posts -->
<div id="container">
<?php $the_query = new WP_Query('showposts=5&orderby=post_date&order=DESC'); ?>
<?php while ($the_query->have_posts()) : $the_query->the_post();
$id = get_the_ID(); ?>
<div class="proyect <?php wp_get_post_categories($id); ?>">
<div class="view view-tenth">
<a style="display:block;" href="<?php the_permalink(); ?>">
<article> <?php if ( function_exists("has_post_thumbnail") && has_post_thumbnail() ) { the_post_thumbnail('', array("class" => "")); } ?></article>
</a>
<div class="mask">
<h2><?php echo substr(strip_tags(get_the_title()),0,35); ?></h2></a>
<p class="fecha-post"><?php the_time('F j, Y'); ?></p>
<?php echo substr(strip_tags(get_the_content()),0,100); ?>
<a class="info" href="<?php the_permalink(); ?>">Ver más...</a>
</div>
</div>
</div>
<?php endwhile;?>
</div>
<!-- #feature post -->
From http://wordpress.org/support/topic/getting-category-slug-from-posts-in-the-loop:
<li class="<?php foreach(get_the_category() as $category) { echo $category->slug . ' ';} ?>">
You should be able to use:
$cats = wp_get_post_categories($post->ID);
This will be an array of the categories associated with this post. Then you can loop through them and do whatever you need.

How do I remove the most recent post from the sidebar in Wordpress?

On both the front page and the blog page - the sidebar shows the most recent post, which I find doesn't look very good duplicated against the same post expanded on the main page.
This is my code for the sidebar:
<div class="blog-sidebar">
<?php query_posts('showposts=5'); ?>
<?php while (have_posts()) : the_post(); ?>
<div class="blog-sidebar-feature">
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>" rel="bookmark" title=""><?php the_title();
?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
</div>
<?php endwhile;?>
<br />
<?php wp_pagenavi(); ?>
</div>
and the relevant code for how the blog appears on the home page:
<div class="blog-sidebar">
<div class="blog-sidebar-feature">
<?php query_posts('orderby=date&order=DESC&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?>
<?php if ( has_post_thumbnail() ) { ?>
<div class="blog-sidebar-image"><?php the_post_thumbnail('medium'); ?></div>
<?php
}
?>
<div class="blog-sidebar-content">
<p class="date"><?php the_time('F j, Y') ?></p>
<h3 <strong><?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?></strong></h3>
<h2 <p><a href="<?php the_permalink() ?>"
rel="bookmark" title=""><?php the_title(); ?></a></p></h2><?php echo get_excerpt(166); ?>
</div>
<?php endwhile;?>
</div>
</div>
<div id="connect">
<?php query_posts('page_id=1');
while (have_posts()): the_post();
the_content();
endwhile;
wp_reset_query(); ?>
</div>
Is there any way to remove only the most recent post from the sidebar when it appears in full on the main container? Thanks in advance for any help.
UPDATE V2
So you do want recent posts, just not the post currently showing in the main content.
UPDATE V3:
This should work now. I had to change arguments of query_posts to array to make it work.
Try it now:
<?
global $wp_query;
$skip_posts=array();
if (is_single()) //only exclude posts when single post is shown
$skip_posts[]=$wp_query->post->ID;
?>
<?php query_posts( array( 'showposts'=>5,'post__not_in'=>$skip_posts)); ?>
<?php query_posts('posts_per_page=5&offset=1'); ?>
Thanks to 850010 for all the help, I went back and had a look at the offset rule and the 'array' wasn't needed. Deceptively simple.

Categories