I have 1 single post page template which will display 2 different taxonomy terms, and I like to call 2 different sidebar for these 2 different terms. This post template is "single-recipe.php", in which I call "content-single_recipe.php". In "content-single_recipe.php", I call for 2 different sidebars base on the different terms with a condition statement:
SINGLE-RECIPE.PHP
while ( have_posts() ) : the_post();
get_template_part( 'content', 'single_recipe' );
endwhile; // end of the loop.
CONTENT-SINGLE_RECIPE.PHP
php the_content();
// Here are code for sidebars:
$term = get_the_term_list($post->ID, 'recipe_type');
if ( $term = 'salad' ){
dynamic_sidebar('sidebar-salad');
}elseif($term = 'sandwich'){
dynamic_sidebar('sidebar-sandwich' );
}
However, no matter what the $term is, it always call the "sidebar-salad".
get_the_term_list gives you HTML list of terms. Use has_term instead. And comparing is done with ==, not =. Since you use =, which is assigning value to variable, you first if will always be true.
if( has_term( 'salad', 'recipe_type', $post->ID ) ){
dynamic_sidebar('sidebar-salad');
}
elseif( has_term( 'sandwich', 'recipe_type', $post->ID ) ){
dynamic_sidebar('sidebar-sandwich');
}
// Sidebars
if(is_front_page())
$op_sidebar_id = __('Sidebar Home','options');
// Single page and post type sidebars
elseif(is_attachment())
$op_sidebar_id = __('Sidebar Attachment','options');
elseif(is_single())
$op_sidebar_id = __('Sidebar Single','options');
elseif(is_page())
$op_sidebar_id = __('Sidebar Page','options');
else
$op_sidebar_id = __('Sidebar Home','options');
After that, I add the normal widgetized section with the variable set:
// If using the No Sidebar template
if(is_page_template('no-sidebar.php')) :
echo '';
// Else, show the sidebar
else :
echo "";
if(function_exists('dynamic_sidebar') &&
dynamic_sidebar($op_sidebar_id)) :
else :
// Default to Home page sidebar if no widgets are set
if(function_exists('dynamic_sidebar') && dynamic_sidebar(__('Sidebar
Home','options'))) :
else : _e('Add content to your sidebar through the widget control
panel.','options');
endif;
endif;
echo '</div>';
endif;
Related
I am having a hard time getting the Wordpress thumbnail to show up when I am using Advanced Custom Fields Pro Post Object.
My goal is to have the user select a single featured post to show up after the 6th post on the blog page.
This is my code (which is pretty much directly from ACF documentation):
<?php
$featured_post = get_field('featured_post', 'option');
if( $featured_post ): ?>
<?php echo esc_html( $featured_post->post_title ); ?></h3>
<?php the_post_thumbnail(); ?>
<?php endif; ?>
This returns the correct title but the wrong featured image (from the post above).
The code above is called inside the loop on index.php inside the featured template part.
$counter = 1;
/* Start the Loop */
while ( have_posts() ) :
the_post();
/*
* Include the Post-Type-specific template for the content.
* If you want to override this in a child theme, then include a file
* called content-___.php (where ___ is the Post Type name) and that will be used instead.
*/
get_template_part( 'template-parts/content', 'get_post_type()' );
//check the counter and display your content
if( $counter === 3 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'subscribe'); ?>
<?php } elseif ( $counter === 6 && !is_paged() ) { ?>
<?php get_template_part('template-parts/components/component', 'featured'); ?>
<?php }
//update the counter on every loop
$counter++;
endwhile;
I have tried the other code examples on the documentation page but those return all of my posts (again with only one thumbnail, not the correct thumbnail per post).
I am not sure where to go from here.
Anyone who can help would be greatly appreciated!
Since this is not in the loop context, the function does not find the correct post object set up to take the ID from. Use get_the_post_thumbnail instead, that takes the post id as parameter.
<?php echo get_the_post_thumbnail( $featured_post->ID ); ?>
I am using the Category and Taxonomy Meta Fields plugin to create some necessary fields in the product categories.
When trying to display this information in php, it does not appear.
$cate = get_queried_object();
$cateID = $cate->term_id;
if (function_exists('get_all_wp_terms_meta'))
{
print_r( get_all_wp_terms_meta($cateID) );
}
in page archive-product.php, return Array ( )
Use the plugin Advanced Custom Fields
In file archive-product.php use:
$cateID = get_queried_object();
$return = get_field('NameField', $cateID);
if using a flexible field, name the layout in the field settings and use
if ( have_rows( 'NameItem', $cateID ) ) :
while ( have_rows( 'NameItem', $cateID ) ) : the_row();
if( get_row_layout() == 'NameLayout' ):
echo get_sub_field('NameFiel');
endif;
endwhile;
else :
I want to display in my WP search results ONLY pages with specific template beceuse I trying to build product catalog without any plugins. Pure WP code. Some pages are my products, and they have product.php template file. This is my search.php:
<?php
if ( have_posts() ) :
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/post/content', 'excerpt' );
endwhile;
the_posts_pagination();
else :
?>
<p><?php _e( 'No results.' ); ?></p>
<?php
get_search_form();
endif;
?>
And the question is, how to display only my product pages without any other pages?
Try this out!
This function will interrupt the search query and add some condition fo the query to returns specific pages.
//Filter the search for only posts and parts
function SearchFilter($query)
{
// Now filter the posts
if ($query->is_main_query() & $query->is_search && $search_template) {
$query->set('post_type', 'page');
$query->set('meta_key' => '_wp_page_template',);
$query->set('meta_value' => 'product.php');
}
// Returning the query after it has been modified
return $query;
}
add_action('pre_get_posts', 'SearchFilter');
Explaining the code:
The "post_type" will limit this filter for page post type only.
The condition Will limit this filter to only work on the search queries and not every time the posts query is called.
$query->is_search && $search_template
These parameters will filter the returned posts by the meta_key "_wp_page_template" which contains the page template, so we only return the pages with the page template "product.php".
$query->set('meta_key' => '_wp_page_template',);
$query->set('meta_value' => 'product.php');
I am developing a wordpress website which uses woocommerce for e-commerce functionality. I have 3 categories on the website and each one will have it's own template assigned for products within these categories.
I have created the templates and have got two of them working fine. However I'm not sure how to call the third template within my single-product.php file which contains the following code to change the templates depending on what category the product is assigned to:
<?php while ( have_posts() ) : the_post(); ?>
<?php global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal' );
} else {
woocommerce_get_template_part( 'content', 'single-product-merc' );
} ?>
<?php endwhile; // end of the loop. ?>
the templates i have are:
single-product-legal (custom template)
single-product-merc (default woocommerce template)
single-product-show (custom template)
The categories are legal, show and merchandise.
I need help with the php code so I can switch between the 3 templates. Not sure if I should use a switch statement, or how to implement it or if I could use elseif or how to implement that. Even if there's a completely different way to achieve this, I'd love to know.
Any pointers would be appreciated.
The best it to use elseif:
if ( in_array( 'legal', $categories ) ) {
woocommerce_get_template_part( 'content', 'single-product-legal');
}elseif (in_array('show', $categories)){
woocommerce_get_template_part('content', 'single-product-show');
}else {
woocommerce_get_template_part( 'content', 'single-product-merc');
}
Rather than adding a seperate elseif for merchandise you can just do it in the else like above because by default if it is not legal or show it must be merchandise, but you may just add another elseif as well with the same basic structure.
By default WordPress homepage shows latest posts. How can I display the latest posts on a page that is not the homepage ?
My first goal "GOAL A" is for the homepage to display a specific category called "popular posts" (instead of latest posts).
"GOAL B" is to have a link on the menu to ALL posts ordered by date, aka "the latest posts".
I have accomplished GOAL A with code below. How can I accomplish "GOAL B" ? I can make a category called "New" and make that a link on the menu, but how can I make it display all posts ordered by date ? Or is there a better method ?
.
"GOAL A" CODE: display specific category on homepage
function popular_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'category_name', 'popular' );
}
}
add_action( 'pre_get_posts', 'popular_category' );
I think the best here will be is to create a static front page with a blog page. This seems to be fit for what you are trying to do
Here is how:
STEP 1
You should delete the code in your question. This will not be necessary here
STEP 2
Make a copy of your page.php (or index.php) and rename it front-page.php. Open it up, and replace the loop with a custom query which will only display posts from the desired category. Unfortunately, pre_get_posts does not work on a static front page, so here you will have to make use of a custom query.
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'page' ) ) ? get_query_var( 'page' ) : 1;
// the query
$the_query = new WP_Query( 'category_name=popular&posts_per_page=10&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Just remember, for paging on a static front page, you have to use page, not paged as you would for all other custom queries.
STEP 3
Make a copy of your index.php and rename it home.php. This will be your blog page template
STEP 4
You can now set your static front page and blog page in the back. You should have a read here about setting up a static front page and a blog page