Have a wordpress site with woocommerce.
I'm trying to add descriptions for the categories.
It is only adding a description for the first category and then copies it to the other categories.
In my child-themes functions.php I added:
function add_descript() {
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $post;
if ( ! $post->post_excerpt ) {
return;
}
?>
<div itemprop="description">
<?php echo apply_filters( 'woocommerce_short_description', $post->post_excerpt ) ?>
</div> <?php
}
add_action( 'woocommerce_before_subcategory_title', 'add_descript' );
Is there a way to loop through all of the categories and grab and excerpt?
Thanks,
Matt
Related
I need the last post not to be displayed on the post archive page.
What should I do to do this?
You can see the page code of my page archive below.
<?php
/* Start the Loop */
$i=0;
while ( have_posts() ) :
if($i==5) {
echo '
adv code
';
}
the_post();
get_template_part( 'template-parts/content', 'archive' );
$i++;
endwhile;
custom_pagination();
else :
get_template_part( 'template-parts/content', 'none' );
endif;
?>
note: Only the last post. Because the last post of the page archive is displayed manually in the slider of that page. I do not want the last post in the list to be displayed and repeated.
Using offset to the query will do the trick like
function exclude_last_post( $query ) {
if ( $query->is_main_query() && !is_admin() && is_archive() ) {
$query->set( 'offset', '1' );
}
}
add_action( 'pre_get_posts', 'exclude_last_post' );
I want to display the value of a custom-field in the content-product.php in woocommerce. I did like this, but the output is only the word "array".
where is my mistake?
Thanks a lot!
rabox
<?php if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
global $product;
// Ensure visibility
if ( empty( $product ) || ! $product->is_visible() ) {
return;
}
?>
<li <?php post_class(); ?>>
<span class="product_additional_info"><?php echo get_post_meta($post-
>ID, ‚additional-info‘, true); ?></span>
I found a piece of code that works here https://wordpress.stackexchange.com/questions/179451/unable-to-display-custom-fields-on-woocommerce-product-pages
It goes like that:
<?php
$custom_fields = get_post_custom($post->ID);
$my_custom_field = $custom_fields["Name of your Field"];
foreach ( $my_custom_field as $key => $value ) {
echo "<strong>$key: </strong> $value <br />";
}
?>
Would be interesting to know why I cannot use the "wordpress-way" of using custom fields.
Woocommerce content-product.php uses global $product; so you can get the product id with $product->get_id()
Now, you can get the value of custom field by passing the product id and custom field name in get_post_meta();
Example: get_post_meta( $product->get_id(), '_your_custom_field', true );
I want to create a simple plugin that prints the content and the excerpt in the head section. Below is the code I tried but didn't worked:
function content_excerpt() {
if( is_single() ) {
the_content();
the_excerpt();
}
}
add_action( 'wp_head', 'content_excerpt' );
Try following:-
function content_excerpt() {
if( is_single() ) {
//use global $post variable
global $post;
echo $post->post_content; //echoing post_content
echo $post->post_excerpt; //echoing post_excerpt
}
}
add_action( 'wp_head', 'content_excerpt' );
Currently I have the following code:
<?php while ( have_posts() ) : the_post(); ?>
<?php
if ( get_post_type() !== 'post' ) {
if ( get_post_type() == 'landing-pages' ) {
get_template_part( 'templates/content/archive', 'landingpages' );
} elseif ( get_post_type() == 'product' ) {
get_template_part( 'templates/content/archive', 'product' );
} else {
get_template_part( 'templates/content/archive', 'cpt' );
}
} else {
get_template_part( 'templates/content/archive', 'posts' );
};
?>
<?php endwhile; ?>
<?php get_template_part( 'templates/modules/nav', 'pagination' ); ?>
However, this just displays most recent to oldest. With custom posts, woocommerce products, posts, pages, all mixed togather.
I want only products to be displayed, then once all matching products have been displayed, anything else that matches the search query is returned, eg: other custom post types, pages, and posts.
Any suggestions on how to achieve this?
Thanks.
Copy and paste the following code in functions.php file of your theme.
add_filter('posts_orderby','search_sort_custom',10,2);
function search_sort_custom( $orderby, $query )
{
global $wpdb;
if(!is_admin() && is_search())
$orderby = $wpdb->prefix."posts.post_type DESC, {$wpdb->prefix}posts.post_date DESC";
return $orderby;
}
On my blog's archive page if I click on a month, it takes me to a page showing me all the posts that I've created that month (obviously). Is there a way to filter that page so it only shows me posts from one of my categories?
archive.php
<?php if ( have_posts() ) : ?>
<div class="rightColumn">
<?php
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
endwhile;
// Previous/next page navigation.
twentyfourteen_paging_nav();
else :
get_template_part( 'content', 'none' );
endif;
?>
</div>
<?php
get_footer();
Thanks.
I ended up finding a solution that worked for me:
function only_show_blog_posts( $query ) {
// Only modify the main loop query
// on the front end
if ( $query->is_main_query() && ! is_admin() ) {
// Only modify date-based archives
if ( is_date() ) {
// Only display posts from category ID 1
$query->set( 'cat', '12' );
}
}
}
add_action( 'pre_get_posts', 'only_show_blog_posts' );
try using the pre_get_posts hook, something along the lines of:
function filter_by_category( $query ) {
if ( $query->is_archive() && $query->is_main_query() && basename( $_SERVER['PHP_SELF'] ) == 'archive.php' ) {
$category_id = get_cat_ID( 'THE_CATEGORY_NAME' ); //change to the actual name of the category you are filtering with
$query->set( 'cat', $category_id );
}
}
add_action( 'pre_get_posts', 'filter_by_category' );
you can drop this code into your functions.php file
you can find more info about the pre_get_posts hook here
That simple hook helped me too. I modified the function a little to get the category Id from $_GET:
function only_show_blog_posts( $query ) {
if ( $query->is_main_query() && ! is_admin() ) {
$catId = (int)$_GET['catId'];
if ( is_date() && is_int($catId))
$query->set( 'cat', $catId);
}
}
No filters or hooks necessary. Just pass the category you want to filter for in the URL.
With an ID
https://myblog.com/2018/?cat=1234
With a slug
https://myblog.com/2018/?category_name=my-category-slug