Reduce number of posts depending on post content - wordpress

In a custom post archive; Is it possible to reduce the the number of posts shown, depending on the contents of the showing posts?
I have a custom post type for products. In the product archive there are 20 products showing on each page, 4 rows, 5 cols.
In some cases where the product image is super wide I'd like that product to be shown with double width.
When showing a super wide product, I'd like to reduce the number of posts to 19 (18 if there are two super wide products). Keeping the bottom line straight.
This is the loop for my custom post archive.
I got your point with numcols. And as you say, I should probably modify the query, making sure that page 2 and 3 (and on) shows the right posts.
<?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink(); ?>" class="produkt_item normal various" data-fancybox-type="iframe">
<?php if ( has_post_thumbnail() ) { ?>
<div class="utvaldbild">
<?php the_post_thumbnail('produktbild'); ?>
</div>
<?php } ?>
<h2><?php the_title();?></h2>
<p><?php the_field('produktnotis'); ?></p>
<?php if (get_field('nedladdning')) { ?><div class="nedladdning"></div><?php } ?>
</a>
<?php endwhile; ?>

you could add a custom field to your post custom posttype called numcols or something like that. normal products would be 1 and super images 2 etc.
then just keep a count of them in your php code and cut off when appropriate.
you could probably modify the query in the lookup page to do this but I'd have to see some code to be of any help. ideally your custom product archive page and the definition of your custom post type.

Related

How to repeat ACF Flexible Content on single pages?

Disclaimer: If you have never used ACF Flexible Content to build a site, please reserve the hate and all that - looking just for help.
I'm creating a website that's 100% modular, done with Flexible Content.
I have Pages such as:
Home, About, Services, Blog, Portfolio, Contact
All of those pages styled using Flexible Content, these are PAGES.
I have created a custom field for Services and Portfolio, and I used WP Query to display each individal item. These are CUSTOM fields. Show this field group if post type to Services. Not a page.
So when you click it, what happens you go to single-[custom-post-type].php, however I just have single.php to keep it dynamic, which works.
The problem lays that when I have showed the flexible content page templates in the Services or Portfolio, and I add few items, I need go to every single item and set the layout.
How can I do so the layout stays all the same in Service or Portfolio custom field?
Firstly create your ACF Master block, for example, a testimonial section, this would have the repeater fields inside the block etc. In this example, we will call it Testimonial Master.
Then Create another Flexible Content Field Called Testimonial Block with a checkbox that you check to display or hide.
You will now populate Testimonial Master on your selected Parent Page or on an Options page. You then should add the Testimonial Block to any page you are looking to have that block appear on.
You will then went to check that the Testimonial Block is on the page, if it is on the page then you will check if Testimonial Master has a value if it has a value then you can pull in the values from Testimonial Master.
<?php
if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) :
the_row();
if( get_row_layout() == 'testimonial_block' ):
if( get_sub_field('testimonial_block_toggle') == 'show' ):?>
<div class="testimonials">
<?php the_sub_field('testimonial_master_title', 1); ?>
</div>
<?php endif;
endif;
endwhile;
endif;
?>
The "1" is the page ID, use this if you have populated the field on a master page. Or if you are using the ACF Options Page plugin change the "1" to "'options'".
Here is some further documentation on getting values from other posts including that of repeater fields.
https://www.advancedcustomfields.com/resources/how-to-get-values-from-another-post/
Edit: Custom Header as per your example
Make a custom header file and add your code for that header block.
Now make an advanced custom field for a text area. For example header_text_block, set it to a WYSIWYG editor.
Now in your code just use:
<?php the_field('header_text_block');?>
This will then use the exact same header on every page you pull it into, however it will let you edit the text individually for each page.
Edit: Example of what this code looks like and does
Also I think you are very confused, there will only be 1 php/html file? you could use page.php.
Here's an example of what I am saying, 1 php file:
<?php if( have_rows('page_structure') ):
while ( have_rows('page_structure') ) : the_row();
if( get_row_layout() == 'page_featured' ):
$image = get_sub_field('page_featured_image');
if( !empty($image) ):?>
<img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>">
<?php endif;
endif;
if( get_row_layout() == 'page_breadcrumbs' ):
if( !in_array( 'hide', get_sub_field('page_breadcrumbs_hide') ) ):?>
<?php if ( function_exists('yoast_breadcrumb') ) {yoast_breadcrumb('<p id="breadcrumbs">','</p>');}?>
<?php else :
endif;
endif;
endwhile;
endif;
?>
This then looks like this on the page editor:
You can then edit each section as you please and also drag and drop sections where you want them which enables easy split testing.
All the styling will be the same, just you can change the content and if you wanted you could add the ability to alter stylings pretty easily. And it only uses 1 PHP file for the template.

Categories with post from child categories on front page

