Wrong post title displaying from wordpress loop - wordpress-theming

I'm building my own theme. I have a page set for my blog (with a template that I made) which I'd like to just display a few of my posts. It uses the following loop:
<?php
query_posts('post_type=post');
if (have_posts()) {
while (have_posts()) {
?>
<div class="blog_post">
<h2><?php the_title(); ?></h2>
<div class="entry_date"><?php the_time('F jS, Y') ?></div>
<?php
the_post();
the_content();
?>
</div>
<?php
}
}
?>
The titles of my posts are "First Post, Second Post, Third Post, and Fourth Post" respectively. When the posts are displayed on the blog page, they are displayed in the correct order, but the titles of the posts are incorrect. The first post's title reads: "Second Post". The second post's title is: "Third Post", and so on until the last (most recent) post which has the title: "Blog" (the page title). What happened to the titles that they got so screwed up?
What I've Tried:
I've researched this a lot before I came here. I tried using get_the_title() instead but that lead to no titles being displayed. I've also tried using the_title_attribute() to no avail. I also understand that I shouldn't be using query_posts for this loop but I'm unsure which is the correct method to use for getting the posts in this particular case. Most of the info I've read was unclear though and didn't seem to fix the issue.
Any help is greatly appreciated.

Try something like this: untested
<?php
global $post;
$args = array();
$myposts = get_posts( $args );
foreach( $myposts as $post ) :
setup_postdata($post); ?>
YOUR HTML HERE
<?php endforeach;
wp_reset_postdata(); ?>

Okay Nevermind! I just found out what the problem was. I moved, "the_post()" to just after the while loop so now it reads like this:
<?php
query_posts('post_type=post');
if (have_posts()) {
while (have_posts()) {
the_post();
?>
<div class="blog_post">
<h2><?php the_title(); ?></h2>
<div class="entry_date"><?php the_time('F jS, Y') ?></div>
<?php
the_content();
?>
</div>
<?php
}
}
?>
I found this solution while I was reading about the_post() in the wordpress codex. Turns out this function is setting up information for the next post in the line, so it shouldn't be mixed in with the html output for the current post.
In regards to whether or not I should be using "query_posts()" is still something I'm unsure of and willing to take any advice. But the loop in it's current form is working.

Related

Wordpress post grid last posts

how can i create a grid like image? for show last posts
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php $latest_post = get_posts( 'numberposts=4' ); ?>
<?php foreach( $latest_post as $post ) : setup_postdata( $post ); ?>
<?php the_title(); ?><br />
<?php the_content(); ?>
<?php endforeach; ?>
<?php wp_reset_query(); ?>
One option is to use PHP to swap around the position of the image based on whether or not the increment is odd or even, for instance:
$i = 0;
while ( have_posts() ) : the_post();
if ($i % 2 == 0 ) {
// Display two columns with image on left
}
else {
// Display two columns with image on right
}
$i++;
endwhile;
If you're building your theme from scratch I'd suggest using a grid framework to handle your columns, otherwise see if the theme you're using has a grid framework already.
Edit:
There are also ways of doing this without actually having to change the markup of the page. For instance:
Swapping columns (left / right) on alternate rows
In this case you'd be able to generate your post markup without the if statement and just use CSS to swap the position of the image/video.
You can use the masonry technology or you can try this plugin
In your code above you fired two WordPress content loops. I am not sure why you had to fire two loops, although they both will work. First will print the recent posts depending on the number of Posts Per Page you selected via Settings->Reading tab from your WordPress Dashboard and the second one will again print the top 4 recent posts. I am using the first loop to tell you how you can create the attached image like grid.
Below is the PHP/HTML modifications that you have to make:
<?php $count = 1; if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<div class="one-half <?php if( $count++ % 2 == 0 ): echo 'last-col'; endif; ?>">
<?php
// the function below will print the featured image attached to the post
the_post_thumbnail( 'your-featured-image-sizename' ); ?>
</div>
<!-- one-half -->
<div class="one-half">
<span class="post_stamp"><?php the_time( 'j m Y' ); ?></span>
<span class="post_cat"><?php the_category(', '); // This will print News if you filled the post under News Category ?></span>
<h3><?php the_title(); ?></h3>
<?php the_excerpt(); // Insteading of printing the whole content this function will print exceprt only ?>
</div>
<!-- one-half -->
<div class="clearfix"><!-- --></div>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
You would have to put the below given links to your style file:
<style>
.one-half{width: 50%; float: left;}
.last-col{float: right;}
.clearfix{clear: both; overflow: hidden;}
</style>
After making the above changes your posts will be displayed as the attached image. Good Luck (y)

Wordpress: HTML sidebar at bottom, instead of right aligned with body

