Wordpress permalink strange behavior: output depends on <div> - php

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 –

Related

If (is_page_template()) not working in header.php

I have 3 separate header options, all with a banner image:
1. Homepage
2. Sponsor Template
3. All other pages.
I have placed the below code in the header. The homepage and all other pages are working as expected, but I can't seem to make the Sponsor template work (the class="sponsor-title" is not appearing).
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if ( is_front_page()): ?>
<span class="home"><h1><?php echo event_title(); ?></h1></span>
<span class="tag-line"><?php the_field('tag_line'); ?></span>
<span class="date"><?php the_field('date_time_header'); ?></span>
<?php
$ticket = get_field('ticket_url');
if ( $ticket ):
$ticket_url = $ticket['url'];
$ticket_title = $ticket['title'];
?>
<a class="button" href="<?php echo esc_url($ticket_url); ?>"><?php echo esc_html($ticket_title); ?></a>
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php endif; ?>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif;?>
What have I done wrong? I want to ensure that when the template or page is chosen that the correct styling appears, as it is very different from the other pages.
Your problem is the syntax of your if statement. Try this instead:
<?php if (!is_page_template('page-templates/all-sponsor-template.php')) { ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php } else { ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php } ?>
I always use the {..} brackets, as it's easier to follow and understand the logic of the code. However, to rewrite your code using the correct structure of the if statement you've attempted, it would look like this:
<?php if (!is_page_template('page-templates/all-sponsor-template.php')): ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif; ?>
And, of course, the exclamation mark here:
if (!is_page_template('page-templates/all-sponsor-template.php'))
means "If NOT page template all-sponsor-template.php" so remove it if you are checking for TRUE.
Finally, keep in mind that due to certain global variables being overwritten during The Loop, is_page_template() will not work in The Loop. However, if this code is in your header.php, then you should be fine.
<?php if (!is_page_template('page-templates/all-sponsor-template.php')); ?>
// Stuff
<?php endif; ?>
Here you are checking if the page template is NOT page-templates/all-sponsor-template.php, and if it isn't - then you show the sponsor title...
I might be confused, but shouldn't it be like this?
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
// Stuff
<?php endif; ?>
I.e. swapping the if condition so that the sponsor title shows if the page template IS the mentioned file.
Edit:
The if statement that checks if the sponsor page template is being used, is inside the if statement that checks if the current page is the home page.
If you move the template check one level up, it should do the trick:
<?php if ( has_post_thumbnail()) : ?>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
<?php if ( is_front_page()): ?>
<span class="home"><h1><?php echo event_title(); ?></h1></span>
<span class="tag-line"><?php the_field('tag_line'); ?></span>
<span class="date"><?php the_field('date_time_header'); ?></span>
<?php
$ticket = get_field('ticket_url');
if ( $ticket ):
$ticket_url = $ticket['url'];
$ticket_title = $ticket['title'];
?>
<a class="button" href="<?php echo esc_url($ticket_url); ?>"><?php echo esc_html($ticket_title); ?></a>
<?php endif; ?>
<?php if (is_page_template('page-templates/all-sponsor-template.php')); ?>
<span class="sponsor-title"><h1><?php echo event_title(); ?></h1></span>
<?php endif; ?>
<?php else: ?>
<span class="page-title"><h1><?php the_field('page_header'); ?></h1></span>
<span class="sub-header"><?php the_field('sub_header'); ?></span>
<?php endif;?>
This way it will check if the sponsor template is being used on all pages and not only when on the home page.

Wordpress post loop

So im trying to add a function called "latest posts" in a sidebar section.
This is how my code looks like in sidebar.php-file.
<?php
query_posts('category = all');
if (have_posts()) :
while (have_posts()) : ?>
<?php the_post();
the_excerpt(); ?>
<p class="datum2"><?php the_time('Y-m-d'); ?></p>
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
<p class="textnyhet2">
</p>
<?php endwhile;
endif;
?>
My problem is, no matter how much I move around in the code I just cant seems to get the correct title/date to appear above the correct post.
Now it's like the post comes first, then right under the post the date and title appears above another incorrect post etc.
Example:
Latest post text
space---->
title/date, (though to the post above)
latest post text
space----->
and continue like that.
Would be grateful if anyone could help me out.
Thanks!
The reason the title/date appears below the post is because that's the way you have specified it to appear. Change
<?php the_post();
the_excerpt(); ?>
<p class="datum2"><?php the_time('Y-m-d'); ?></p>
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
<p class="textnyhet2">
</p>
to
<?php the_post(); ?>
<p class="datum2"><?php the_time('Y-m-d'); ?></p>
<p class="Nyhetsrubrik3"><a style="orange"; href="<?php the_permalink() ?>" ><?php the_title(); ?></a></p>
<?php the_excerpt(); ?>
<p class="textnyhet2">
</p>
It should display the way you want it.
the_excerpt(); is printing out an excerpt of the post, then the time/title displays. Now it should display the time and title first, then display the excerpt of the post.

