in_caterogy won't show after changing publishing date/time - php

Wordpress is behaving really strange here, beyond my understanding!
So having this 4 posts:
And this is how I show them on my site:
As you can see, it only shows 3 posts from the 'Events' category. This is right! See code:
if (have_posts())
{
while (have_posts()) : the_post();
if (in_category( 'Events' )) {
$haveEvents = true;
**if ($eventCount < 3) {**
$eventCount++;
?>
<div class="event-tile">
<div class="event-tile-content">
<h3><?php the_title(); ?></h3>
<h4><?php the_time('F jS, Y'); ?></h4>
Lees meer
</div>
</div>
<?php
}
}
endwhile;
}
I also have some other categories en thereby I use multiple have_posts loops for showing the post from each category.
Now after I added some new posts for each category, the 'Event' posts where gone. I tested like an hour or so, and I found the problem, for some reason the loop have troubles to show 'older' posts (belonging to the 'events' category).
Here Ill demonstrate what I mean:
Changed the 'published' time, so the posts was the 'oldest':
After committing this change, the post was gone..
I don't understand this strange behaviour from Wordpress.
As the code shows;
loop trow posts
If post in category 'Event' go on ->
if 'eventCount' smaller than 3 -> show
'eventCount'++
Did I made a mistake? Is wordpress buggy here?

Related

Wordpress show different sidebar when one of two conditions is met (is_category('25')) II (in_category('25'))

I want to display different sidebars in wordpress for different categories and for posts belonging to these categories. I want to make this happen within one expression.
<?php if (is_category('25')) : ?>
<p>Sidebar for Category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
This works but when I try to display the custom sidebar for category page and single posts from cat25 at the same time with:
<?php if (is_category('25')) || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
nothing happens.
I have a learning deficit and a hard time with logic. But still I try and I keep improving.
Please consider this when giving me an answer.
I made a simple mistake. I closed to early with the ) and used the ) one more time at the end.
At least this might help someone coming from a search engine.
This is the working code:
<?php if (is_category('25') || (in_category('25')) : ?>
<p>Sidebar for category 25 archive and posts within category 25</p>
<?php elseif (is_category('26')) : ?>
<p>Sidebar for Category 26</p>
<?php else : ?>
<p>No custom sidebar for this post/category</p>
<?php endif; ?>
If someone has a simpler solution or any changes according to best practices please still feel free to give your input :-)

Show a single post on other template/page

