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' );
Related
In the template, wp_head() function adds bunch of styles and scripts.
Which overrides the other CSS of the theme and in return creates problems.
I tried using a function to remove the CSS and js. It worked but also removes the CSS from the WP admin toolbar.
function clear_styles_and_scripts() {
global $wp_scripts;
global $wp_styles;
foreach( $wp_scripts->queue as $handle ) :
wp_dequeue_script( $handle );
wp_deregister_script( $handle );
endforeach;
foreach( $wp_styles ->queue as $handle ) :
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
}
add_action( 'wp_enqueue_scripts', 'clear_styles_and_scripts', 100 );
This worked.
function clear_styles_and_scripts() {
global $wp_scripts;
global $wp_styles;
$styles_to_keep = array("wp-admin", "admin-bar", "dashicons", "open-sans");
foreach( $wp_styles ->queue as $handle ) :
if ( in_array($handle, $styles_to_keep) ) continue;
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
}
add_action( 'wp_enqueue_scripts', 'clear_styles_and_scripts', 100 );
Although I do not recommend to dequeue all scripts and styles, you can check file handle to ignore dequeuing that specific file:
foreach( $wp_styles ->queue as $handle ) :
// allows admin bar to be added
if( 'admin-bar' === $handle ) continue;
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
endforeach;
I am using WordPress shortcode with $wpdb->insert. It works, but it runs TWICE.
function f_registrazione()
{
$output = '';
if(!$_POST['txtNome'])
{
$output .= "do something in html";
} else {
$output .= "do something else in html";
}
global $wpdb;
$wpdb->insert( mg_nomi, array('des_nome' => $_POST['txtNome']) );
return $output;
}
add_shortcode( 'registrazione', 'f_registrazione' );
This function is written in a custom file linked to them's functions.php
What's wrong? I've already tried different solutions, none work.
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
wp_reset_postdata();
wp_reset_query();
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.
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
I want to exclude a category from my homepage, but my code didn't work:
<?php if( $category_items_query->have_posts() ): ?>
<div class="row columns-layout content-grid">
<?php if( $layout === 'two' ): ?>
<!-- EXCLUDE CATEGORY 17 -->
<?php query_posts($query_string . '&cat=-17'); ?>
<?php while( $category_items_query->have_posts() ): $category_items_query->the_post(); ?>
<div class="col-lg-6 col-md-6 recent-item two-columns post-box">
<?php get_template_part( 'parts/home-content', 'columns' ); ?>
</div><!-- .two-columns -->
<?php endwhile; ?>
<?php endif; ?>
</div><!-- .row -->
<?php endif; ?>
Please help me.
Thanks
Guido
Please do not use query_posts as it is ineffiecnt and re-run sql queries and will also mess up your pagination..
Instead use pre_get_posts e.g.
function exclude_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '-17' );
}
}
add_action( 'pre_get_posts', 'exclude_category' );
First you must define global variable like below , then you can try your code.
global $query_string;
query_posts($query_string . '&cat=-17');
Also there is another way that you can try as well :
In function.php :
function exclude_category( $wp_query ) {
$excluded = array( '-1' );
$wp_query->set('category__not_in', $excluded);
set_query_var( 'category__not_in', $excluded );
}
add_action( 'pre_get_posts', 'demo_exclude_category' );
In your code (you can use in your way) :
class Exclude_Cat_Plugin {
public function __construct() {
add_action( 'pre_get_posts', array( $this,'exclude_first_category_posts') );
}
public function exclude_first_category_posts( $wp_query ) {
if ( ! is_search() && ! is_archive() ) {
$excluded = array( '-1' );
$wp_query->set( 'category__not_in', $excluded );
set_query_var( 'category__not_in', $excluded );
}
}
}
I hope its help you.