How to disable a sidebar in wordpress - wordpress

I am using a wordpress theme with two sidebars. I want to disable one for them for a specific page but the code I found regarding disabling sidebar by applying check on function:
<?php get_sidebar() ?>
It will disable both of them. How can I disable one of them only. with the other sidebar working.
Please help!!!!

You can simply for example
<?php if (!is_page('about-me')) get_sidebar(); ?>
This will disable sidebar on about me page. If you put this on page.php (if have any) otherwise put this on index.php. Here 'about-me' is page slug but you can also use page id like is_page(5) and as well as page title too. To check multiple page using slug, id and title you can use an array like
is_page(array(42,'about-me','Contact'));
For more about is_page() function see http://codex.wordpress.org/Function_Reference/is_page
Or using filter in functions.php, simply put this in functions.php
function disable_footer_widgets( $sidebars_widgets )
{
if (is_single())
{
$sidebars_widgets['footer'] = false;
}
return $sidebars_widgets;
}
add_filter( 'sidebars_widgets', 'disable_footer_widgets' );
This is just an example, you have to change widget name.

Related

Switch Between Multiple Headers in WordPress Theme

I am building a custom theme for WordPress. One thing I want to do is allow myself to switch the header from the edit page.
I know I can hard code in the header to switch based on the page ID or name, like this:
<?php
if(is_page(10)) {
get_header('new');
}
else {
get_header();
}
wp_head();
?>
But I want a drop down menu similar to the Page Template option in the sidebar. (See screenshot)
Screenshot of sidebar menu
I have looked for any online tutorials that cover this type of option, but they all cover the basic ID or name setup shown above.
Does anyone know of a good tutorial to create a drop down similar to Page Templates to use for multiple headers?
You can use Custom Metaboxes. That link contains a comprehensive tutorial on creating custom metaboxes for post meta fields by hand. You can also use a plugin like Advanced Custom Fields to create them.
Doing this would allow you to check for the header style value through get_post_meta() or get_field(), respectively.
<?php
// If using the Custom Metabox/post_meta approach:
$header_style = get_post_meta( get_the_ID(), 'my_custom_header', true );
// If using ACF:
$header_style = get_field( 'my_custom_header', get_the_ID() );
if( $header_style == 'new' ){
get_header('new');
if( $header_style == 'something-else' ){
get_header('something-else');
} else {
get_header();
}
?>

Wordpress Shortcode for keywords?

So, I know you can put:
function page_title_sc( ){
return get_the_title();
}
add_shortcode( 'page_title', 'page_title_sc' );
Into function.php to fetch the page title and get a [page_title] shortcode within Wordpress, but is it possible to do exactly that for keywords?
I put a focus keyword down in Yoast SEO and I would like to have a shortcode for that.
Or another idea: is there a way to have a custom shortcode field in every page? So that I only have to put something into that once and can use it as a shortcode within the whole page?
If you want a shortcode for Yoast to output the keyphrase automatically:
function wpso_61018203_output_yoast_keyphrase() {
// Make sure Yoast is installed/active.
if ( class_exists( 'WPSEO_Meta' ) ) :
// Hold the global post object.
global $post;
return WPSEO_Meta::get_value( 'focuskw', $post->ID );
endif;
}
add_shortcode('yoast_kw', 'wpso_61018203_output_yoast_keyphrase' );
You can then do this ['yoast_kw'] in your content, or use echo do_shortcode('[yoast_kw]'); in your template.
Use get_post_meta and grab _yoast_wpseo_focuskw:
function page_focus_keyword( ){
return get_post_meta(get_the_ID(), '_yoast_wpseo_focuskw');
}
add_shortcode( 'focus_keyword', 'page_focus_keyword' );
WordPress filters all content to make sure that no one uses posts and page content to insert malicious code in the database. This means that you can write basic HTML in your posts, but you cannot write PHP code.
And you want to use Wordpress shortcode for focus keyword.
Possible solution given on Wordpress.org Yoast SEO plugin support query on similar query, and checkout the linked solution
insert the below php snippet in functions.php
if(!function_exists('wpseoFocusKW'))
{
function wpseoFocusKW()
{
$focuskw = wpseo_get_value('focuskw', $post->ID);
echo $focuskw;
}
}
And to use the shortcode in another page for focused keyword, insert the below shortcode in any pages with yoast-seo plugin :
Shortcode for focus keyword
[<?php wpseoFocusKW();?>]

How to create Custom Pagination Permalinks in wordpress

I have an article with several pages in my wordpress blog. if for example i have the following link in my blog :
http://example.com/heartbreaking-photos
any idea how can i change the link of the second page from
http://example.com/heartbreaking-photos/2
to http://example.com/heartbreaking-photos/CUSTOM-STRING
CUSTOM-STRING aimed to be a custom title inside the page
To achieve this, you will need to do 2 things:
Disable the default WordPress canonical redirect - this is necessary, because WordPress will always redirect to the /2/ page when it encounters the page parameter in the URL or query args.
Add a custom rewrite rule to direct your custom title to the second page of your page - this is essentially necessary to allow the link format that you want.
In terms of code, this is what you need (this is a working solution, I've just tested it locally):
// Removes the canonical redirection
remove_filter( 'template_redirect', 'redirect_canonical' );
// Add custom rewrite rules
add_action( 'init', 'my_add_custom_rewrite_rules' );
function my_add_custom_rewrite_rules() {
// Slug of the target page
$page_slug = 'heartbreaking-photos';
// Page number to replace
$page_num = 2;
// Title you wish to replace the page number with
$title = 'custom-string';
// Add the custom rewrite rule
add_rewrite_rule(
'^' . $page_slug . '/' . $title . '/?$',
'index.php?pagename=' . $page_slug . '&page=' . $page_num, 'top'
);
}
There are three things you might want to configure or change here:
$page_slug - this is the slug of your page, in your case this is heartbreaking-photos
$page_num - the number of your pagination page, in your case this is 2
$title - the title you wish to use instead of your page number 2.
Feel free to alter the code as you wish, or copy it to cover more additional cases, similar to this one.
EDIT
Important: Once you use the code, go to Settings > Permalinks and click the "Save Changes" button. This will rebuild your rewrite rules, and is necessary for the solution to work.
Hope that helps. Let me know if you have any questions.
You can try this codex. Pass the arg and you will get page id, page title and use those
https://codex.wordpress.org/Function_Reference/get_pages
Or you can call page title by page id
$pagetitle= get_post_field( 'post_title', $page_id );
Ok, so basically you don't want to display the navigation link under the page (use css or modify the post template in the child theme) and add your custom link. If I understand it well:
Remove navigation links (depends on your theme, but basically):
.nav-links { display: none; }
You can add the custom link through function + custom fileds:
create a custom field, for example "my-url" in your post, see codex: https://codex.wordpress.org/Custom_Fields
add to your functions.php (in the child theme or in a custom site plugin):
function my_page_add_to_content( $content ) {
if ( ! empty(get_post_meta( get_the_ID(), 'my-url', true ) ) {
$content .= 'URL TEXT HERE'
}
return $content;
}
add_filter( 'the_content', 'my_page_add_to_content' );

Targeting the shop page only

This is what I need to do:
To target only the main shop page with CSS (a specific custom class I created as mentioned below).
This is what I've done and tried so far:
I have an override template of archive-product.php in my child theme.
In this override template I've added a div with a custom class custom-shop-class just before the product loop start, because I want to target the looped products specifically.
I tried targeting with class page-id-4 because if I hover over the "shop" page in wp-admin, it gave me http://.../post.php?post=4&action=edit
The problem I'm having is as follows:
From what I've discovered is that the same archive-product.php template is being used in the other various shop pages and not only in the main "shop" page (correct me if I'm wrong), so when I target my custom-shop-class with CSS, it affects all the other shop pages using the same template file, and it must not.
There is no unique identifier or class specifically for the shop page, something like the page-id-?? class in the body tag (as in the usual WP pages).
Any suggestions on the best method/solution?
Set a conditional statement to check for the primary shop page, then echo the div in question if that statement evaluates to true.
WooCommerce Conditional Tag for main shop page:
is_shop()
Example
if (is_shop()) {
echo "<div class='custom-shop-class'></div>";
} else {
echo "<div class='custom-product-category-class'></div>";
}
Alternatively:
<?php if (is_shop()):?>
<div class="custom-shop-class"></div>
<?php else: ?>
<div class="custom-product-category-class"></div>
<?php endif;?>
Conditional Tags | WooThemes Documentation
Using the filter body_class with the is_shop conditional you can target the main WooCommerce shop page.
add_filter( 'body_class', 'woo_shop_class' );
// Add WooCommerce Shop Page CSS Class
function woo_shop_class( $classes ) {
if ( is_shop() ) // Set conditional
$classes[] = 'woo-shop'; // Add Class
return $classes;
}
The code goes in your themes functions.php file.
To build on the answer provided here, how might I go further and add a unique identifier to the shop page based on the active WPML language? (I'm basically trying to reduce a font size on the German version of the shop page only - it's proving surprisingly difficult to do)

how to call complete my custom php page without header and footer of wordpress with shortcode or a custom page

I am creating a Plugin, as that plugin activated a "custom post type" also created named "documentation" also a page named "Documentation" created.
Now I want that whenever "Documentation" page is viewed it should show completely my custom php page. I dont want wordpress header, footer etc.
I want this page completely in my hand.I tried with shortcode, I created shortcode and put it in page content but when this page is viewed it shows shortcode content with worpress theme layout (header, footer).
Following is my code.
function documentation_fun( $atts ) {
ob_start();
include 'documentation.php';
return ob_get_clean();
}
add_shortcode( '3pane-documentation', 'documentation_fun' );
I am creating this in my plugin.Please suggest me the best way to achieve my goal.
EDIT
I found something else as well, there is a filter for the content
function magicalendar_get_event_page( $content ) {
global $post;
if ($post->post_title == ' Documentation') {
$single_template = dirname( __FILE__ ) . '/documentation.php';
}
return $single_template;
}
add_filter( 'the_content', 'magicalendar_get_event_page' );
but this is just returning file path not the content of file (html). Also header and footer still coming which I dont want.
I found hook by myself :) it is add_filter('taxonomy_template');
Thanks!
If you want to remove header and footer then create your custom page template. its best option.
Create your template file.
<?php
/**
* Template Name: My Custom Page Template.
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*/
And in end select this template in page.
so it will not print header and footer.
It will only print header and footer when you call.
get_header();
get_sidebar();
get_footer();
if not then it will out put your content only with custom header and footer.

Resources