Getting the post ID outside outside of the Wordpress loop - wordpress

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>';

Related

Get the URL of the taxonomy's category in WordPress

I need help to retrieve the URL of the category in a taxonomy. I have a taxonomy called 'leagues' and it is displaying the image and the name of the category, I need to add the link so that people can go to that category when clicking the name. Here is the code I have below:
<ul>
<?php
$taxonomies = get_terms(array(
'taxonomy' => 'leagues',
'orderby' => 'name',
'show_count' => 0,
'pad_counts' => 0,
'hierarchical' => 1,
));
foreach ($taxonomies as $category) {
if ($category->parent == 0) {
$cat_id = $category->term_id;
$logos = get_field('logo', 'term_' . $cat_id);
//for image return format: Image Array
$logo = $logos['sizes']['thumbnail']; //default WP image sizes: thumbnail, medium, large
if ($logo) {
echo '<li>';
echo '<img src="';
echo $logo;
echo '" class="w-25 mr-4">';
echo '<a href="';
echo '#';
echo '">';
echo $category->name;
echo '</a>';
echo '</li>';
}
}
}
?>
</ul>
Since you are getting the term object in your loop ( $category ), you can use get_term_link() https://developer.wordpress.org/reference/functions/get_term_link/
get_term_link( $category->term_id )
echo '<a href="';
echo get_term_link( $category->term_id );
echo '">';
I think the function you're looking for is get_category_link().

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();
}

WordPress: get_permalink doesn't work in plugin

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

Get first two category only and excluding one in Wordpress

My code:
<?php
$category = get_the_category();
echo $category[0]->cat_name;
?>
<?php
$category = get_the_category();
echo $category[1]->cat_name;
?>
I want get two category as links but exclude "uncategorized" category.
Try below code:
<?php
$args = array(
'include' => '1,2'
);
$categories = get_categories( $args );
foreach ( $categories as $category ) {
echo '' . $category->name . '<br/>';
}
?>
In above code change 'include' => '1,2' and add the category that you want to include.
Find more here.
Updated :
If you want to get only first two category and exclude Uncategorized from that two category, then you can try below code :
<?php
$i=0;
$args = array(
'orderby' => 'id'
);
$my_category = get_categories($args);
foreach($my_category as $mcategory){
if($i==0 || $i==1){
if($mcategory->name != 'Uncategorized'):
echo '' . $mcategory->name . '<br/>';
endif;
}
$i++;
}
?>
Here I have added one dummy condition to make it work.

Returning an image url from its attachment ID using a custom post type

I'm using a custom post type plugin that returns an uploaded file's attachment ID instead of its url.
I've been able to get the image to display using wp_get_attachment_image_src as outlined in the codex here http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src but my problem is getting it to play nicely with the code on the template page used to call the information from the custom post type.
Stripping it down to the basics, this is what calls the custom post type info from the template page:
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
echo '<img src="' . $slide['slide'] . '" />';
}
?>
I'm having difficulty reconciling this with what the codex provides:
<?php
$attachment_id = 8; // attachment ID
$image_attributes = wp_get_attachment_image_src( $attachment_id ); // returns an array
?>
<img src="<?php echo $image_attributes[0]; ?>">
It seems like something like the following should work, but I'm obviously missing something with the php syntax
<?php
$slideshowplatform = get_post_meta($post->ID, 'slideshowplatform', true);
foreach($slideshowplatform as $slide) {
$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
echo '<img src="<?php echo $image_attributes[0]; ?>" />';
}
?>
Any thoughts would be appreciated, thanks.
I think this is what you want
if ( $post->post_type == 'slideshowplatform' && $post->post_status == 'publish' )
{
$attachments = get_posts(array(
'post_type' => 'attachment',
'posts_per_page' => -1,
'post_parent' => $post->ID,
'exclude' => get_post_thumbnail_id()
));
if ($attachments) {
foreach ($attachments as $attachment) {
$thumbimg = wp_get_attachment_link( $attachment->ID, 'thumbnail-size', true );
echo $thumbimg;
//$image_attributes = wp_get_attachment_image_src( $slide['slide'] );
//echo '<img src="' . $image_attributes[0] . '" />';
}
}
}

Resources