What's query for WordPress archives that I can put in functions and call by shortcode...
<?php do_shortcode('[archives]'); ?>
inside archive.php
Found get_queried_object();, I don't know is it right function.
Instead of
<?php while ( have_posts() ) : the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
<div class="entry-content">
<?php the_content(); ?>
/* Custom Archives Functions Go Below this line */
/* Custom Archives Functions Go Above this line */
</div><!-- .entry-content -->
<?php endwhile; // end of the loop. ?>
I want to call it by
<?php do_shortcode('[archives]'); ?>
I want to make function, something like
function archives( $atts, $content = null ) {
if ( have_posts() ) : while ( have_posts() ) : the_post();
$return .= '<div class="archives_posts">';
$return .= '<div class="archives_posts_title">';
$return .= ''.get_the_title($ID).'';
$return .= '</div>';
$return .= '</div>';
endwhile;
endif;
return $return;
}
add_shortcode( 'archives', 'archives' );
Add following codes in functions.php of your theme.
function my_archives($params, $content = null) {
extract(shortcode_atts(array(
'type' => 'style1'
), $params));
ob_start();
?>
<div class="archives_posts">
<?php wp_get_archives( array( 'type' => 'monthly', 'limit' => 10 ) ); ?>
</div>
<?php return ob_get_clean();
}
add_shortcode('archives','my_archives');
You can change arguments to list archive posts as per your requirement.
Use following shortcode to get list of archive posts
Use <?php echo do_shortcode('[archives]'); ?> in template
Use [archives] in posts/pages
Add the following lines to your /wp-content/themes/themeName/functions.php:
function sc_archive(){
return wp_get_archives();
}
add_shortcode('archives','sc_archive');
Then use it in your Page: [archives].
Related
I'm pulling my hair over here searching and trying to modify the WooCommerce core files.
So, what I am trying to do is modify my theme's woocommerce.php file because I want to display the products on my shop by category.
What I've figured out so far is that at woocommerce.php file in my theme's root folder there is this line
<?php woocommerce_content(); ?>
which displays all of the products. Then, I found out that the file content-product.php at mytheme/woocommerce/ is being ran for each product and in a few words, that file contains the styling and classes of each product. In other words, this file is being ran inside the loop.
So I now know that I have to modify some function that actually calls that file and I want to pass the product category to that function so I can display the products I want.
Doing some digging I have found that in wp-content/plugins/woocommerce/includes/wc-template-functions.php there is the function woocommerce_content() with the following code
function woocommerce_content() {
if ( is_singular( 'product' ) ) {
while ( have_posts() ) :
the_post();
wc_get_template_part( 'content', 'single-product' );
endwhile;
} else {
?>
<?php if ( apply_filters( 'woocommerce_show_page_title', true ) ) : ?>
<h1 class="page-title"><?php woocommerce_page_title(); ?></h1>
<?php endif; ?>
<?php do_action( 'woocommerce_archive_description' ); ?>
<?php if ( woocommerce_product_loop() ) : ?>
<?php do_action( 'woocommerce_before_shop_loop' ); ?>
<?php woocommerce_product_loop_start(); ?>
<?php if ( wc_get_loop_prop( 'total' ) ) : ?>
<?php while ( have_posts() ) : ?>
<?php the_post(); ?>
<?php wc_get_template_part( 'content', 'product' ); ?>
<?php endwhile; ?>
<?php endif; ?>
<?php woocommerce_product_loop_end(); ?>
<?php do_action( 'woocommerce_after_shop_loop' ); ?>
<?php else : ?>
<?php do_action( 'woocommerce_no_products_found' ); ?>
<?php
endif;
}
}
Finally, I think that wc_get_loop_prop function is what initializes the loop and I am trying to find a way to pass a param to that function (the product category ID for example) so I can call woocommerce_content(118) where 118 a product category and display my products the way I want.
Is there any way I can do this? I am stuck at this part for long now and can't seem to find a solution.
SOLUTION:
Finally, what I had to do was to simply create a function
function getProductsByCat($theCat) {
$args = array(
'post_type' => 'product',
'posts_per_page' => 50,
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'id',
'terms' => $theCat
)
)
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
endwhile;
} else {
return false;
}
return true;
}
And call it anywhere in my website to display products by cat ID like
<div class="col-md-12"><h4>Special Offers</h4></div>
<?php
if(!getProductsByCat(191)){
//echo "<p>No products available.</p>";
}
?>
I hope this will be helpful for those out there who are trying to do the same.
This is a music website with multiple artist pages - one page per artist. New content is added as a post with a Wordpress tag to denote the artist. This is so that I can add a Wordpress loop on each artist page to show all the posts filtered with that artist's tag.
I've got the filtered loop working correctly, but unfortunately it's currently hardwritten inside the page template's HTML, so it's only filtering for one tag. I don't want to create a new page template for each artist, so I'd like to add it to my functions.php file instead, where I can instead create a new shortcode for each artist.
Here's the current code in my page template, which filters the loop for all posts with our seefour tag:
<?php
query_posts( "tag=seefour" );
if ( have_posts() ) { ?>
<?php while ( have_posts() ) { ?>
<?php the_post(); { ?>
<div class="jd-box">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( ); ?>
<div class="jd-overlay"></div>
<div class="jd-overlay-text">
<?php the_title(); ?>
</div>
</a>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
I'm assuming the best option is to turn this into a seefour shortcode inside my functions.php file - how can I achieve this?
Bonus question: is this sustainable in the long run (with 30-50+ artists) or will it cause a lot of redundant code? Open to suggestions...
P.S. I know this kind of question has been answered already (starting with raw PHP), but since I'm starting with a mix of HTML/PHP (and I'm a PHP newb), I just can't get it to work. So my apologies for asking again.
First of all, you should never ever use query_posts(). It is internal WordPress function to create and maintain the main WordPress cycle. Using it, you can crash your site in unpredictable manner. You should use get_posts() or WP_Query instead.
To have your custom shortcode, add the following to your functions.php:
function showtag_shortcode( $atts ) {
$atts = shortcode_atts( array(
'tag' => '', // Default value.
), $atts );
$posts = get_posts( 'tag=' . $atts['tag'] );
if ( $posts ) {
$output = '';
foreach ( $posts as $post ) {
setup_postdata( $post );
$output .= '<div class="jd-box">';
$output .= '<a href="' . get_the_permalink( $post ) . '">';
$output .= get_the_post_thumbnail( $post );
$output .= '<div class="jd-overlay"></div>';
$output .= '<div class="jd-overlay-text">';
$output .= get_the_title( $post );
$output .= '</div>';
$output .= '</a>';
$output .= '</div>';
}
} else {
$output = 'no data';
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'showtag', 'showtag_shortcode' );
This function created [showtag] shortcode with one parameter: tag. You can use this shortcode on any page as follows:
[showtag tag="seefour"]
[showtag tag="disco"]
etc. You will have posts with relevant tags to be shown in place of your shortcode.
Putting a whole loop in a shortcode will make it messy, I know you can use shortcodes in Widgets etc as well but I guess that's not what you're after.
If so, the best option would be to make this code a page template say Artist and pass a variable in URL i.e. http://example.com/artist?t=seefour and then use the following code in the page template.
<?php
/**
* Template Name: Artist
*/
query_posts( "tag=" . $_GET['t'] );
if ( have_posts() ) {
?>
<?php while ( have_posts() ) { ?>
<?php the_post(); { ?>
<div class="jd-box">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( ); ?>
<div class="jd-overlay"></div>
<div class="jd-overlay-text">
<?php the_title(); ?>
</div>
</a>
</div>
<?php
}
}
}
?>
This can be used for any number of artists, totally flexible because it is dependant on a variable that is provided on run time (when accessed).
I want to display Posts order by Last updated on the home page, how to right function for it and where to paste that function in wordpress?
Create your plugin and paste this function in plugin file.
function wpb_lastupdated_posts()
{
// Query Arguments
$lastupdated_args = array(
'orderby' => 'modified',
'ignore_sticky_posts' => '1'
);
//Loop to display 5 recently updated posts
$lastupdated_loop = new WP_Query( $lastupdated_args );
$counter = 1;
$string .= '<ul>';
while( $lastupdated_loop->have_posts() && $counter < 5 ) : $lastupdated_loop->the_post();
$string .= '<li> ' .get_the_title( $lastupdated_loop->post->ID ) . ' ( '. get_the_modified_date() .') </li>';
$counter++;
endwhile;
$string .= '</ul>';
return $string;
wp_reset_postdata();
}
//add a shortcode
add_shortcode('lastupdated-posts', 'wpb_lastupdated_posts');
use this shortcode : [lastupdated-posts]
There are 3 ways to do this
add this code in the functions.php file
function shortcode_latest_homepage_posts(){
$lquery = new WP_Query(array('order' => 'DESC'));
if($lquery->have_posts()){
while($lquery->have_posts()){
$lquery->the_post();
the_title();
the_content();
}
}
wp_reset_postdata();
}
add_shortcode('latest_posts', 'shortcode_latest_homepage_posts');
and add simply this [latest_posts] shortcode in the page editor that you have assign for the front page in cms
OR
add this code in functions.php
function latest_homepage_posts(){
$lquery = new WP_Query(array('order' => 'DESC'));
if($lquery->have_posts()){
while($lquery->have_posts()){
$lquery->the_post();
the_title();
the_content();
}
}
wp_reset_postdata();
}
add_action('latest_post', 'latest_homepage_posts');
and add this code at the place where you want to show the post in the template that assign for or made for a home page like home.php or front-page.php
<?php do_action('latest_post');?>
OR
3. simply add this code at the place where you want to show the post in the template that assign for or made for a home page like home.php or front-page.php
<?php
$lquery = new WP_Query(array('order' => 'DESC'));
if($lquery->have_posts()){
while($lquery->have_posts()){
$lquery->the_post();
the_title();
the_content();
}
}
wp_reset_postdata();
?>
I'm trying to display a custom field in a widget I'm customizing but I can't get the custom field to display. I think it has something to do with the variable I'm using to get the post ID in the loop because when I change it to the standard the_title function, the widget works, so it has something to do with how I'm calling the custom field.
I know the key to the custom field is "wpcf-promo-title" but no matter what I've tried, the custom field (which holds a shortened version of the post title for the sidebar) just won't display. This code results in the thumbnails showing, but not the promo title. You can see this in action at http://www.cantstopshipping.com
Here's my code, including the query and the front end of the widget.
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
if ( ! $number = absint( $instance['number'] ) ) $number = 5;
if( ! $cats = $instance["cats"] ) $cats='';
// array to call recent posts.
$crpw_args=array(
'showposts' => $number,
'category__in'=> $cats,
);
$crp_widget = null;
$crp_widget = new WP_Query($crpw_args);
echo $before_widget;
// Widget title
echo $before_title;
echo $instance["title"];
echo $after_title;
// Post list in widget
echo "<ul>\n";
while ( $crp_widget->have_posts() )
{
$crp_widget->the_post();
?>
<li class="crpw-item">
<p style="float:left">
<?php the_post_thumbnail('sidebar-small'); ?>
</p>
<?php $promotitle = get_post_meta($post->ID, 'wpcf-promo-title', true); ?>
<p style="float:right; width:200px">
<?php echo $promotitle; ?>
</p>
<?php if ( $show_date ) : ?>
<span class="crpw-date"><?php echo "("; ?><?php echo get_the_date(); ?><?php echo ")"; ?></span>
<?php endif; ?>
</li>
<?php
}
wp_reset_query();
echo "<div class=\"fix\"></div>";
echo "</ul>\n";
echo $after_widget;
}
It looks like your global $post is missing.
But you could try get_the_ID() instead of $post->ID.
You should also consider getting rid of extract(), it's now considered a "bad" practice.
Another thing is that you should use wp_reset_postdata() to restore the global $post object. The wp_reset_query() call should be used with the query_posts() call.
I have set up a few categories and want to display specific ones based on is_page().
Inside page.php, I've created an if..else statement that checks the page name and prints out the specific category. My problem at the moment is that instead of just the_title being printed out the whole post is being printed.
Where am I going wrong with this?
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php if ( is_page( 'Greywater Recycling' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Greywater Recycling&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Stormwater Management' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Stormwater Management&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Rainwater Harvesting' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Rainwater Harvesting&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } ?>
<?php endwhile; // end of the loop. ?>
Many problems with your code. For one, is_page does not work inside the loop.
Second, don't mess with query_posts: When should you use WP_Query vs query_posts() vs get_posts()?. Really, forget about it for secondary loops.
Your code can be simplified to the following. In functions.php, we drop one function to get the Category ID by its Name. And another to do a secondary loop using the ID. And then, in page.php a simple call to those functions.
Documentation: Class_Reference/WP_Query.
page.php
Notice that you don't need to open PHP tags at each line, that makes the code dreadly difficult to read.
Use it only to swap between PHP and HTML
<?php
get_template_part( 'content', 'page' );
// This page title
$the_title = get_the_title();
// Search for category with the same name of the page
$category = brsfl_get_category_id( $the_title );
// Category found, go to the secondary loop
if( $category ) {
brsfl_check_print_categories( $category );
}
functions.php
Always prefix your function names with something distinctive to avoid conflicts that may take the site down.
/**
* Get the category ID based on its name
*/
function brsfl_get_category_id( $cat_name )
{
$term = get_term_by( 'name', $cat_name, 'category' );
// term found, return its ID
if( $term ) {
return $term->term_id;
}
// not found
return false;
}
/**
* Print a loop based on a category ID
*/
function brsfl_check_print_categories( $cat_id )
{
$args = array(
'post_type' => 'post',
'cat' => $cat_id,
'posts_per_page' => 5
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
}
else
{
echo 'No posts found...';
}
}
Although I've answered within the proposed scope, I think there are better solutions to this, like:
a shortcode, just adapt the functions.
using Advanced Custom Fields to show a meta box where you can select a very specific category (don't relying in page and category names) and use only the WP_Query function to print it out in the template.
i think it's better to get the category by the slug name.
i've tried your solution, it works fine but in the case of one of my category has a name with special html characters like apostrophes or quotes it doesn't.
here is the piece of code edited from your solution :
in your functions.php
function brsfl_get_category_id($cat_name){
$term = get_term_by( 'slug', $cat_name, 'category' );
if( $term ) {
return $term->term_id;
}
return false;
}
in your page.php
$the_slug = get_post(get_the_ID())->post_name;
$category = brsfl_get_category_id( $the_slug );
if( $category ) {
brsfl_check_print_categories( $category );
}