how could i get for every post on a page a post_class "first"?
with this code a managed a class "first_post" for the really first post of the loop, but i need it also on the second, third and so on page of the loop.
function.php
function firstpost_class($class) {
global $post, $posts;
if ( is_home() && !is_paged() && ($post == $posts[0]) ) $class[] = 'firstpost';
return $class;
}
add_filter('post_class', 'firstpost_class');
Thanks for your help. Google couldn't help yet.
I've seen this kind of thing done on the loop itself -- comment to clarify if you want to do this site-wide, or particularly want to hook into post_class (then again, you can edit loop.php or a content-LOOPTYPE.php and reuse it over and over). Here's an example loop:
<?php
$counter = 0;
if ( have_posts() ) : while ( have_posts() ) : the_post();
$counter++;
?>
<div class="post <?php echo 'item-' . $counter; ?>">
<?php the_title(); ?>
<?php the_content(); ?>
</div>
<?php endwhile; endif; ?>
This would give you item-1, item-2, etc. etc.
Related
I am customizing an old theme called AmazeTheme which was not specifically made with WooCommerce support. And an woocommerce.php was at theme's root. However with help of plugins like Simply Show Hooks and Show current template I am able to see where to put which info.
But I am having an weird problem. The ACF fields I have added are not visible at all. I even tried to assign the field group inside product loop section.
I also tried the following method.
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
if (function_exists('the_field')){
the_field('shop_support');
}
}
And inside the loop of single-product.php
<?php while ( have_posts() ) : the_post(); ?>
<?php wc_get_template_part( 'content', 'single-product' );
$dump = get_fields();
?>
<?php endwhile; // end of the loop. ?>
And then did var_dump($dump) at desired place of the file.
This site's php version is 5.6.40 and WooCommerce version is 3.4.8
WP version is 4.9.18
I have looked up many solutions. Also tried the WooCommerce Hooks but still no clue why ACF not showing.
Can you try this code:
add_action( 'woocommerce_after_single_product_summary', 'view_acf_field_for_single_product', 10 );
function view_acf_field_for_single_product(){
global $post;
if (function_exists('the_field')){
the_field('shop_support', $post->ID);
}
}
Not tested.
For some weird reasons, this approach worked for me (path/to/theme/woocommerce/single-product.php):
<?php if (get_field_objects()):
foreach ( get_field_objects() as $field_id => $field ) :
$value = trim($field['value']);
if (!empty($value)) :
?>
<div class="product_field" id="field-<?php echo $field_id; ?>">
<?php the_field($field_id); ?>
</div>
<?php endif; ?>
<?php endforeach; // end of the loop. ?>
<?php endif; ?>
Also the following one for Category (path/to/theme/woocommerce/archive-product.php) pages:
<?php
$extra_info = get_field_object( 'category_details', get_queried_object() );
if ( !empty($extra_info) ) :
?>
<div class="category_field" id="field-<?php echo $extra_info['key']; ?>">
<?php echo $extra_info['value']; ?>
</div>
<?php endif; ?>
Even at these locations, get_fields() did not work.
I have a number of flexible content fields set up.
One of these is called 'product-picker' and another is called 'product-information'.
The product picker uses the post object functionality to pull in selected products.
I can pull in the default wordpress parts such as:
<?php the_post_thumbnail(); ?>
But I can't pull in the values within 'product-information'.
Is this actually possible? Extracts of code below:
<?php if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
if( get_row_layout() == 'product_information' ):
include("inc/product-information.php");
elseif( get_row_layout() == 'products_picker' ):
include("inc/products-picker.php");
endif;
endwhile;
endif; ?>
Then within 'products-picker' I am using the following code:
<?php foreach( $post_objects as $post): ?>
<?php setup_postdata($post); ?>
<div class="product">
<?php the_post_thumbnail(); ?>
<div class="description">
/* Echoing post ID returns correct value */
<h1><?php echo $post->ID; ?></h1>
/* Does not work */
<h1><?php the_sub_field('heading', $post->ID); ?></h1>
/* Does not work */
<?php the_sub_field('description', $post->ID); ?>
</div> <!-- .description -->
</div> <!-- .product-->
<?php endforeach; ?>
<?php wp_reset_postdata(); ?>
I might have the structure of my content wrong.
Maybe you can't call flexible content within flexible content?
But I thought it should be possible to do this in light of the fact that the default fields such as:
the_post_thumbnail();
and
the_title();
work.
All your time and help is greatly appreciated.
You have to set up the flexible rows part for each of your post objects. So after you setup your post data, replicate your
if( have_rows('content_blocks') ):
while ( have_rows('content_blocks') ) : the_row();
with the product information fields for each post object.
Check out https://www.advancedcustomfields.com/resources/flexible-content/ and the nested loop example. I think that should get you a little closer.
i use that code to post an ad in my homepage every 3 posts but instead of that the ad appears behind the 1st post and it seems like it doesnt recognise the loop at all
<div id="mason-layout" class="transitions-enabled fluid">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'blog' ); ?>
<?php if ($i == 3) { ?> ADHERE <?php } ?>
<?php $i++; ?>
<?php endwhile; ?>
any ideas?
thanks
You're missing part of your loop.
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
Edit: If you want to break the if statement onto multiple lines you'll probably want to use a different syntax. Eg:
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
//
// Post Content here
//
} // end while
} // end if
Recommended reading: http://codex.wordpress.org/The_Loop
I'm working on my own custom WordPress theme, using a blank, default template to start. I haven't edited the search.php file.
Many of my WordPress posts are Pages. Unfortunately, the site search only retrieves posts, not pages.
Any idea how to get the theme to search both posts and pages?
Here is the majority of search.php:
<?php if (have_posts()) : ?>
<h3>Search Results</h3>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php while (have_posts()) : the_post(); ?>
<div <?php post_class() ?> id="post-<?php the_ID(); ?>">
<h4><?php the_title(); ?></h4>
<?php include (TEMPLATEPATH . '/inc/meta.php' ); ?>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</div>
<?php endwhile; ?>
<?php include (TEMPLATEPATH . '/inc/nav.php' ); ?>
<?php else : ?>
<h3>No posts found.</h3>
<?php endif; ?>
Add this code to your functions.php file.
function wpshock_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array('post','page') );
}
return $query;
}
add_filter('pre_get_posts','wpshock_search_filter');
http://wpth.net/limit-wordpress-search-results-to-specific-post-types
WP Search http://wpsear.ch/ has that ability.You can adjust what post types you want to show in results page.
In your search.php, find The Loop and insert this code just after it. You can recognize the Loop because it usually starts with:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
Code to be inserted:
if (is_search() && ($post->post_type=='page')) continue;
So, your code should be like this:
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();?>
<?php if (is_search() && ($post->post_type=='page')) continue; ?>
Let me know if it worked.
Lack of page search and search results ranked by post date instead of relevance is a typical WP problem. Try http://wordpress.org/extend/plugins/relevanssi/
I am using the following code to try and display posts from only a certain category horizontally in three rows. I have the horizontal display issue figured out (using css) but with the following code it displays all posts and not posts from specific category.
<?php query_posts('showposts=5'); ?>
<?php query_posts('cat=7'); ?>
<?php $posts = get_posts('numberposts=5&offset=0'); foreach ($posts as $post) : start_wp(); ?>
<?php static $count1 = 0; if ($count1 == "5") { break; } else { ?>
<?php the_title(); ?>
<?php the_content(); ?>
<?php $count1++; } ?>
<?php endforeach; ?>
Any help would be greatly appreciated.
You're misunderstanding some concepts in query_posts and get_posts.
query_posts is to be used inside the loop. get_pages isn't. If you want to use query_posts, you don't need to create the get_pages call. Use query_posts or get_pages to accomplish what you're trying to do.
You need to combine your category parameters in query_posts.
<?php
query_posts('showposts=5&cat=7');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
..
endwhile; else:
..
endif;
//Reset Query
wp_reset_query();
?>
If you want to do the same logic but without The Loop, just call
$posts = get_posts('numberposts=5&offset=0&category=7').
Read the links I provided. They have all information you need to understand how to do what you need.