Wordpress archive pages url change - wordpress

I have my blog archive to show the months and i can access them by going at
http://www.mysite.com/date/2014/02
Now, i want to change these links to something like
http://www.mysite.com/blog/date/2014/02
without changing the permalinks admin panel setting.
Is it possible to achieve this by coding it?

An idea of what you want can be found here and here.
Install WP Router, and what you will have at end will be something like this:
add_action( 'wp_router_generate_routes', 'bl_add_routes', 20 );
function bl_add_routes( $router ) {
$route_args = array(
'path' => '^blog',
'query_vars' => array( ),
'page_callback' => 'bl_new_demo_route_callback',
'page_arguments' => array( ),
'access_callback' => true,
'title' => __( 'Blog/Date' ),
'template' => array(
'page.php',
dirname( __FILE__ ) . '/page.php'
)
);
$router->add_route( 'demo-route-id', $route_args );
}
function bl_new_demo_route_callback( ) {
return "Congrats!";
}
Here is another reading which is more straight forward.

Related

Visual composer custom loop shortcode

I need to use custom loop from visual composer:
if( function_exists('vc_map') ) {
vc_map( array(
'base' => 'minimag_popular_post_custom',
'name' => esc_html__( 'Popular Post Custom', "minimag-toolkit" ),
'class' => '',
"category" => esc_html__("Minimag Theme", "minimag-toolkit"),
'params' => array(
array(
// this param
"type" => "loop",
"heading" => esc_html__("Display Custom Loop", "minimag-toolkit"),
"param_name" => "custom_loop",
)
),
) );
}
In past I've used vc_link which had the proper function to retrieve the value in the correct form: vc_build_link($href).
There is some function to extract the data from loop parameter? I've looked in the reference but I've not find nothing.
Here an example of the output that I need to parse:
size:8|order_by:date|order:DESC|post_type:post|categories:32,5|by_id:1537,1673
I need to have something like:
$myVar['size'] = 8;
$myVar['order_by'] = 'date';
$myVar['order'] = 'DESC';
$myVar['post_type'] = 'post';
$myVar['categories'] = array(32,5);
$myVar['by_id'] = array(1537,1673);
tested and working :)
list($args, $wp_query) = vc_build_loop_query($atts["custom_loop"]);
while ( $wp_query->have_posts() ) {
$wp_query->the_post();
}
wp_reset_postdata();
If you know your query I like to create a shotcode in the functions.php of my child-theme to create that. You can pass parameters to create different output and you can use such a shortcode everywhere in your site.

Remove title & add new button from a custom post type - wordpress

Need a solution to remove the "Title" and "Add new" button from a post type without jQuery.
You can disable the add new capabilities while passing the parameter in the register post type.
The parameter is :
create_posts' => false
Assuming you have the code like below :
$args = array(
'label' => __( 'Custom Post Type', 'text_domain' ),
'capabilities' => array(
'create_posts' => false
)
);
register_post_type( 'custom_post_type', $args );
In order to remove the title I'm not sure if that is possible,
one solutions would be hiding using css
Best way to remove the title is to use the function remove_post_type_support().
remove_post_type_support('custom_post_type', 'title');
You can add this to your functions.php file by calling it via the init hook.
add_action( 'init', 'remove_custom_post_support' );
function remove_custom_post_support() {
remove_post_type_support( 'custom_post_type', 'title' );
}

How to implement styling in page?

