Wordpress get_category_link with a custom template - wordpress

I'm listing categories for a custom post type, which is going great so far.
The problem is when I get the category link, is linked to a default category archive template, and I'd like to link it to a custom-page-template.
Is there a way to do that?
<?php
//Llamar al catálogo
$taxonomy = 'lookbook-category';
$orderby = 'date';
$post_type = 'post';
$args = array(
'taxonomy' => $taxonomy,
'orderby' => $orderby,
'post_type' => $post_type,
);
?>
<?php
$categorias = get_categories($args);
// Mostrar categorías
foreach ($categorias as $cat) {
echo '<div class="temporadas-catalogo ">';
echo '<h1 class="">'.$cat->name.'</h1>';
echo '<p class="">'.$cat->description.'</p>';
echo '<div class="center margin-top">';
echo '<a class="button " href="'.get_category_link($cat).'">Ver Catálogo</a>';
echo '</div>';
echo '</div>';
}
?>

My assumptions are that you are using a custom Taxonomy called 'lookbook-category', with this you can set a custom taxononmy template like so:
taxonomy-{taxonomy}.php
The Wordpress developer docs is an excellent resource for understanding how the template hierarchy works.
https://developer.wordpress.org/themes/basics/template-hierarchy/

Related

Woocommerce Category Heirarchy

How to show category hierarchy in woocommerce
What i want is something like this
given with this slug
domain.com/category/balloons/helium-baloons
the hierarchy would be:
Baloons
Helium Baloons
Decor
Weddings
To simplified the idea it should be something like this
you can use wp_list_categories function with arguments
wp_list_categories(
array(
'taxonomy'=>product_cat'
)
);
use the below code to achive this functionality
$myterms = get_terms( 'category', array( 'parent' => 0 ) );
$current_term = get_queried_object();
echo '<ul>';
foreach($myterms as $term){
echo '<li> '.$term->name.'';
if($term->term_id == $current_term->parent){
$child_terms = get_terms( 'category', array('child_of'=>$term->term_id) );
if($child_terms){
echo '<ul>';
foreach($child_terms as $child_term){
echo '<li> '.$child_term->name.'';
}
echo '</ul>';
}
}
echo '</li>';
}
echo '</ul>';
also if you have big child heirarchi you can make this code recursive.

Wordpress with Genesis framework, how do I change the output of an archive page for a custom content type?

I have an archive page for a custom content type, and I want to display one of the custom fields in the archive listing. Im not sure what hook to use or how to access the variables in the custom content type to display in -archive.php. Can anyone help me out with the relevant hooks, or how to access those field variables?
First create archive file with your custom post type, the file name should be like this archive-{post_type}.php
Go through the below code this may help you
<?php
remove_action ('genesis_loop', 'genesis_do_loop'); // Remove the standard loop
add_action( 'genesis_loop', 'custom_do_grid_loop' ); // Add custom loop
function custom_do_grid_loop() {
// Intro Text (from page content)
echo '<div class="page hentry entry">';
echo '<h1 class="entry-title">'. get_the_title() .'</h1>';
echo '<div class="entry-content">' . get_the_content() ;
$args = array(
'post_type' => 'custom post type', // enter your custom post type
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page'=> '12', // overrides posts per page in theme settings
);
$loop = new WP_Query( $args );
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post(); global $post;
echo '<div id="testimonials">';
echo '<div class="one-fourth first">';
echo '<div class="quote-obtuse"><div class="pic">'. get_the_post_thumbnail( $id, array(150,150) ).'</div></div>';
echo '<div style="margin-top:20px;line-height:20px;text-align:right;"><cite>'.genesis_get_custom_field( '_cd_client_name' ).'</cite><br />'.genesis_get_custom_field( '_cd_client_title' ).'</div>';
echo '</div>';
echo '<div class="three-fourths" style="border-bottom:1px solid #DDD;">';
echo '<h3>' . get_the_title() . '</h3>';
echo '<blockquote><p>' . get_the_content() . '</p></blockquote>';
echo '</div>';
echo '</div>';
endwhile;
endif;
echo '</div><!-- end .entry-content -->';
echo '</div><!-- end .page .hentry .entry -->';
}
/** Remove Post Info */
remove_action('genesis_before_post_content','genesis_post_info');
remove_action('genesis_after_post_content','genesis_post_meta');
genesis();
?>

How do I display a "category products drop down in a wordpress page" in Woocommerce 2.5.2