I am developing my first WordPress site and I am using the Customizr theme. I need to build a front page, that shows the latest posts from 2 categories - News and Events and all their child categories. So my category tree looks like this:
Articles
-News
--First news child category
--Second news child category
-Events
--First events child category
--Second events child category
and I want the front page to show the category News and the category Events with all the posts in their child categories packed in 2 containers with the headlines News and Events respectively. I am a total newbie in WordPress and I've searched for days for a solution, but I think I'm searching with the wrong keywords, because I didn't found what I need. I've created a child theme, and I'm currently trying to make an index.php with The Loop to achieve that. I've also tried making a static front page which uses a different template.
So which is the right way to do that? Can you please at least give me some pointers? Thank you!
If i understood corectly you can achieve expected result with Category Parameters for WP Query. Then you can loop over posts like this:
<?php
$the_query = new WP_Query( "category_name=news,events"); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Also, to customize your front page better make a new template (home.php), do your job there and then asssign it to an empty page and make that page your front page from Settings->Reading

WORDPRESS: How to remove certain content on single post page of post_format gallery?

First off I created my own theme from scratch. I've been trying to figure this out all day yesterday. Ended up using a couple of twentyelevens and twentyten's files just to accomplish this. Then deleted them cause there was no success. All I want to do is remove certain text such as "Filed Under" and "Posted By" that appears at the bottom of my single posts page. I want the Standard Single Posts Page to have the Meta Data, while the Gallery Single Posts Page doesn't have Meta Data.
I tried to use the loop.php, loop-single.php, loop-gallery.php, content.php method but nothing was working for me. Where can I start to get these two different post formats to display differently on their single pages?
Is there anything I need to add to my functions.php file just to make this work?
Do I need to recreate the loop files?
Please help...
If 'gallery' is a category, you could edit your single.php template and use is_category():
<?php if ( in_category('gallery') ) : ?>
<!-- Single post style for gallery posts -->
<?php else: ?>
<!-- Normal single post style -->
<?php endif; ?>
If it's a custom post type, you could use get_post_type() in single.php and use its result in a condition, e.g.
<?php
$post_type = get_post_type( $post->ID );
if ( $post_type == 'gallery' ): ?>
<!-- Single post style for gallery posts -->
<?php else: ?>
<!-- Normal single post style -->
<?php endif; ?>
If it's a post format, use get_post_format(), e.g.
<?php
$post_format = get_post_format( $post->ID );
if ( $post_format == 'gallery' ): ?>
<!-- Single post style for gallery posts -->
<?php else: ?>
<!-- Normal single post style -->
<?php endif; ?>

WordPress Get all the Category's Post

I have Developed WordPress Site Successfully, but i have doubt in Getting all the post by
Categories.
My Code is like this
new WP_Query("cat=54,71,72&order=ASC");
Default it is getting the first category id and the Post.
Thanks
If you want to get ALL your categories instead of a selected few, you don't need to query by category.
new WP_Query("order=ASC");
If you want to query a particular category, but you're not sure what's the category ID number, query it by category slug
new WP_Query("category_name=your-category-slug&order=ASC");
You can also use this code as well, just need to change the number of post, like as of now, below listed code is use for display 5 post only, so you have to change this limit.
<?php query_posts('showposts=5'); ?>
<ul>
<?php while (have_posts()) : the_post(); ?>
<li><?php the_title(); ?></li>
<?php the_content();?>
<?php endwhile;?>
</ul>

WordPress Rewind Posts

I have the following code: http://pastebin.com/ng99augD
It basically spits out five posts for the five authors I have specified in the query posts in each chunk of code and then rewinds the posts prior to the next query. This is far from ideal and would like to make the code more efficient without having to repeat all the template code for the posts over and over. I also want them to be in a random order instead of coming out in the order they have been written as I don't want any particular author to be at the top. Thanks.
I'm not sure why the use of rewind_posts() is necessary. You are creating a new query using by your use of query_posts() each time. rewind_posts() rewinds the query for re-use.
If your performance isn't suffering, it may be okay to run 5 queries to get your five authors. To avoid rewriting the same template code each time you have at least two options. Create a php file with the template code in it, and include it each time you need it:
<?php include('author_posts.php');?>
Or, better yet, create a function in your functions.php or in a plugin file that accepts your query results and outputs the template.
<?php $posts = query_posts('posts_per_page=1&author=author1');
if(function_exists('my_authors')){my_authors($posts);}
?>
Then in your functions.php or plugin:
function my_authors($posts){
//template code here, using $posts->ID, $posts->post_content, etc
}
The third option, which cleans up your code and the number of queries would be to use a category to display posts assigned to it here. You are making 5 queries to display five posts. To use one query to display five posts and sort randomly like you want to, edit each of the five posts and assign them to a new category, lets say:'author sampler'. Then in your template file:
<?php $posts = get_posts('category=author sampler'&order_by=rand&order=asc');
foreach($posts as $post):?>
<?php setup_postdata($post);
//your posted template code follows:
?>
<article id="post-<?php the_ID(); ?>">
<h2><?php the_title(); ?><?php edit_post_link('Edit', ' (', ')'); ?></h2>
<p class="p-cat">In: <?php the_category('|') ?></p>
<p class="p-author">
<span class="name">
<?php the_author_posts_link(); ?></span> <span class="avatar"><?php echo get_avatar( $email, $size = '32' ); ?>
</span>
</p>
<?php //etc ?>
<?php endforeach;?>

Resources