I want to include custom Hashtag search page in my Wordpress theme.And I have the following code in functions.php file
function hashtag_template( $template ){
global $wp_query;
$hash = get_query_var('hashtag');
if( !empty($hash) ){
return locate_template('hashtags-search.php');
}
return $template;
}
add_action( 'template_include', 'hashtag_template' );
and I have also added a hashtag-search.php template
but when I am searching with www.mysite.com/?hashtag=string, it does not shows that(hashtag-search.php) search page.Where may be the problem?
Related
On custom-page-template.php, what code can customize the section ( and <meta name=”description”) for all pages which are based on this template?
I tried using the following but they nothing on a page template. (They work on functions.php though).
add_filter( 'document_title_parts', 'custom_document_title_parts' );
add_action( 'wp_head', 'my_add_meta_description');
Any idea, what code can customize and in a custom-page-template.php?
Thanks.
Use this in functions.php: (change page template's name in is_page_template)
add_filter( 'document_title_parts', function( $title_parts_array ) {
if ( is_page_template('custom.php') ) {
$title_parts_array['title'] = 'New title';
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description");</script>';
}
return $title_parts_array;
} );
Or you can use this before get_header() in your custom page template
function new_page_title() {
echo '<script>document.querySelector(\'meta[name="description"]\').setAttribute("content", "New description2");</script>';
return 'New Title2';
}
add_action( 'pre_get_document_title', 'new_page_title' );
I am using the Genesis framework and need to load a custom footer when a certain page template loads, but I am having issues passing the page template file name into the function. I cannot use the slug because the template can be used for multiple pages, so I really need to use the template file name instead. I believe I'm on the right path with what I have so far, but I don't quite understand what I'm reading about get_query_var and how it can help pass the value into my function.
So far my code is as follows:
remove_action( 'genesis_footer', 'genesis_do_footer' );
if (is_page_template( 'page-alternate.php' )) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
Any assistance would be greatly appreciated. Thank you
The "Is Page Template" function actually looks for the template page name not the file so in this example if the page template name was "alternate" you would have:
remove_action( 'genesis_footer', 'genesis_do_footer' );
$CurrPageTemplate = get_page_template_slug( get_queried_object_id() );
if ($CurrPageTemplate == 'page-alternate.php' ) {
add_action( 'genesis_footer', 'do_alternate_footer' );
} else {
add_action( 'genesis_footer', 'do_main_footer' );
}
function do_alternate_footer() {
echo 'xxx';
}
function do_main_footer() {
echo 'yyy';
}
See how you go with that.
I've a WordPress website and am trying to add a custom search option in the website's home page as well in the header section. Unfortunately I can't see any option to include it from the website's dashboard and decided to follow the link to create a custom one as follows:
Tutorial Link - Custom Search
and this is the code that I've tried same as the tutorial:
add_action('tickercontainer','add_search_to_footer'); //Guessinghere I've to declare the div class
function add_search_to_footer() {
get_search_form();
}
function SearchFilter($query) {
if ( !is_admin() && $query->is_search ) {
$query->set('post_type', 'post'); //OR USE 'PRODUCT'
}
return $query;
}
add_filter('pre_get_posts','SearchFilter');
function storefront_search_form_modify( $html ) {
return str_replace( array('Search …','Search'), array('WooCommerce Hooks, Storefront Theme, Google AdWords...','Search Article'), $html );
}
add_filter( 'get_search_form', 'storefront_search_form_modify' );
function new_nav_menu_items($items) {
$searchicon = '<li class="search"><i class="fa fa-search" aria-hidden="true"></i></li>';
$items = $items . $searchicon;
return $items;
}
add_filter( 'wp_nav_menu_additional-resources_items', 'new_nav_menu_items' );
Though I am not sure what I am missing here and would expect some ideas to implement in an appropriate way.
Note: Right now, after including the code above, I can't see the search option in the website.
I'm trying to disable Jetpack Carousel on a specific post ID using the following code in my functions.php
function djcoh_disable_carousel( $value ) {
wp_reset_query();
if ( is_page( 614 ) ) {
$value = true; // true to disable Carousel
}
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
Here's the reference for jp_carousel_maybe_disable on GitHub
It seems that I'm unable to use is_page() within functions.php - though I thought I'd be able to by using wp_reset_query() as mentioned in the codex
What am I missing?!
The code you have is from a tutorial which is intended for running as a simple plugin. The reason your code doesn't currently work is because you are using it in the functions.php.
In it's current form your function is called as soon as it is read as part of the functions.php file. This is usually some time before the page is formed, and so you can't grab the page id with is_page{}.
Instead you should query the page and get it's id as follows:
function djcoh_disable_carousel( $value ) {
//get the global
global $post
echo "TEST PAGE ID: ".$post->ID;
//wp_reset_query();
if ( $post->ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
if that doesn't work try this:
function djcoh_disable_carousel( $value ) {
//get the global
global $wp_query;
$post_ID = $wp_query->post->ID;
echo "TEST PAGE ID: ". $post_ID;
//wp_reset_query();
if ( $post_ID == 614 ) {
$value = true; // true to disable Carousel
}
wp_reset_query();
// Return original or changed value
return $value;
}
add_filter( 'jp_carousel_maybe_disable', 'djcoh_disable_carousel' );
If none of the above work then your script is being called far too early in the process to grab the page id. So, the easiest option would be to simply place this script in it's own .php file and then upload that to the plugins root folder. Then activate it from the plugins menu.
The final option would be to create this as a filter or script and add the function call in the actual page template.
I managed this by using REQUEST_URI within a plugin file:
<?php
// No direct access
if ( ! defined( 'ABSPATH' ) ) exit;
if ( $_SERVER["REQUEST_URI"] === '/PAGE-SLUG/' ) {
add_filter( 'jp_carousel_maybe_disable', '__return_true' );
}
Change PAGE-SLUG for your slug and you are all set.
You can find info on REQUEST_URI in PHP's manuals:
'REQUEST_URI'
The URI which was given in order to access this page; for instance, '/index.html'.
It seems simplest to conditionally dequeue the Jetpack carousel script and stylesheet. The conditionals that you would typically use to control output would be available at the point in the request when the wp_footer action fires.
add_action( 'wp_footer', function() {
if ( is_page( $page ) ) {
wp_dequeue_script( 'jetpack-carousel' );
wp_dequeue_style( 'jetpack-carousel' );
}
}
Be certain to modify the is_page function to include the $page parameter or the condition will match all pages. Place the code in your theme's functions.php file and the Jetpack carousel should be disabled.
i need to create an custom template for childs of the a specific category.
Is possible ?
You could add a conditional in your category.php or archive.php file for getting your custom template if url match the category or add a function in your functions.php theme. An example for the latest, where template_cat_c.php is your custom template:
add_filter( 'template_include', 'template_cat' );
function template_cat( $template ) {
if ( is_subcategory_from($parent_id) ) {
return 'template_cat_c.php';
} else {
return $template;
}
}
Check the function there: https://wordpress.stackexchange.com/questions/89512/wordpress-function-like-is-category-for-subcategory-is-subcategory to make the is_subcategory_from($parent_id).