I would like to display a drop down menu for products in a category.
<select>
<option value="CODE HERE">Volvo</option>
</select>
So according to Wordpress coding..
<?php
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
} else {
// no posts found
}
/* Restore original Post Data */
wp_reset_postdata();
Okay so I investigated further and I am looking to do a single page template according to https://developer.wordpress.org I am using a child theme for Storefront which is called NOVA WP.
To make this "single page template" I copied page.php and renamed it to page-buildit.php
This is Mypage in which I actually editing the code. I did copy the code but it turns out blank
found this: WooCommerce: Create a shortcode to display product categories
but my undestanding is we cant do this anymore with the new wordpress version.
<?php
$args = array(
'order' => 'ASC',
'hide_empty' => $hide_empty,
'include' => $ids,
'posts_per_page' =>'-1'
);
$product_categories = get_terms( 'product_cat', $args );
echo "<select>";
foreach( $product_categories as $category ){
echo "<option value = '" . esc_attr( $category->slug ) . "'>" . esc_html( $category->name ) . "</option>";
}
echo "</select>";
?>
Check this out. This is the way to get product categories.!
You can also use the function wp_dropdown_categories to make your code simpler.
To get a dropdown of categories of products you can write like this.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1);
wp_dropdown_categories($args);
Or if you want to hold the output in a variable you can use the argument 'echo'=>0 and then echo the variable to get the same output.
$args = array('hide_empty'=> 0,
'taxonomy'=> 'product_cat',
'hierarchical'=>1,
'echo'=>0);
$cats = wp_dropdown_categories($args);
echo $cats;
Okay so here is how I solved it, with the help of Hemnath mouli, I already gave you the credit for the answer but I wanted to publish the products inside a category in a dropbox.
$args = array(
'posts_per_page' => -1,
'product_cat' => 'motherboard',
'post_type' => 'product',
'orderby' => 'title',
);
$products = new WP_Query( $args );
echo "<select>";
foreach ( $products as $product ) {
$products->the_post();
?>
<option value="<?php the_permalink(); ?>"> <?php the_title(); ?>
<?php
}
echo "</select>";
?>
Now I will need to show the image of this product after selecting it.

Returning the name of a category in Wordpress

I have a loop for a custom post type an all is working well, except I cannot return the actual category name that the post has assigned to it.
I have the following code:
<?php
$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'portfolio',
'orderby' => 'menu_order',
'order' => 'ASC'
));
if($posts) {
foreach($posts as $post) {
setup_postdata( $post );
?>
<div>
<div class="mix <?php get_cat_name( ); ?>" ><img src="<?php the_field('portfolio_image'); ?>" alt=""></div> <!-- ACF -->
</div>
<?php
}
}
wp_reset_postdata();
?>
This is just one of the WP Codex functions I have tried including tring all with echo. I imagine my problem is something else?
Many thanks in Advance.
According to the codex an id is required for this method (https://codex.wordpress.org/Function_Reference/get_cat_name). Leaving out the id will not work.
One solution would be to use get_the_category($postId) to retrieve the category for your post (https://codex.wordpress.org/Function_Reference/get_the_category). You can leave out the postId here
post_id
(integer) (optional) The post id.
Default: $post->ID (The current post ID)
Note that the method returns an object - not the category name. You can try the following code to see if it's working:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
try this code
<?php $terms = get_the_terms($post->ID, 'TAXONOMY' );
if ($terms && ! is_wp_error($terms)) :
$term_slugs_arr = array();
foreach ($terms as $term) {
$term_slugs_arr[] = $term->name;
}
$terms_slug_str = join( " ", $term_slugs_arr);
endif;
echo $terms_slug_str;?>
Replace your code
from
get_cat_name( );
to
$categories = get_the_terms($post->ID,'custom-texonomy-name');
$cat = key($categories);
echo $categories[$cat]->name;

How to display my all category names using loop (WordPress)

I have below code here. It's display that I want category names and description as well. Now I need to display post that they have inside their categories. How I do it?
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 10, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div class="one_fourth">';
echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
$post = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 10 );
$posts_array = get_posts( $post );
echo '</div>';
}
?>
If there have any other way to get child category post and display child category name and posts in loop. Please let me to know here.
After I solved this. I think this will help for you.
<?php
$args = array(
'orderby' => 'id',
'hide_empty'=> 0,
'child_of' => 5, //Child From Boxes Category
);
$categories = get_categories($args);
foreach ($categories as $cat) {
echo '<div style="width:20%;float:left;">';
echo '<h1 class="valignmiddle uppercase title-bold">'.$cat->name.'<img src="'.$cat->term_icon.'" alt="" class="alignleft"/>'.'<br />'.'<span class="solutions">'.$cat->description.'</span>'.'</h1>';
//echo '<br />';
$args2= array("orderby"=>'name', "category" => $cat->cat_ID); // Get Post from each Sub-Category
$posts_in_category = get_posts($args2);
foreach($posts_in_category as $current_post) {
echo '<span>';
?>
<li type='none' style='list-style-type: none !important;'><?='+ '.$current_post->post_title;?></li>
<?php
echo '</span>';
}
echo '</div>';
}
?>
Write for get all parent category
Now do a foreach loop for them and get their child category.
Now under above foreach write another foreach using this get post for child category
$parent_cats = get_categories($args);
foreach ( $parent_cats as $parent_cat) {
$child_cats = some wp functions to get child cats of current parent category
foreach ( $child_cats as $child_cat ) {
$child_cat_post = get the post of child category
}
}
Helpful links:
http://codex.wordpress.org/Function_Reference/get_categories
http://codex.wordpress.org/Template_Tags/get_posts
Wordpress has <?php wp_dropdown_categories( $args ); ?>
Example Usage (Dropdown without a Submit Button using JavaScript)
<li id="categories">
<h2><?php _e( 'Posts by Category' ); ?></h2>
<?php wp_dropdown_categories( 'show_option_none=Select category' ); ?>
<script type="text/javascript">
<!--
var dropdown = document.getElementById("cat");
function onCatChange() {
if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
location.href = "<?php echo esc_url( home_url( '/' ) ); ?>?cat="+dropdown.options[dropdown.selectedIndex].value;
}
}
dropdown.onchange = onCatChange;
-->
</script>
</li>
Other examples can be found on the Codex site - https://codex.wordpress.org/Function_Reference/wp_dropdown_categories

Resources