WordPress: get_permalink doesn't work in plugin - wordpress

I am trying to build a sitemap plugin ,but am stuck at a simple WordPress loop. Every time I try to get the URL, the site crash. Its the get_permalink that is making the loop and crashing the site. I have tested these and non of them works for me:
get_permalink
the_permalink
get_post_permalink
get_the_permalink
The loop:
function fa_sitemap_build() {
// Create/open the file
$file = fopen( get_template_directory() . '/sitemap.xml','wb');
$the_query = get_posts('post_type = page');
foreach ( $the_query as $post ) {
$title = get_the_title($post->ID);
$link = get_permalink($post->ID);
$date = $post->post_modified;
$url .= '
<url>
<title>'.$title.'</title>
<url>'.$link.'</url>
<lastmod>'.$date.'</lastmod>
</url>
';
}
wp_reset_postdata();
$sitemap = '
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
'.$url.'
</urlset>';
// write content to the file
fwrite( $file, $sitemap );
fclose( $file ); // Close the file
}

The arguments passed to the get_posts() must be an array. Try the following code.
<?php
$args = array(
'post_type' => 'page',
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
echo $post->title;
echo '<br />';
echo $post->post_content;
echo '<br />';
echo get_permalink( $post->ID );
}
or you can use the following code as well, it works same.
$args = array(
'post_type' => 'page',
);
$posts = get_posts( $args );
foreach ( $posts as $post ) {
setup_postdata( $post );
the_title();
echo '<br />';
the_content();
echo '<br />';
the_permalink();
}
wp_reset_postdata();
Reference:
get_posts() function on Codex

Related

WordPress get_posts() is NOT returning template tags

I want to get the latest 5 posts using WordPress get_posts function. I did the following:
In functions.php file I have added extra.php file which contain following code:
if( !function_exists( 'evertstrap_post' ) ) {
function evertstrap_post() {
$args = array(
'post_type' => 'post',
'numberposts' => 5,
);
$recent_posts = get_posts( $args );
foreach ( $recent_posts as $post ) {
setup_postdata( $post );
echo get_the_title();
echo '<br/>';
}
wp_reset_postdata();
}
}
Now, from home.php file I am calling evertstrap_post() BUT it's not get the latest 5 posts !!
BUT
If I directly put the code into index.php file then it's working.
How can I solve it?
I've seen this sometimes in WordPress where echoing the output is unfavorable. Could you give this a shot?
if( !function_exists( 'evertstrap_post' ) ) {
function evertstrap_post() {
global $post;
$args = array(
'post_type' => 'post',
'numberposts' => 5,
);
$recent_posts = get_posts( $args );
$output = '';
foreach ( $recent_posts as $post ) {
setup_postdata( $post );
$output .= get_the_title();
$output .= '<br/>';
}
wp_reset_postdata();
return $output;
}
}
Then in home.php you could do:
<?php echo evertstrap_post(); ?>
Try this:
if( !function_exists( 'evertstrap_post' ) ) {
function evertstrap_post() {
$args = array(
'post_type' => 'post',
'numberposts' => 5,
);
$recent_posts = get_posts( $args );
foreach ( $recent_posts as $post ) {
setup_postdata( $post );
echo get_the_title($post->ID);
echo '<br/>';
}
wp_reset_postdata();
}
}
You saw only last post title 5 times because get_the_title() is a template tag used in "while post : do post" wordpress loop. However the same function accepts argument with post ID. more here: https://developer.wordpress.org/reference/functions/get_the_title/
Getting posts via function get_posts() you store post obejcts in an array and using normal foreach loop you can retrieve object data using $post->key_name within this loop. PHP: Getting data out of an object
Also I recommend wordpress codex. Its very well documented.

How do I get the woocommerce product price via functions.php

I am curretly trying to create a shortcode that shows 3 products and the snippet looks like this:
function show_recent_products(){
$args = array('posts_per_page' => 3, 'post_type' => 'product');
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
echo '<div id="recent-posts" class="flex space-between">';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
echo '<div class="woocommerce_recent_products" style="background: url('.get_the_post_thumbnail_url().')">';
echo '<p>';
//echo get_regular_price();
echo'</p>';
echo '</div>';
}
echo '</div>';
}
wp_reset_postdata();
}
How can I get hold of the product price?
Reason why I am not using the default recent products shortcode it because the layout looks completely different and I would not know how to change the default code.
You can fetch product detail from ID. Use wc_get_product(). This will return Product Object.
$current_product = wc_get_product( get_the_ID() );
After that you can use this object $current_product and fetch relevant product information using its methods like get_price(). Example.
echo $current_product->get_price();
I used the global product inside the while loop and with I managed to get hold of all the product information.
function show_recent_products(){
global $post;
$args = array('posts_per_page' => 3, 'post_type' => 'product');
$post_id = $post->ID;
$product = wc_get_product( $post_id );
$custom_query = new WP_Query( $args );
if ( $custom_query->have_posts() ) {
echo '<div id="recent-posts" class="flex space-between">';
while ( $custom_query->have_posts() ) {
$custom_query->the_post();
global $product;
echo '<a href="'. get_the_permalink() .'" class="woocommerce_recent_products flex align-center" style="background: url('.get_the_post_thumbnail_url().')">';
echo '<div class="recent-content-inner">';
echo '<div>';
echo '<p class="recent-add-title">';
echo get_the_title();
echo'</p>';
echo '<p class="recent-add-price">';
echo "R". $product->price;
echo'</p>';
echo '<p class="recent-add-cart">';
echo "ADD CART";
echo'</p>';
echo'</div>';
echo'</div>';
echo "</a>";
}
echo '</div>';
}
wp_reset_postdata();
}

