I have a code chunk that is inside the_content(); I'm using acf repeater as well. So when I post a blog, I'll either use the_content(); or the acf field. I have h2 tag ( latest articles ) that I only want printed one time, but it's printing everytime I make a post.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="container">
<div class="row">
<div class="col-md-4 sidebar-r">
<?php echo the_content(); ?>
</div><!-- end sidebar-r -->
<?php
$i = $wp_query->post_count;
if($i <=1) {
echo '<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>';
}else{
echo '';
}
?>
<div class="col-md-8 links-wrap">
<?php if(have_rows('daily_links')): ?>
<?php while(have_rows('daily_links')): the_row(); ?>
<a href="<?php the_sub_field('link_url'); ?>" target="_blank">
<h2 class="link-title">
<?php the_sub_field('link_title'); ?>,
</h2>
<h3 class="link-source">
<?php the_sub_field('link_source'); ?>
</h3>
</a>
<?php endwhile; ?>
<?php endif; ?>
</div><!-- end links wrap -->
</div><!-- end row -->
</div><!-- end container -->
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>
You'll see I tried using php to count the posts and if more than one post, don't print the tag, but couldn't figure out the exact logic and syntax.
I am honestly struggling a bit to understand exactly what you are trying to do and since I do not even have the posts and other key pieces of information so that I can properly replicate your issue so that I can help you better, this is a little bit challenging. That being said, looking into some ideas I came across another stackoverflow question/answer that might be relevant for you in catching the first post and does something to it. The answer to the referenced question instance was this:
<?php if (have_posts()) : $postCount = 1; while (have_posts()) : $postCount++; ?>
<?php if($postCount == 2) { ?>
// SOMETHING TO DO WITH FIRST POST
<?php } else { ?>
// SOMETHING TO DO WITH ALL OTHER POSTS
<?php } ?>
This was suggested by user Bora in this answer from 2013.
Let me know if that helped!
Related
I'm trying to create a different layout on my blog posts page depending on when the post is created. so blog post 1 will have one class while post two will have another and post 3 will have a final class. hard to explain but here is my code hopefully it will make more sense:
<?php
if(have_posts()):
while(have_posts()): the_post();
$counter = 1;
?>
<?php if($counter == 1){ ?>
<div class="new-row">
<div class="boxes picture-box contact-smaller-box">
<?php the_post_thumbnail(); ?>
</div><!--/.picture box-->
<div class="boxes white-box contact-smaller-box">
<div class="box-inner right-side">
<h2><?php the_title(); ?></h2>
<p><?php echo substr(get_the_excerpt(), 0,70); ?></p>
Read More
</div><!--/.box inner-->
<div class="arrow arrow-left"><img src="..."></div>
</div><!--/.white box-->
</div><!--/.new row-->
<?php $counter = $counter++; ?>
<?php } if($counter == 2){?>
<!-- second section of the blog post content -->
<div class="new-row">
<div class="boxes pink-box contact-smaller-box">
<div class="box-inner">
<h2><?php the_title(); ?></h2>
<p><?php echo substr(get_the_excerpt(), 0,70); ?></p>
Read More
</div><!--/.box inner-->
<div class="arrow arrow-right"><img src="..."></div>
</div><!--/.pink box-->
<div class="boxes picture-box contact-smaller-box">
<img src="..." alt="hearing aids for adults">
</div><!--/.picture box-->
</div><!--/.new row-->
<?php } ?>
<?php
endwhile;
endif;
?>
At the moment both of my test posts are showing as the first type of post, so im thinking my counter is not working correctly, or i have my divs in the wrong position to make this work. im new at working with php so i dont know where i am going wrong. any advice would be brilliant.
After about 4 hours of research and testing different things out i have found a solution (credit):
<?php if (have_posts()) : ?>
<?php $count = 0; ?>
<?php while (have_posts()) : the_post(); ?>
<?php $count++; ?>
<?php if ($count == 1) : ?>
Add your Custom Post Divs Here for the 1st post.
<?php elseif ($count == 2) : ?>
Add your Custom Post Divs Here for the 2nd post.
<?php elseif ($count == 3) : ?>
Add your Custom Post Divs Here for the 3rd post.
<?php elseif ($count == 4) : ?>
Add your Custom Post Divs Here for the 4th post.
<?php else : ?>
Add your Custom Post Divs Here for the rest of the posts.
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
hope it helps someone else out.
I'm trying to make a custom search page, by creating all new search.php file, for my wordpress template...so far, so good.
The problem is that when I search for something it doesn't show any results.
I'm guesing that it has something to do with some php script or something I don't know.
How can I fix that ?
P.S The function for the number of the results works fine, but there isn't any results.
Here is the content of search.php
<?php
get_header();
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<?php endwhile; ?>
<?php else : ?>
<?php _e( 'Nothing Found' ); ?>
<?php endif; ?>
<?php
get_footer();
?>
The problem is that you don't have anything in your loop to print the results, i.e.
<?php while (have_posts()) : the_post(); ?>
<h1>Search Results</h1>
<!-- Needs something here -->
<?php endwhile; ?>
To fix the problem, simple replace <!-- Needs something here --> with the following
<a href="<?php the_permalink() ?>">
<h2><?php the_title(); ?></h2>
</a>
<p><?php the_excerpt(); ?></p>
You also need to move <h1>Search Results</h1> to above the loop to stop it from displaying multiple times. It may be best to move it above the if statement if you don't intend on adding it to your else statement as well.
Hi there, I have a little bit of a problem. The front page of our new site has a series of thumbnail boxes with rollovers to open certain showcased products. The loop I have written shows all posts currently but will eventually be tied down only to the portfolio category.
My manager however wants a slider with recent blog posts after the third post and a couple of more sliders with references and suchlike after the 5th and 7th posts. I found a little bit of code that injects the same snippet every n posts but this is no good for me because I only want it to happen 3 times and all with different content, including one with a loop inside a loop (where presumably time will move mega slowly haha). Is this possible? If so, can anybody point me towards a code snippet?
Current Loop
<!-- Start of loop -->
<?php if (have_posts()) : ?>
<!-- Start of Post -->
<?php while (have_posts()) : the_post(); ?>
<!-- Check to see if there is featured image -->
<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>
<?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array( 960,960 )); ?>
<!-- End Checking -->
<div class="portfolioblock" style="background-image: url('<?php echo $img_src[0]; ?>');">
<a href="<?php the_permalink(); ?>">
<div class="rollover">
<div class="center">
<img src="<?php bloginfo('template_url'); ?>/img/zoom.png" alt="More" />
</div>
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</div>
</a>
</div>
<!-- Start Content Block -->
<?php } else { ?>
<div class="block">
<h2><?php the_title(); ?></h2>
<p><?php the_excerpt(); ?></p>
</div>
<?php } ?>
<!-- End Content Block -->
<?php endwhile; ?>
<!-- End of Post -->
<?php else : ?>
//Something that happens when a post isn’t found.
<?php endif; ?>
<!-- End of Loop -->
And I found this code for injecting a code snippet every n posts.
<?php $postnum++; if($postnum%5 == 0) { ?>
YOUR AD CODE HERE
<?php } ?>
You could count the number of posts that passed and then decide on that:
$postnum++;
if ($postnum == 3 || $postnum == 5 || $postnum == 7) {
echo 'foo';
}
Elaborating on cweiske's answer:
Inserted just before endwhile
<?php $postnum++;
if ($postnum == 4) { ?>
<div class="block"><h2>Blog</h2></div>
<?php }
if ($postnum == 6) { ?>
<div class="block"><h2>References</h2></div>
<?php }
if ($postnum == 9) { ?>
<div class="block"><h2>Meet the team</h2></div>
<?php }; ?>
Basically what you want to do is this.
Before the loop begins, initialise a counter variable to 0.
Each time the loop successfully finds a post, increment the variable.
Now, each loop, you will know how many posts have been displayed.
So at the appropriate place, you check the counter to see if it matches either 3 5 or 7. If it does match, you display the correct block.
I seem to be having difficulty.
I need to show a specific piece of text if there are more than or equal to 3 post on a post page template in wordpress. (loop-single.php)
It should be dynamic enough to detect if the total number of post in related category is greater or equal to 3.
here is a code I found which works well on category template pages(archive.php) but it messes up when I use it in a post template.
<?php
$count = 1;
if (have_posts()) : while(have_posts()): the_post(); ?>
<!-- Less than 3 post - nothing shown at all -->
<?php $count++;
endwhile; endif; ?>
<?php if ($count > '3') { ?>
<div> This line shown when 3 or more posts are in current post category</div>
<?php } ?>
NOTE: I'm trying to get this to work on the loop-single.php template file.
Any help would be greatly appreciated,
Thank You
Code updated to include above solution. I fixed a few syntax errors, but its now throwing a T-STRING error: Parse error: syntax error, unexpected T_STRING
here is my full page code:
<?php /* Start loop */ ?>
<?php while (have_posts()) : the_post(); ?>
<?php roots_post_before(); ?>
<article <?php post_class() ?> id="post-<?php the_ID(); ?>">
<?php roots_post_inside_before(); ?>
<header>
<h1 class="entry-title"><?php the_title(); ?></h1>
<!-- POST DATE STAMP -->
<div class="post-top">
<div class="date-stamp">
<b><?php the_time('M d'); ?></b>
</div>
</header>
<div class="entry-content">
<?php the_content(); ?>
</div>
<footer>
<hr />
<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{
<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) -->
<h5>Featured: <?php $cats=get_the_category(); echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />
}
else
{
Why hello there LESS than three
}
?>
</footer>
<?php comments_template(); ?>
<?php roots_post_inside_after(); ?>
</article>
<?php roots_post_after(); ?>
<?php endwhile; /* End loop */ ?>
This should get you started:
<?php
$cat = get_query_var('cat');
$posts = get_posts(array('category' => $cat));
if(count($posts) >= 3)
{
//CODE EXECUTED IF THREE OR MORE POSTS EXIST IN CURRENT CATEGORY
}
else
{
//CODE EXECUTED IF LESS THAN THREE POSTS EXIST IN CURRENT CATEGORY
}
?>
EXTRA INFO: The reason why it was failing was because your loop was only performing one iteration. Single posts won't go through the loop more than once because.... well...... it's a single post. What this approach does is take the existing category, and queries all Wordpress posts in the matching category. Using PHP's count function will give you the exact number of posts found with the given parameters.
Word of warning: the script above will not find ALL posts in the matching category. Only the five most recent ones in that given category. If you want an actual total of all matching posts, change one line to the following:
$posts = get_posts(array('category' => $cat, 'numberposts' => -1));
UPDATES TO CODE: This line:
<article <?php post_class(); ?> id="post-<?php the_ID(); ?>"> //missing semicolon after post_class()
And this block:
<?php
$cats=get_the_category();
$posts = get_posts(array('category' => $cats[0]->cat_ID));
if(count($posts) >= 3)
{
?>
<!-- POST FOOTER RELATED CONTENT (2 HORIZONTAL) -->
<h5>Featured: <?php echo $cats[0]->cat_name; ?></h5>
<div id="foot-container">
<?php echo do_shortcode("[catlist categorypage=yes comments=yes numberposts=2 class=horizontal-2 offset=2 orderby=date order=desc thumbnail=yes thumbnail_size=75 thumbnail_class=footer-thumb title_tag=p title_class=footer-link comments_tag=p comments_class=comment-count]"); ?>
<div style="clear:both;"></div>
</div>
<hr />
<?php
}
else
{
echo 'Why hello there LESS than three';
}
?>
This is my first major WordPress site from absolute scratch. My PHP skills are only starting out and this is the error I am getting. I know what the problem is, I just don't know how to write this block of PHP code correctly and would appreciate some help please.
As far as I have read, I cannot put html markup into a block of PHP code like I have done here which is odd, as all the example WP Loops I have seen has HTML markup in it.
<?php
/**
* Template Name: Home Page
*
* This Full Width template removes the primary and secondary asides so that content
* can be displayed the entire width of the #content area.
*
*/
// calling the header.php
get_header();
// action hook for placing content above #container
thematic_abovecontainer();
?>
<div id="container">
<?php thematic_abovecontent(); ?>
<div id="content" class="home-content">
<?php
// calling the widget area 'page-top'
get_sidebar('page-top');
the_post();
thematic_abovepost();
?>
<div id="post-<?php the_ID();
echo '" ';
if (!(THEMATIC_COMPATIBLE_POST_CLASS)) {
post_class();
echo '>';
} else {
echo 'class="';
thematic_post_class();
echo '">';
}
// creating the post header
// thematic_postheader();
?>
<div class="entry-content">
<?php
the_content();
wp_link_pages("\t\t\t\t\t<div class='page-link'>".__('Pages: ', 'thematic'), "</div>\n", 'number');
edit_post_link(__('Edit', 'thematic'),'<span class="edit-link">','</span>') ?>
</div>
</div><!-- .post -->
<div class="featureBlocks">
<div class="news">
<div class="featTitle">
<h3>News <br />& Events</h3>
<div class="featSubTitle">What's happening in the world of Process Engineering</div>
<div class="clear"></div>
</div>
<?php
query_posts('cat=4&posts_per_page=1');
if (have_posts()) : while (have_posts()) : the_post();
the_title();
the_content();
/*<a href='<?php the_permalink(); ?>' rel='bookmark' title='Permanent Link to <?php the_title_attribute(); ?>'><?php the_title(); ?></a>*/
endwhile; endif;
// Reset Query
wp_reset_query();
?>
</div>
<div class="research">
<div class="featTitle">
<h3>Research <br />Highlights</h3>
<div class="featSubTitle">Find out what exciting research is happening Process Engineering</div>
<div class="clear"></div>
</div>
<?php
query_posts('cat=5&posts_per_page=1');
if (have_posts()) : while (have_posts()) : the_post();
<strong>the_title();</strong>
the_content();
endwhile; endif;
// Reset Query
wp_reset_query();
?>
</div>
<div class="studentStories">
<div class="featTitle">
<h3>Student <br />Stories</h3>
<div class="featSubTitle">Student life at the Department of Process Engineering</div>
<div class="clear"></div>
</div>
<?php
query_posts('cat=6&posts_per_page=1');
if (have_posts()) : while (have_posts()) : the_post();
the_title();
the_content();
endwhile; endif;
// Reset Query
wp_reset_query();
?>
</div>
<div class="clear"></div>
</div>
<?php
thematic_belowpost();
// calling the comments template
thematic_comments_template();
// calling the widget area 'page-bottom'
get_sidebar('page-bottom');
?>
</div><!-- #content -->
<?php thematic_belowcontent(); ?>
</div><!-- #container -->
<?php
// action hook for placing content below #container
thematic_belowcontainer();
// calling footer.php
get_footer();
?>
I'd appreciate it if someone could show me what I am doing wronf.
Many thanks!
You have an unquoted string inside a <?php ?> block. Anything inside these blocks is interpreted as PHP syntax instead of raw HTML.
This line:
<h1>the_title();</h1>
Should probably be something like:
echo '<h1>' . the_title() . '</h1>';
Here, we concatenate <h1>, the output of the_title() and </h1> together to form a single string.
The syntax error pertains to the first < present in the <h1> tag. As it's not valid PHP syntax in the context it's in (being inside <?php ?> tags), it throws an error. As Pekka says, SO's syntax highlighting picks this up almost instantly.
You should check your code more thoroughly and find an editor with syntax highlighting or turn it on if you already have a capable one. The PHP errors usually give a line number. Find the line, and work out what the error is.
In the problem outlined in the comments (this: <strong>the_title();</strong> throwing an error), PHP is again encountering unquoted string literals. <strong> is perfectly invalid PHP syntax, because you haven't quoted it into a string.
This:
<strong>the_title();</strong>
Needs to become this:
echo '<strong>' . the_title() . '</strong>';
Please consider reading up on basic PHP syntax such as this; it's a trivial problem and is covered by many tutorials.
As a newbie, I would like to use something like that:
<div class="news">
<div class="featTitle">
<h3>News <br />& Events</h3>
<div class="featSubTitle">What's happening in the world of Process Engineering</div>
<div class="clear"></div>
</div>
<?php
query_posts('cat=4&posts_per_page=1');
if (have_posts()) : while (have_posts()) : the_post();
?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<a href='<?php the_permalink(); ?>' rel='bookmark' title='Permanent Link to <?php the_title_attribute(); ?>' ><?php the_title(); ?></a>
<?php
endwhile; endif;
// Reset Query
wp_reset_query();
?>
</div>
Wrapping only php code inside <?php CODE ?> tag.
Someone else managed to answer my question on another forum. Kash, I hope this works for you too.
<?php query_posts('cat=5&posts_per_page=1');
if (have_posts()) : while (have_posts()) : the_post();?>
<strong><?php the_title();?></strong>
<?php the_content();
endwhile; endif;
?>
Thanks everyone else who pitched in! :)