Working on a site, and I need to have a separated template/page for showing just that one post.
On the home page (index) Ill loop through my categories and showing just a part of them - events - news - references.
Like this:
<?php
$query = new WP_Query( array( 'category_name' => 'Events' ) );
if ($query->have_posts())
{
while ($query->have_posts()) : $query->the_post();
$haveEvents = true;
if ($eventCount < 3) {
$eventCount++;
?>
<div class="event-tile">
<div class="event-tile-content">
<h3><?php the_title(); ?></h3>
<h4><?php the_time('F jS, Y'); ?></h4>
Lees meer
</div>
</div>
<?php
}
endwhile;
}
Every section has a read all and read specific item button:
I already found out how to display all posts within a category on a separated page named: page-posts.php
On page-posts.php I simply check on which page I am and depending on which page, make a query to show the posts within a specified category
$query;
if (is_page('events')) {
$query = new WP_Query( array( 'category_name' => 'Events' ) );
}
elseif (is_page('news')) {
$query = new WP_Query( array( 'category_name' => 'News' ) );
}
if ($query->have_posts())
{
etc..
Now what I cant figure out (not even after spending like hours on google), how to show a single post from the home page (index.php), on a single-post page...
On the action buttons I use:
Lees meer
Which brings me back to the homescreen. (just started on Wordpress)
In a Wordpress theme single.php is used to display a post. If it does not exist it drops down to using your index.php
Try moving your page-posts.php to single.php
Maybe its better to use archive-events.php. You can make an archive for custom posts.
With that you can do: where events is your custom post-type. Then you land on the events archive page.
As far as i can see your problem is that the post is the global post (from your home). So maybe you can setup_postdata($post), or use the home_url('url') functions.

Custom Posts not showing in archive page

I currently have a custom post type of Episode with a taxonomy of podcast. When i run the following loop on my archive page i am seeing that I have 149 posts
<?php
$args2 = array(
'posts_per_page' => 1000,
'post_type' => 'episode',
'podcast' => 'my-episodes',
'post_status' => 'publish'
);
$posts = get_posts($args2);
$count = count($posts);
echo $count;
?>
However, when I'm running this variation of the loop on my archive.php file (copied from the wordpress.org page), i am only getting 130 posts
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<small><?php the_time('F jS, Y'); ?> by <?php the_author_posts_link(); ?></small>
<div class="entry">
<?php the_content(); ?>
</div>
<p class="postmetadata"><?php _e( 'Posted in' ); ?> <?php the_category( ', ' ); ?></p>
<?php endwhile; else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
The loop is saying that my most recent entry was from the 5th, however i have been creating posts on a regular basis over the last few days. Are there any tests I can run to see why the most recent posts are not showing up on the archive page?
Your code appears to be correct. Double check that the posts that are missing are published and not set as a draft/saved. Also check that your custom taxonomy and post types are correct for the query you are running.
After reviewing the dashboard of the site per the recommendation of Tom here is what I was able to find
The site has a plugin called WPML activated. This allows the site to create content in multiple languages.
When the plugin is initially activated, it will ask for the site's native language and apply that to the existing posts and custom posts. This way if you were on an englsih version of the site you would see content ABC while on a spanish version you would see content XYZ.
There is a separate section inside of a custom post that allows you to make said custom post translatable. If this is not checked then your custom post will not display on any version of the site. The reason being (at least in my 5 minutes of research) is that because there is no language specified, it will not load on the language specific versions of the site
I have done two things to fix this:
I have added the ability to make the custom post translatable,
There is a setting that will allow you to display all content that is not translated on any version of the site as opposed to not displaying any content
Thank you everyone for your help.

Wordpress custom field only displaying 10 posts

I've used wordpress Advanced Custom Fields plugin to make my portfolio page. I've setup fields for the image, title, category and a yes/no for weather the project is featured.
My code for the project page is as follows:
<?php
$args = array(
'post_type' => 'project'
);
$query = new WP_Query( $args );
?>
<?php Starkers_Utilities::get_template_parts( array( 'parts/shared/html-header', 'parts/shared/header' ) ); ?>
<h1 class="lead"><?php the_title(); ?></h1>
<div id="triggers">
<strong>Filter:</strong>
<span id="filterDouble">Filter All</span>
<span id="filter1">Filter 1</span>
<span id="filter2">Filter 2</span>
</div>
<ul id="gallery" class="group">
<?php if ( have_posts() ) while ( $query->have_posts() ) : $query->the_post(); ?>
<li class="gallery_image" data-id="id-" data-type="<?php the_field('project_category')?>">
<a rel="prettyPhoto[gallery]" class="zoom" href="<?php echo the_field('image') ?>">
<img class="mag" src="<?php bloginfo('template_url'); ?>/imgs/mag.png"/><div class="thumb_bg"></div>
<?php the_post_thumbnail('portfolio'); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
Everything works fine, i can add projects. However My problem starts when i hit 10 projects. After 10, it doesn't display anymore. I am using jpages (jquery plugin) and filtrify to add filters to filer out the categories. They work fine, i can filter by category and i see the correct images. Even with the plugin scripts removed, i still only see a max of 10 posts. Adding more than 10 simply pushes the earlier images off and it displays the 10 latest.
So how can i stop it from being just 10.. My jpage script sets the pagination to 12 per page, but this doesn't even get a chance to kick in. I am thinking it's a post problem as I'm certain it's not the scripts.
I think I've traced an issue - I tested this: 'posts_per_page' => '20' - Which DOES display my missing posts, however i don't want to set a number as i might need lots. How can i define an unlimited number. Providing this is the issue..
If any other code is needed, please let me know, but i think this is the main part of what's controlling my posts appearing on the page.
You're on the right track.Set the value to -1 to get your desired results.
Link to the documentation: http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
posts_per_page (int) - number of post to show per page (available with Version 2.1, replaced showposts parameter). Use 'posts_per_page'=>-1 to show all posts. Set the 'paged' parameter if pagination is off after using this parameter.

Can some one explain why WordPress pagination doesn't like custom loops?

I have the exact same issue as This other stack post. How ever I cannot figure out what the replier means by this. I have tried their example in my custom loop which looks identical to the OP, how ever the pagination does not work. It does not show up.
I also have this issue in this loop:
$attr = array(
'align' => 'left',
'class' => 'thumbnail imageRight',
'width' => 350,
'height' => 350
);
if(have_posts()){
while(have_posts()){
the_post();
?>
<div class="post">
<?php the_post_thumbnail('medium', $attr); ?>
<h1><?php the_title(); ?></h1>
<p><?php the_excerpt(); ?></p>
</div>
<?php
}
next_posts_link('« Older Entries');
previous_posts_link('Newer Entries »');
}
Where the pagination will not show up at all, If I echo something above and below I see the echoed text, obv, but I do not see the pagination links....
I have set WP to display 3 posts per page, and I have over 45 posts in the database.
I had this issue over and over - i dont know if you are using a custom page template, but in the end i just used an archive page instead of querying a category or post type - for example archive-portfolio.php and the pagination just worked on that. - some of these problems cause hours of irritation, but this seemed to fix it.

Categories