Continuing a WordPress PHP Loop - php

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; ?>

Related

Wordpress Custom Theme: Plugins Won't Load or Display Only Code

This is my first attempt at creating a theme purely from scratch. Before this I just used underscores_me and made most of my changes to style.css and left the majority of PHP alone, because I'm a novice with it.
My issue is that plugins are not working. None of the ones I have installed work. At this point I have been trying plugins that create an event calendar, but I'm assuming that any and all plugins will have issues. In the areas that are meant to display the calendar, there are two things I'm seeing. Plugin-generated pages show nothing at all (aside from the theme visuals) and plugins that are inserted into an admin-created page display plugin-generated code.
I am using WampServer. I have wp_footer(); and wp_head(); in the correct places. My functions.php file was created from the example found at https://scanwp.net/blog/create-a-wordpress-starter-theme-from-scratch/ and the only adjustment I have made to it so far is to remove the fontawesome line of code.
My index.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
if ( have_posts() ) :
while ( have_posts() ) : the_post();
// Display post content
endwhile;
endif;
?>
<?php get_footer(); ?>
And my page.php file looks like this:
<?php get_header(); ?>
<h1><?php echo "&#9755 "; ?><?php the_title(); ?></h1>
<?= get_post_field('post_content', $post->ID) ?>
<?php get_footer(); ?>
First of all, you have to understand that in your displayed files, there are no functions that recall objects that will later show the page content.
Then, in your index.php file there is an error, besides what was said above, because you're calling the_title () (function) that extrapolates the title of a post or page via the post object, which in this case should be extracted within the while loop contained within the if condition.
Then try editing the files as follows.
index.php
<?php
get_header(); ?>
<?php if( have_posts() ) : ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php if( is_singular() ) : ?>
<?php the_content(); ?>
<?php else : ?>
<?php the_excerpt(); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php else: ?>
<h1>No posts to display</h1>
<?php endif; ?>
<?php get_footer(); ?>
and page.php
<?php
get_header(); ?>
<?php while( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; ?>
<?php get_footer(); ?>
However, within the wordpress codex you will find all the guides well written on any type of function, look no further.
source: https://codex.wordpress.org/

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!

get_template_part not working on wordpress

I have been working on this for hours and don't know what I'm overlooking or not understanding. I'm trying to use get_template_part to call another php document. When I run the code below there is nothing in the div.content. I don't understand why my partial-banner.php is being called.
front-page.php
<?php get_header(); ?>
<div class="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/partial', 'banner' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php get_footer(); ?>
partial-banner.php
<section class="banner">
<h1>Does this work</h1>
</section>
and here's an image of my file structure.
You could just use a simple PHP include, instead of using WordPress logic
<?php include('template-parts/partial-banner.php'); ?>

Wordpress: Infinite Re-direct Loop Error on Custom Theme

I have a set-up a custom theme on my wordpress install. I have been developing with it locally and everything was working fine.
I uploaded to my server tonight and was setting up the fresh wordpress install. I turned on permalinks and all of a sudden my custom category pages are causing infinite re-direct loops.
My .htaccess is writable so I don't think its that problem (I have seen this mentioned a lot online).
The code from one of my custom pages is below - it pulls from a specific category - does anyone know how to fix this issue?
<?php get_header(); ?>
<?php
/*
Template Name: Podcasts
*/
?>
<ul class="mcol">
<?php
query_posts("cat=1");
while(have_posts()) : the_post(); ?>
<li class="article" id="post-<?php the_ID(); ?>">
<?php
if ( has_post_thumbnail() ) { ?>
<?php
$imgsrcparam = array(
'alt' => trim(strip_tags( $post- >post_excerpt )),
'title' => trim(strip_tags( $post->post_title )),
);
$thumbID = get_the_post_thumbnail( $post->ID, array(200,200), $imgsrcparam ); ?>
<div class="preview"><?php echo "$thumbID"; ?><div class="front-titles"><?php the_title(); ?></div></div>
<?php } else {?>
<div class="preview"><img src="<?php bloginfo('template_url'); ?>/images/default-thumbnail.jpg" alt="<?php the_title(); ?>" /></div>
<?php } ?>
<div class="article-over">
</div>
</li> <?php ?>
<?php endwhile;
//Reset Query
wp_reset_query();
?>
</ul>
<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>
<?php endwhile; ?>
<?php else : ?>
<h1 id="error"><?php _e("Sorry, but you are looking for something that isn’t here."); ?></h1>
<?php endif; ?>
<div class="pagination"><?php previous_posts_link('<< Newer Entries', 0) ?> <?php next_posts_link('Older Entries >>', 0); ?> </div>
<?php get_footer(); ?>
Anyone any ideas? I'm quite new to this so am unsure as to what the problem might be...
What your doing is weird.
query_posts is meant to alter the main loop.
You have 2 loops with different syntax on the same page.
The second loop is not doing anything, so remove it, and use get_posts or WP Query for your custom query.

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

Categories