I am making custom h1 styling menu for customize panel in wordpress site but I don't know how to include the custom h1 styles in html structure of page.
$wp_customize->add_section( 'h1_styles' , array(
'title' => __('H1 Styles','wptthemecustomizer'),
'panel' => 'design_settings',
'priority' => 100
) );
$wp_customize->add_setting(
'wpt_h1_color',
array(
'default' => '#222222',
'transport' => 'postMessage'
)
);
$wp_customize->add_control(
new WP_Customize_Color_Control(
$wp_customize,
'custom_h1_color',
array(
'label' => __( 'Color', 'wptthemecustomizer' ),
'section' => 'h1_styles',
'settings' => 'wpt_h1_color'
)
)
);
I know I have to do something like this :-
if( get_theme_mod( 'wpt_h1_color') != "" ):
/* code */
endif;
But I want to know should I apply styles directly in html in head section with the above code or It can be done through function in functions.php.
Thanks for your help !
You can do it directly in html outputing <style></style> and some css in there (which is faster than loading external files, and Google Speed Insights maybe not recommends, but approves that way). You can also wp_enqueue_style() if you want to.

How do you target a specific page in Wordpress functions.php?

I am currently using the Multi Post Thumbnails plugin for Wordpress, but I only want the extra thumbnails provided by the plugin to show on one specific page. The plugin does not appear to natively support this functionality but it seems like something that would be pretty easy to add, I'm just not sure of the right way to go about it as I'm fairly new to Wordpress development.
The code for Multi Post Thumbnails is the following, which simply goes in functions.php:
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
It seems to me it would just be a simple case of wrapping this in a check so that it only runs for a specific page ID, but I'm not quite sure how to go about doing that.
This is probably somewhat of a hack. To my knowledge post/page id's are not accessible from inside functions.php.
// get the id of the post/page based on the request uri.
$url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$post_id = url_to_postid($url);
// the id of the specific page/post.
$specific_post_id = 3;
// check if the requested post id is identical to the specific post id.
if ($post_id == $specific_post_id) {
if (class_exists('MultiPostThumbnails')) {
new MultiPostThumbnails(
array(
'label' => 'Secondary Image',
'id' => 'secondary-image',
'post_type' => 'page'
)
);
new MultiPostThumbnails(
array(
'label' => 'Tertiary Image',
'id' => 'tertiary-image',
'post_type' => 'page'
)
);
}
}
This is also probably a hack but it worked for me. I got stung by the AJAX 'post_id' back to the admin page once the image has been selected. My usage was for a slug but the function could easily be modified for a post ID.
function is_admin_edit_page( $slug ){
if( ( isset($_GET) && isset($_GET['post']) ) || ( isset($_POST) && isset($_POST['post_id']) ) )
{
$post_id = 0;
if(isset($_GET) && isset($_GET['post']))
{
$post_id = $_GET['post'];
}
else if(isset($_POST) && isset($_POST['post_id']))
{
$post_id = $_POST['post_id'];
}
if($post_id != 0)
{
$c_post = get_post($post_id);
if( $c_post->post_name == $slug )
{
return true;
}
}
}
return false;
}
if( is_admin_edit_page('work') ) {
new MultiPostThumbnails(
array(
'label' => 'Hero 1 (2048px x 756px JPEG)',
'id' => 'am-hero-1',
'post_type' => 'page'
)
);
}

WordPress widget area disapper

I am working on a WordPress site. While adding some widget in widget section, I got some issue.
I add some thing in primary widget area and right widget area, and when I refresh the widget page my added content got disappear although it is displaying in the front-end of WordPress,but now I am not able to edit that content from admin section.
My code is as follows:
add_filter( 'sidebars_widgets', 'disable_sidebar_L_widgets' );
function disable_sidebar_L_widgets( $sidebars_widgets ) {
if (!is_front_page() ) $sidebars_widgets['primary-widget-area'] = false;
return $sidebars_widgets; }
add_filter( 'sidebars_widgets', 'disable_sidebar_R_widgets' );
function disable_sidebar_R_widgets( $sidebars_widgets ) {
if (!is_front_page() ) $sidebars_widgets['right-widget-area'] = false;
return $sidebars_widgets;
}
Edit functions.php and locate for register_sidebar function. Change the id to small caps example :
register_sidebar( array(
'name' => __( 'Main Sidebar', ),
'id' => 'sidebar-1',
'before_widget' => '',
'after_widget' => "",
'before_title' => '',
'after_title' => '',
) );

Resources