I added HTML to my functions.php. After the
<?PHP get_sidebar(); ?>
and before
<?php get_footer(); ?>
while the CSS is added in my style.css Child theme. The result is my box of HTML under the main content on my home page instead of being on the right side of the main content. I am hoping someone may be able to help me?
I tried to add my HTML and CSS but it wouldn't let me post it.
I've spent about 3 hours on this by myself so far. First I tried reformatting a feature provided by the Minamaze Wordpress Theme, but I figured I may as well just right my own code as it may be easier to implement.
I fixed it! Here's how: The real problem was the fact that my front page articles were individually grouped instead of wrapped in a class. I found the index.php file (the copy from my child theme) and surrounded the homepage content in a named div class. Below is code from my index file. Put a div wrapper class between get_header and the php line below it. Close the div before the get_sidebar.
get_header(); ?>
<?php while( have_posts() ): the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class('blog-article'); ?>>
<?php if ( has_post_thumbnail() ) {
$column1 = ' two_fifth';
$column2 = ' three_fifth last';
} else {
$column1 = NULL;
$column2 = NULL;
} ?>
<header class="entry-header<?php echo $column1; ?>">
<?php thinkup_input_blogimage(); ?>
</header>
<div class="entry-content<?php echo $column2; ?>">
<?php think_input_blogtitle(); ?>
<?php thinkup_input_blogmeta(); ?>
<?php thinkup_input_blogtext(); ?>
</div>
<div class="clearboth"></div>
</article><!-- #post-<?php get_the_ID(); ?> -->
<?php endwhile; ?>
<?php thinkup_input_pagination(); ?>
<?php else: ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
?php get_sidebar(); ?>
Under get_sidebar, you can put in your HTML code for YOUR sidebar that you wish to add.
Once I wrapped them into the php into it's own class and wrapped my sidebar boxes into their own class, I used float: left and right respectively in my CSS child theme, successfully creating a sidebar. I also made their widths percentages to increase usability. You can use this same method in the content-page.php, content-single.php, if you wish to add a side bar to other pages besides just the home screen.

If statement not working when it really should

Okay, this is by far one of the strangest things I've come across in some time.. I have the following piece of code in one of my woocommerce files:
<?php $titlevar = wp_title('', 0); ?>
<?php echo $titlevar; ?>
<?php if($titlevar == "Men") { ?>
<div id="sidemenu" style="float:left;width: 180px;float:left;height:100px">
<?php wp_nav_menu( array('menu' => 'sidemenumen' )); ?>
</div>
<?php } ?>
As you can see, if my $titlevar variable equals "Men", it should display a div with a menu inside. I've echo'ed the variable at the top and it actually outputs 'Men' (without quotes of course), how is it possible that it's not showing my div??

Wordpress - How to get the most viewed posts?

How can I display my most viewed posts on a single page ?
For this moment, i've create a new template page (content-most-viewed.php) wich is called by get_template_part();
On Internet, i've read that query_posts could solve my problem, but it seems to fail.
This is my template :
<?php query_posts('meta_key=post_views_count&orderby=meta_value_num&order=DESC');
//query_posts('post_type=post&posts_per_page=10&orderby=comment_count&order=DESC');?>
<div class="post">
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="heading">
<h2><?php the_title();?></h2>
</div>
<?php the_content();?>
<div class="link-holder">Retour</div>
<?php endwhile; ?>
<?php else : ?>
<div class="heading">
<h2>Not Found</h2>
</div>
<p>Sorry, but you are looking for something that isn't here.</p>
<?php endif; ?>
</div>
But, I can only get recent posts, or "Not Found" (any resutls)
In advance, thanks !
WordPress by itself does not keep track of the number a post is viewed there are pluging that do this: WPP, plugin repos
These plugins will have build in functions to do this.
side note
don't use query_posts its bad for preformance. use WP_query in stead. reasons why it is bad: https://wordpress.stackexchange.com/a/50762/10911, https://wordpress.stackexchange.com/a/50762/10911

Wordpress Attachment URL function not working

I've been working on a redesign for a website on a local development setup and have decided to give a shot at WordPress for the first time. On my front page, I have an image slider that slides through the three most recently posted items that I want on the front page. On all of the posts, I have attached an image that I want to display on the front page. Here's my code on the homepage:
<?php query_posts('category_name="main page"&showposts=3');
while ( have_posts() ) : the_post(); ?>
<div class="panelContent">
<img class="panelPhoto" src="<?php echo wp_get_attachment_url(get_the_ID()); ?> "/>
<div class="panelCaption">
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
The code returns an underscore in the src part of the img tag. Not exactly sure why.
Also, here's an image of my media panel showing that the images are indeed attached:
wp_get_attachment_url() takes an Attachment ID, not a Post ID.
Use get_children() to get the attachments for a specific post.
<?php $images = get_children(array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1,
'post_mime_type' => 'image',)); ?>
<img class="panelPhoto" src="<?php echo wp_get_attachment_url($images[0]->ID); ?> "/>