How to list all posts of a category in wordpress shortcode?

I need a shortcode that lists all posts of a certain category.
I found this php code that works on page templates but as soon as I add it in shortcodes it doesn't work (and I really need it in shortcode format):
<ul>
<?php
$catPost = get_posts(get_cat_ID("31")); //change this
foreach ($catPost as $post) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;?>
</ul>
So how can I do it?
Here's an updated and tested version of Amin. T's code. Add to your functions.php file.
/*
* Output a simple unordered list of posts in a particular category id
* Usage e.g.: [posts_in_category cat="3"]
*/
function posts_in_category_func( $atts ) {
$category_id = $atts['cat'];
$args = array( 'category' => $category_id, 'post_type' => 'post' );
$cat_posts = get_posts($args);
$markup = "<ul>";
foreach ($cat_posts as $post) {
$markup .= "<li><a href='" . get_permalink($post->ID) . "'>" . $post->post_title . "</a></li>";
}
$markup .= "</ul>";
return $markup;
}
add_shortcode( 'posts_in_category', 'posts_in_category_func' );
It should be something like this (add this to your functions.php file)
function posts_in_category_func( $atts ) {
$category_id = $atts['cat'];
?>
<ul>
<?php
$args = array( 'category' => $category_id, 'post_type' => 'post' );
$catPost = get_posts($args);
foreach ($catPost as $post) : setup_postdata($post); ?>
<li><?php the_title(); ?></li>
<?php endforeach;?>
</ul>
<?
}
add_shortcode( 'posts_in_category', 'posts_in_category_func' );
You can call it like that [posts_in_category cat=1]

to show thumbnail along with recent posts on blog page in wordpress

<?php echo get_the_post_thumbnail($post_id); ?>
Above is the code that i am using on page but nothing is showing on page,want to show thumbnail of recent post along with the content on blog page
Are you using the WP_Query method ? If not then use
$args = array(
'post_type' => 'post',
'posts_per_page'=> your_desired_number_of_post,
);
// The Query
$the_query = new WP_Query( $args );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
echo '<li>';
$the_query->the_post();
the_post_thumbnail();
the_content();
echo '</li>';
}
echo '</ul>';
}
else {
echo 'no posts found';
}
/* Restore original Post Data */
wp_reset_postdata();

Getting the post ID outside outside of the Wordpress loop

So I have a snippet of code that grabs the categories and their coinciding posts and lists them outside of the loop (Below). I've been trying to get the post to link to #post-[ID] instead of the permalink - but I keep failing. Can anyone help?
<ul id="sidebar">
<?php
foreach( get_categories('orderby=ID&order=desc') as $cat ) :
if( !$cat->parent ) {
echo '<li class="title"><h2>' . $cat->name . '</h2>';
echo '<ul>';
process_cat_tree( $cat->term_id );
}
endforeach;
wp_reset_query(); //to reset all trouble done to the original query
//
function process_cat_tree( $cat) {
$args = array('category__in' => array( $cat ), 'numberposts' => -1);
$cat_posts = get_posts( $args );
$id = $post->ID;
global $post;
if( $cat_posts ) :
foreach( $cat_posts as $menuPost ) :
echo '<li';
if ( $menuPost->ID == $post->ID ) { echo ' class="active"'; }
echo '>';
echo '' . $menuPost->post_title . '';
echo '</li>';
endforeach;
endif;
echo '</ul></li>';
}
?>
The above code is outputting UL/LI tags like this:
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
CATEGORY
Post
Post
Post
Admittedly, I don't exactly understand what you mean by "linking to #post-[ID]", but going with the question title:
If you use get_permalink() when echoing the link, you will get the permalink - that's just what that function does.
Use get_the_ID() instead, if you want the post-ID returned, or the_ID() if you want it displayed (the_ID() is the same as echo get_the_ID()).
Edited from here:
If you're otherwise happy with the above code, changing
echo '' . $menuPost->post_title . '';
to
echo '' . $menuPost->post_title . '';
ought to do it.
However, I'd go about it like so:
echo '<ul>';
$cat_args = array(
'orderby' => 'name',
'order' => 'ASC'
);
$categories = get_categories($cat_args);
foreach($categories as $category) {
echo '<li class="title"><h2>' . $category->name . '</h2><ul>';
// query posts of that category:
$query_args = array(
'cat' => $category->cat_ID,
'posts_per_page' => -1
);
$your_query = new WP_Query( $query_args );
// loop through them:
while ( $your_query->have_posts() ) : $your_query->the_post();
echo '<li><a href="#post-';
the_ID();
echo '">';
the_title();
echo '</a></li>';
endwhile;
echo '</ul></li>';
// Reset Post Data
wp_reset_postdata();
}
echo '</ul>';

Resources