How can i get slug from url in wordpress. For example my url is https://ww.test.com/test-page/test-post/data-to-get how can i get data-to-get from url in wordpress. I tried following code
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
what is wrong here
There is a built-in function for that in wordpress: url_to_postid.
<?php
global $wp;
$current_url = $wp->request;
$post_id = url_to_postid($current_url);
$slug = get_post_field( 'post_name', $post_id );
echo $slug;
You can achieve your output using below methods.
by using Global post variable.
<?php
global $post;
$post_slug = $post->post_name;
?>
using PHP
<?php
global $wp;
$current_url = $wp->request;
echo substr($current_url , strrpos($current_url , '/') + 1);
?>
Suppose URL: https://ww.test.com/test-page/test-post/data-to-get
OUTPUT: data-to-get
I have checked and it's work for me.
Let me know if this works for you.
Related
I'm new in Woocommerce. I would like to customize the template for the product page of Woocommerce just for the books category. I replaced the content-single-product.php file with a custom one modifying the code in single-product.php file like this:
<?php while ( have_posts() ) : the_post(); ?>
<?php
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'livros', $categories ) ) {
wc_get_template_part( 'content', 'single-product-books' );
} else {
wc_get_template_part( 'content', 'single-product' );
}
?>
<?php endwhile; // end of the loop. ?>
Now I want to do the same in replacing the product-image.php file with this one product-image-books.php. Tried like this but not working:
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'books', $categories ) ) {
wc_get_template_part( 'content', 'single-product-books' );
wc_get_template_part( 'single-product/product', 'image-books' );
} else {
wc_get_template_part( 'content', 'single-product' );
wc_get_template_part( 'content', 'product-image' );
}
?>
This file is in the template folder: woocommerce > single-product. Any help?
The single product images are displayed using woocommerce_show_product_images() function & hooked via woocommerce_before_single_product_summary hook.
So instead of calling template directly, first remove the action from that hook and then add your custom function on the same hook.
Your files to show images (product-image-books.php & product-image.php) must be with in wordpress\wp-content\themes\your-theme\woocommerce\single-product\ directory.
Please add below code in your theme's functions.php file or custom plugin file. I have tested the code & it's working fine in my installation.
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
add_action( 'woocommerce_before_single_product_summary', 'qt563_woocommerce_show_product_images', 20 );
function qt563_woocommerce_show_product_images() {
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'livros', $categories ) ) {
wc_get_template( 'single-product/product-image-books.php' );
} else {
wc_get_template( 'single-product/product-image.php' );
}
}
#Emmanu, Can you please verify & accept it as answer?
I want to get current url of page in init hook. but they return nothing. my code is.
function mship_access_level() {
global $wpdb;
global $post;
$slug = basename(get_permalink());
echo get_permalink();
exit;
}
add_action('init', 'mship_access_level');
you can try this below code for permalink
<?php
function mship_access_level() {
global $wpdb;
global $post;
$slug = basename(get_permalink( get_the_ID()));
echo get_permalink( get_the_ID());
exit;
}
add_action('init', 'mship_access_level');
?>
<?php
function mship_access_level() {
global $wpdb;
global $post;
$slug = basename(get_permalink($post->ID));
echo get_permalink($post->ID);
exit;
}
add_action('init', 'mship_access_level');
?>
please try this one also.
I'm trying to display the taxonomy term of a custom post type (like using <?php the_category( ' ' ); ?> in regular posts). The code below works but need to specify the taxonomy name, is there a way to use only the Post ID?
<?php
$terms = get_the_terms( $post->ID , 'taxonomy_name' );
foreach ( $terms as $term ) {
echo $term->name;
}
?>
Thanks in advance!
<?php print the_terms( $post->ID, 'taxonomy_name' , ' ' ); ?>
You cannot get the custom taxonomy terms without taxonomy name but the above code its shorter for you.
I found a way to do it. Using "get_post_taxonomies" and selecting the array witch cat[1]
<?php
$cat = get_post_taxonomies($post);
$terms = get_the_terms( $post->ID , $cat[1] );
// Loop
if ( $terms != null ){
foreach( $terms as $term ) {
$term_link = get_term_link( $term, $cat[1] );
// Print the name and URL
echo '' . $term->name . '';
unset($term); } }
?>
Try this.,
$term_list = wp_get_post_terms($post->ID, 'my_taxonomy', array("fields" => "names"));
print_r($term_list);
You must use the taxonomy without taxonomy can not get term details.
Thanks you!
I am creating a file outside of wordpress but i want to get wordpress post etc. I created the following which does whar i want it to do but it is missing th P tags. if i view this post through wp-admin it looks fine. if i view it through this function it doe snot look find
ini_set('display_errors','on');
//Global WordPress
global $wpdb;
if(!isset($wpdb))
{
require_once('wp-config.php');
require_once('wp-load.php');
require_once('wp-includes/wp-db.php');
}
$args = array("post_title" => $_GET['name'],"category_name"=>"knowledge-map");
$query = get_posts( $args );
$posttitle = $_GET['name'];
$postid = $wpdb->get_var( "SELECT ID FROM $wpdb->posts WHERE post_title = '" . $posttitle . "'" );
$post = $getpost= get_post($postid);
$post_title = sanitize_post_field( 'post_title', $post->post_title, $post->ID, 'display' );
$post_content = sanitize_post_field( 'post_content', $post->post_content, $post->ID, 'display' );
//$postcontent= $getpost->post_content;
//setup_postdata( $getpost );
$data = $post->post_content;
//foreach ( $getpost as $post ) : setup_postdata( $post );
//$data = get_the_title() . the_content();
//endforeach;
wp_reset_postdata();
echo $data;
post_content won't show proper formatting...basically ever. You need to apply the_content filter to it:
echo apply_filters('the_content', $data);
Here's where it's mentioned in the Codex.
While wpautop will only add paragraph tags where new lines exist, the_content runs a bunch of core filters (including wpautop). From the source, the_content includes:
add_filter( 'the_content', 'wptexturize' );
add_filter( 'the_content', 'convert_smilies' );
add_filter( 'the_content', 'convert_chars' );
add_filter( 'the_content', 'wpautop' );
add_filter( 'the_content', 'shortcode_unautop' );
add_filter( 'the_content', 'prepend_attachment' );
you can also use wpauto, it uses in wordpress for auto formatting of text
echo wpautop( $post_content );
Not very techy, but I found that if I copy everything from the "text" area in a Wordpress post, then paste it into a markdown to HTML editor, then it results in tags being added wherever Wordpress omits them!
Copy the "Text" from a Wordpress post
Paste into markdown/HTML editor
View the HTML for that markdown
Is it possible to loop in a wordpress plugin?
I've created this plugin that utilizes a wordpress loop to grab some info about posts in my custom post type of events:
function getEventsFeed() {
$args = array( 'post_type' => 'events' );
$loop = new WP_Query( $args );
$htmlOutput = '<h2>Events</h2>';
while ( $loop->have_posts() ) : $loop->the_post();
$date = get_post_meta($post->ID, 'events_0');
$location = get_post_meta($post->ID, 'events_9');
$htmlOutput .= '<tr><td>' . the_title() . '</td><td>' . $date[0] . '</td><td>' . $post->post_title .'</td><td>' . $location[0] . '</td></tr>';
endwhile;
$htmlOutput .= '</div>';
echo $htmlOutput;
}
Problem is only the the_title info is being returned. $post is not working within the loop so $post->ID and $post->post_title are not being returned. I'm using this exact code in another page template and it returns all the data correctly. I'm not sure why it won't return when I use it in a plugin.
Any ideas?
Try adding
global $post;
to the beginning of your function. $loop->the_post() will set the global $post variable, but it is not available inside your function scope.