Making an image have a link

I`m new to this website and I was hoping to get my first question out and get some feedback on my issue.
I am creating a "slider" which you can find here: http://complusoft.net/demo-catarroja2/es/ (at the very beginning of the page) and i`m trying to make the images which pass through the slide to be a link.
Here you can find the code which I have done:
<?php foreach ($items as $key=>$item): ?>
<?php if($params->get('itemImage') && isset($item->image)): ?>
<a class="moduleItemTitle" href="<?php echo $item->link; ?>">
<img src="<?php echo $path1;?>?src=<?php echo $item->image;?>&w=635&h=386&zc=1&q=100" alt="<?php echo K2HelperUtilities::cleanHtml($item->title); ?>" title="#htmlcaption<?php echo $key;?>"/>
</a>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php foreach ($items as $key=>$item): ?>
<div id="htmlcaption<?php echo $key;?>" class="nivo-html-caption">
<?php if($params->get('itemTitle')): ?>
<!--<h2><a class="moduleItemTitle" href="<?php echo $item->link; ?>"><?php echo $item->title; ?></a></h2>-->
<?php endif; ?>
<div class="clr"></div>
<?php if($params->get('itemIntroText')): ?>
<div class="description-slider2">
<p><?php echo $item->introtext; ?></p>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
Ive looked around the internet and besides my own knowledge I know this should be the correct way besides the point that this is simple HTML I believe, I may be wrong please correct me if so.

do_shortcodes in Wordpress

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
}

Loop add .last class to last item in loop

Im using wordpress and using the following to get the last 3 most recent posts:
<?php query_posts('showposts='.$latest_num.'&cat=-'.$featured_cat.','.$latest_ignore.''); ?>
<?php while (have_posts()) : the_post(); ?>
<li>
<div class="imgholder">
<a href="/wp-content/themes/twentyten/images/slide1.jpg" data-gal="prettyPhoto[featured]" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" width="275" height="145" alt="Post Image" class="postimg-s" />
</a>
</div>
<h4><?php the_title(); ?></h4>
<p><?php the_content('Read more...'); ?></p>
</li>
<?php endwhile; ?>
What I want to do is add a class named 'last' to the <li> element on the 3rd interation through the loop.
Anyone got any ideas how I could add this?
Setup a counter outside your while loop
$count = 1;
Check the modulus of that counter and output the class if required
<li <?php if(!$count % 3) echo 'class="last"; ?>>
Increment your counter before closing the loop:
$count++;
}
Or, applied to your code:
<?php
$count = 1;
while (have_posts()) : the_post();
?>
<li <?php if(!$count % 3) echo 'class="last"; ?>>
<div class="imgholder">
<a href="/wp-content/themes/twentyten/images/slide1.jpg" data-gal="prettyPhoto[featured]" title="<?php the_title(); ?>">
<img src="<?php echo get_post_meta($post->ID, 'thumbnail',true) ?>" width="275" height="145" alt="Post Image" class="postimg-s" />
</a>
</div>
<h4><?php the_title(); ?></h4>
<p><?php the_content('Read more...'); ?></p>
</li>
<?php
$count++;
endwhile;
?>
The counter-intuitive look of the modulus condition is that whenever the counter is divisible by exactly 3 it will return 0.
Replace the line
<li>
with
<li <?php print have_posts() ? '' : ' class="last"' ?>>
The have_posts() simply calls into
$wp_query->have_posts() which checks a
loop counter to see if there are any
posts left in the post array (source)
Li should be li
<li <?php $iCounterLi++; ($iCounterLi==3)?'class="last"':''; ?>>
Don't forgot to initialize the $iCounterLi before the loop

Categories