if statement inside of wordpress post loop - php

I've tried to add an if statement inside of the wordpress loop to not show anything inside of a category, but the posts are still showing up on the front-end, I've got a feeling its something to do with the colon on the end of as if I change it to a semi colon I get an error (500) but I'm just not sure how to fix it. Does anyone have any idea how to get an if statement into the wordpress post loop?
<div class="row page-wrapper">
<?php if ( have_posts() ): the_post(); ?>
<?php if ( !in_category('65') ): ?>
<?php WpvTemplates::left_sidebar() ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(WpvTemplates::get_layout()); ?>>
<?php
global $wpv_has_header_sidebars;
if( $wpv_has_header_sidebars) {
WpvTemplates::header_sidebars();
}
?>
<div class="page-content">
<?php
rewind_posts();
get_template_part('loop', 'category');
?>
</div>
</article>
<?php WpvTemplates::right_sidebar() ?>
<?php endif ?>
<?php else: ?>
<article>
<h1 style="text-align: center;margin-top: 35px;"><?php _e('Sorry, nothing found', 'church-event') ?></h1>
</article>
<?php endif ?>
This is the code I've tried to add seen on line 3 and line 20.
I'm just looking to hide all posts from that category from showing on the search page in this case.
Any help would be greatly appreciated, if I can gain an understanding of what exactly the colon does that would be great too.
Thank you

Related

HTML only print once in Wordpress Loop

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!

Continuing a WordPress PHP Loop

I'm currently trying to develop a custom Wordpress theme, and on my homepage I need to add a second content block. I am using a plugin to do this, which simply requires me to add the following where I want the content block to be.
<?php the_block('Latest Products')?>
However when I add this it seems to have no effect which I believe is due to the formatting of my php. I'm fairly new to php, so any help is greatly appreciated.
My code is as follows - I've cut out the best part of the HTML. I think it's something to do with that 'endforeach' tag?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php the_block('Latest Products')?>
<?php endwhile; endif; ?>
<?php get_footer(); ?>
EDIT
Okay, so apparently it needs to be put outside the loop, however it still won't work. Any ideas?
<?php get_header(); ?>
<?php if(have_posts()) :?>
<?php while (have_posts()) : the_post(); ?>
<?php the_content(); ?>
<?php
global $post;
$myposts = get_posts('numberposts=4&category=1');
foreach($myposts as $post) :
?>
<div class="blogsnippet">
<div class="postdate">
<span class="top"><?php the_time ('j')?></span><br/><span class="bottom"><?php the_time('M');?></span>
</div>
<div class="postexcerpt">
<h3><?php the_title(); ?></h3>
<p><?php echo(get_the_excerpt());?></p>
</div>
</div>
<?php endforeach;?>
<?php endwhile; endif; ?>
<?php the_block('Latest Products')?>
<?php get_footer(); ?>
This mostly depends on what the plugin is actually doing because your code syntax is correct.
If you are using the Multiple Content Blocks plugin and are using the latest Wordpress version 3.5.1 then I believe the plugin may not be compatible. I'd check the version compatibility of the plugin to your Wordpress install as this could be your issue.
EDIT:
The plugin works by applying a filter to the function the_content() so that is why it only works by declaring the_block() before the_content() function is called.
A solution could be to capture the output the_block() and use print it out later, as an example:
<?php
ob_start();
the_block('Latest Products');
$latest_products_contents = ob_get_contents();
ob_end_clean();
?>
<!-- Further down.. -->
<?php echo $latest_products_contents; ?>

Display wordpress search results

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.

WordPress: Parse error: syntax error, unexpected '<' in C:\Inetpub\wwwroot\

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! :)

putting php function in template works but putting concatenated function in functions.php doesn't?

Here are two functions I'm working with (line 1 and line 33): http://pastebin.com/GWCJGS1i
If you notice around line 123, I have included the concatenated function . fb_comment_count() .. For some reason, this produces an incorrect comment count. It just shows zero no matter how many comments there are.
However, if I insert <?php echo fb_comment_count(); ?> into a page template, it works fine. Why does this happen? How can I get the correct comment count to show up with the concatenated function?
Here is the page template:
<?php get_header();?>
<section id="content">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<article class="post" id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?></h2>
<section class="entry">
<?php echo fb_comment_count(); ?>
<p class="attachment"><?php echo wp_get_attachment_image( $post->ID, 'medium' ); ?></p>
<div class="caption">
<?php if ( !empty($post->post_excerpt) ) the_excerpt(); // this is the "caption" ?>
</div>
</section>
</article>
<?php comments_template(); ?>
<?php endwhile; else: ?>
<p>Sorry, no attachments matched your criteria.</p>
<?php endif; ?>
</section>
<?php get_sidebar(); ?>
<?php get_footer(); ?>
Two possible causes:
The global $post variable is not set when you call your function from functions.php but it is when inside the template.
The call to get_posts() is trashing the $post global.

Categories