custom field link button for different brand - php

i m creating a wordpress theme for mobiles in which i used a custom field with name of "brand". and add three type of brand value LG, Nokia, and Sony..
Then how to create three separate link buttons for these three type of values which show all other post for same brand value.
<a href="#"><?php echo get_post_meta( get_the_ID(), 'brand', true ); ?>
<?php _e( '', 'mobilewebsite' ); ?></a>
<li>Nokia</li>
<li>LG</li>
<li>Sony</li>

Assuming that the brand custom field is a meta value for posts, you can use this code to echo each brand field.
<?php
// WP_Query arguments
$args = array (
'post_type' => array( 'post' ),
'post_status' => array( 'publish' ),
'meta_query' => array(
array(
'key' => 'brand',
),
),
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
?>
<a href="#">
<?php echo get_field('brand'); ?>
</a>
<?php
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
?>

Related

Get Relationship Field Value Inside on Repeater going to Another Posts

<?php
$related_articles = get_posts(array(
'post_type' => 'book',
'meta_query' => array(
array(
'key' => 'related_articles', // name of custom field
'value' => '"' . get_the_ID() . '"',
'compare' => 'LIKE'
)
)
));
if( have_rows('related_articles_repeater') ): // i added this line im not sure about this
while( have_rows('related_articles_repeater') ) : the_row();
if( $related_articles ):
foreach( $related_articles as $article ):
// Do something to display the articles. Each article is a WP_Post object.
// Example:
echo $article->post_title; // The post title
echo $article->post_excerpt; // The excerpt
echo get_the_post_thumbnail( $article->ID ); // The thumbnail
endforeach;
endif;
endwhile;
endif;
?>
Hi guys! Whats wrong did i make? the code work well without repeater. I Wanted to work it with repeater so that i coded but is not working thougt.......................

Get parent if child doesn't have the terms of custom taxonomy

I'm trying to get the items from custom taxonomy on a page but not really going well. I would like to get the term name of custom taxonomy and custom field from custom post to show on the page. If there is no applicable item, would like to get the item from parent.
For example, As a category of food type,
**“Food> Italian> Pizza" (page category)**
On the other hand, in Recipes of custom post, If there was only
**"Food> Italian" (custom taxonomy is recipe-cat)**
The food of Pizza page will list recipe in the “Italian" category.
page category slug and taxonomy term are the same.
I appreciate your help!
$cat = get_the_category();
$children = get_term_children($cat->cat_ID ,'category');
$parents_cat = get_category(!($children));
if(!($children)) :
$args = array(
'post_type' => 'recipe',
'posts_per_page' => '5',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'recipe-cat',
'field' => 'slug',
'terms' => $parents_cat->slug,
)
),
);
$recipe = new WP_Query( $args );
if($parents_cat): ?>
<div class="recipe-box">
<h2><span><?php $parents_cat->name; ?>s recipe</span></h2>
<ul class="recipe-info">
<?php while ( $recipe->have_posts() ) : $recipe->the_post(); ?>
<?php $title = get_field('title'); if( $title ): ?>
<li><?php echo $title; ?></li>
<?php endif; ?>
<?php endwhile; ?>
</ul>
</div>
<?php endif; ?>
<?php endif; ?>

How to get all products from current WooCommerce product category?

Customizing my archive-product.php. How can I show only products within a certain category in a custom loop? This similar question, didn't solve my problem. I tried single_cat_title() to get the current category based on this question but got an error. I think I need to use get_queried_object() based on this documentation but I keep getting errors.
I tried this:
<?php
$category = single_cat_title('', false); // this returns your current category ?>
<?php
// Setup your custom query
$args = array(
'post_type' => 'product',
'product_cat' => $category,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php the_post_thumbnail(); ?>
<br>
<?php endwhile; wp_reset_query(); // Remember to reset ?>
I also tried:
`$term_name = get_queried_object()->name;`
// Setup your custom query
$args = array(
'post_type' => 'product',
'product_cat' => $term_name, );
Updated
On product category archive pages, to get the current product category term you will use:
Wordpress function get_queried_object() (to get the WP_Term Oject).
or Wordpress function get_queried_object_id() (to get the term Id).
Using directly the taxonomy parameter in a WP_Query is deprecated since WordPress 3.1. Instead you will use a tax query as follow:
<?php
// Get The queried object ( a WP_Term or a WP_Post Object)
$term = get_queried_object();
// To be sure that is a WP_Term Object to avoid errors
if( is_a($term, 'WP_Term') ) :
// Setup your custom query
$loop = new WP_Query( array(
'post_type' => 'product',
'posts_per_page' => -1,
'post_status' => 'publish',
'tax_query' => array( array(
'taxonomy' => 'product_cat', // The taxonomy name
'field' => 'term_id', // Type of field ('term_id', 'slug', 'name' or 'term_taxonomy_id')
'terms' => $term->term_id, // can be an integer, a string or an array
) ),
) );
if ( $loop->have_posts() ) :
while ( $loop->have_posts() ) : $loop->the_post();
echo '<div style="margin:8px; text-align:center;">
<a href="'.get_the_permalink().'">';
the_post_thumbnail();
the_title();
echo '</a></div>';
endwhile;
wp_reset_postdata(); // Remember to reset
endif; endif;
?>
Tested and works.
Documentation: WP_Query and Taxonomy Parameters

How to display posts of taxonomy in WordPress page template?

Not sure if this has been asked before, but I'm a bit lost. I've created a "Newsroom" Pods with a custom taxonomy of Newsroom Category. Newsroom Category has 3 fields: Press Release, Media, Others. I have a WordPress page template: taxonomy-newsroom_category.php
taxonomy-newsroom_category.php is used to display Pods posts if meets the following:
1 - pods = 'newsroom'
2 - taxonomy = 'press_release' || 'media' || 'others'
My issue right now is that I can't find a way to display the post details:
image(thumbnail), title(post title), date_published
I hope someone can help. Thanks
Here's the code I'm currently using:
<?php
//Setup Pod object
//Presuming permalink structure of example.com/pod-name/item-name
//See http://pods.io/code/pods/find
//Set $params to get 5 items
$params = array(
'limit' => 5,
);
//get current pod name
$pod_name = pods_v( 0, 'newsroom');
//get pods object
$pods = pods( $pod_name, $params );
//check that total values (given limit) returned is greater than zero
if ( $pods->total() > 0 ) {
//loop through items using pods::fetch
while ($pods->fetch() ) {
//Put title/ permalink into variables
$post_title = $pods->display('post_title');
$date_published = $pods->display('date_published');
$permalink = site_url( trailingslashit( $pod_name ) . $pods->field('permalink') );
?>
<div class="news-item col-sm-4">
<div class="news-item-img"></div>
<div class="news-item-header">
<h5 class="news-category"></h5>
<h2 class="news-item-title"><?php echo $post_title; ?></h2>
<h5 class="news-item-date"><?php echo $date_published; ?></h5>
</div>
</div><!-- close -->
<?php
} //endwhile;
} //endif;
// Output Pagination
//see http://pods.io/docs/code/pods/pagination
echo $pods->pagination( );
?>
I also found out solution for that... I had my Custom Post Type "studies" and I had to filter them, based on custom taxonomy category. If you want to write Posts of one taxonomy category, try to use something like that:
$type = $_GET['type'];
$args = array(
"post_type" => "studien",
"post_per_page" => -1,
"relation" => "AND"
);
if($type != "") {
$args['tax_query'][] = array(
'taxonomy' => 'market',
'field' => 'slug',
'terms' => $type
);
$wp_query = new WP_Query($args);
}
$type represents one category in my created Taxonomy (value comes from HTML code in select option), $args Is some query on database and 'market' is slug of my custom Taxonomy, $wp_query returns all filter Posts
screenshot of my custom taxonomy in custom post type. As you can see, I have two groups. First is clicked in two Posts and Second is clicked in last two Posts. Maybe it will helps you to give imagination
Check this out https://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters. This document explained how to query with custom post type and taxonomy in details. Youd code could be something like this.
$args = array(
'post_type' => 'newsroom',
'tax_query' => array(
array(
'taxonomy' => 'newsroom_category',
'field' => 'slug',
'terms' => 'press_release',
),
),
);
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
$id = get_the_ID(); // with post id, you can get whatever you want.
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
from the wordpress documenation:
https://codex.wordpress.org/Custom_Taxonomies
we have the below , for taxonomy person
$args = array(
'tax_query' => array(
array(
'taxonomy' => 'person',
'field' => 'slug',
'terms' => 'bob'
)
)
);
$query = new WP_Query( $args );

How to write more efficient fallback for empty wp_query in Wordpress

I have a WooCommerce store where I want to display a featured image & heading of one of the following (in order):
Featured Product
If no featured product, then sticky post
If no sticky post, then most recent post
But I also want to write efficient code. How do I simplify this and remove redundant PHP and HTML?
/* START FEATURED PRODUCT QUERY */
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_query' => array(
'key' => '_featured',
'value' => 'yes'
),
$query = new WP_Query( $args );
if( $query->have_posts() ) {
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
} else {
/* START FALLBACK POST QUERY */
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1
);
$query = new WP_Query( $args );
while( $query->have_posts() ) {
$query->the_post(); ?>
<a href="<?php the_permalink(); ?>" id="featured-blog-post">
<?php the_post_thumbnail('full');
the_title('<h2>', '<span>»</span></h2>' );
the_excerpt(); ?>
</a> <?php
} // end while
wp_reset_postdata();
}
The second WP_Query has the exact same HTML output, just different $args
I’m writing a similar query and I’m not sure you can make the query much more efficient in Wordpress than you already have. The only thing I did differently was to make the output of the posts a function so that it calls the same code. This’ll make it easier to update.
Also since you’re only querying one meta field in the first query I switched to a simple custom field query.
// Function to output posts
function output_posts( $query ){
while( $query->have_posts() ) {
$query->the_post();
echo '<a href="' . get_permalink() '" id="featured-blog-post">';
the_post_thumbnail( 'full' );
the_title( '<h2>', '<span>»</span></h2>' );
the_excerpt();
echo '</a>';
}
wp_reset_postdata();
}
// Featured query
$args = array(
'posts_per_page' => 1,
'post_type' => 'product',
'meta_key' => '_featured',
'meta_value' => 'yes',
);
$featured = new WP_Query( $args );
// If featured has posts
if( $featured->have_posts() ) {
// Output
output_posts( $featured );
// Else fallback
} else {
// Fallback query
$args = array(
'posts_per_page' => 1,
'post__in' => get_option( 'sticky_posts'),
'ignore_sticky_posts' => 1,
);
$fallback = new WP_Query( $args );
// If fallback has posts
if ( $fallback->have_posts() ){
// Output
output_posts( $fallback );
}
}

Categories