wordpress conditional statements - wordpress

I'm trying to get this logic to work in WordPress Widget Logic:
Widget displays if it's NOT the homepage OR NOT the page 86 OR NOT if it's a child of page 86.
!is_home || !is_page('86') || !is_child('86')
I've added this function to functions.php:
function is_child($parent) {
global $post;
return $post->post_parent == $parent;
}

try this:
(!is_home() && !is_page(86) && !is_child(86))
is_home() is a function. And to check the pages I suggest using a integer not a string.
And the overall ( ) is just a safety measure. Maybe you can leave them out, I'm not sure.
Not tested.

Related

How can I alter the WordPress query without "overriding" it?

I have a search results page that I want to limit results on i.e. posts_per_page. However if I use query_posts('posts_per_page=6') I lose the original query.
How do I alter my query without damaging the original?
You can use the pre_get_posts filter to access/alter the $query object. In functions.php:
function search_filter($query) {
if ( !is_admin() && $query->is_main_query() ) {
if ($query->is_search) {
$query->set('posts_per_page', 6);
}
}
}
add_action('pre_get_posts', 'search_filter');
Never ever use query_posts, it breaks the main query object ($wp_query) and all functionality that relies on the main query object. It also breaks page functionality. Apart from that, it is slow and reruns queries. query_posts should be on top of your most evil list together with functions like create_function(), eval() and extract().
If you need to alter the main query, always use pre_get_posts to do so. Never change the main query with a custom one, it might solve one problem, but it creates many other.
The following will work
add_action( 'pre_get_posts', function ( $q )
{
if ( !is_admin()
&& $q->is_main_query()
&& $q->is_search()
) {
$q->set( 'posts_per_page', 6 );
}
});

Wordpress: How can I hook only on certain pages? Can I even do that?

So I created a action add_action( 'header', 'admin_bar', 8 );, so that it loads in the header. However, I don't want it to load on certain pages, for example, post-new.php. Can I have a condition for hooks to load only certain pages?
I believe there is a $pagenow global. not sure if it's still there but you could var_dump($pagenow) on the pages you want to exclude to find the value then do something like this in your function...
function admin_bar() {
global $pagenow;
if ( 'post-new' != $pagenow || 'dashboard' != $pagenow ) {
//execute code
}
}

How to display disqus on home page

I have a problem with disqus plugin on wordpress. How to display disqus on home page. so the single page is to be a home page, maybe like that.
Any idea to solve it?
Thanks.
I am also unable to get disqus to work on homepage.
I can force the comments_template to appear by setting the following variable:
$withcomments = 1;
which makes the comments.php template appear but the discus plugin only kicks in if its on other pages other than home page.
Its as if the plugin itself prevents it if is_home() rather than listening to wp $withcomments variable
UPDATE
Can be fixed with plugin hack to disqus.php:
In function dsq_comments_template change the conditional if(!(is_singular() && ( have_comments() || 'open' == $post->comment_status ))
In mycase where I wanted it to work on home and an aggregate page for a custom taxonomy 'issue' I did the following:
after global $comments;
made a var for the more complex condition (it can go in the if instead)
$pass = (is_home() || is_taxonomy('issue')) || (is_singular() && ( have_comments() || 'open' == $post->comment_status ));
if(!$pass) {
return
}
... the rest of function ...
Be great if the developer made an option for this condition instead
not sure what you mean. Any comment plugin generally replace your current comment template and place their comment system. So make sure your comments_template(); in right place.
Please send details of your problem.
There's a Disqus tutorial with step-by-step instructions for a CMS. Does that help you with your installation?
I'm not sure what you mean by "not the disqus comment, just the standard comment", though. Can you explain?
I found this worked great, but the
if(!$pass) { return }
was breaking the page, I replaced
if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
return;
}
with
if ($pass = (is_home() || is_taxonomy('issue')) || (is_singular() && ( have_comments() || 'open' == $post->comment_status ))) {
}
leaving out the if(!pass){return}
not sure why

custom pages give 404 error title in Wordpress

I'm running a site powered by WordPress with extra pages...
To integrate these pages with the WordPress theme I use this code:
<?php
$blog_longd='Title'; // page title
define('WP_USE_THEMES', false);
require('wp-blog-header.php');
get_header();
?>
html code
<?php
get_sidebar();
get_footer();
?>
This works fine, however page title shows always 404 Error Page (not "Title").
It seems that $wp-query->is_404 is always set to true. I tried overriding this value but it doesn't seem to work. I tried fixing it by putting header status 200 above function get_header()..also it doesn't work.
Any suggestions?
Thanks
I know it has been a long time since you asked but I had the problem and here is the solution.
<?php
require('./wp-config.php');
$wp->init();
$wp->parse_request();
$wp->query_posts();
$wp->register_globals();
$wp->send_headers();
get_header();
echo "HELLO WORLD";
get_footer();
?>
Maybe clumsy, but if you implement the wp_title filter, you can change the title to what you want. You can add this code to the header of each custom page:
add_filter('wp_title', 'replace_title');
function replace_title() {
return 'My new title';
}
If you want it a bit cleaner, use a smarter version of this filter to a plugin, and set only the global variable (here $override_title) in your page:
add_filter('wp_title', 'replace_title_if_global');
function replace_title_if_global($title) {
global $override_title;
if ($override_title) {
return $override_title;
}
return $title;
}
There is code in the file class-wp.php:
function handle_404() {
...
// Don't 404 for these queries if they matched an object.
if ( ( is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object() ) {
status_header( 200 );
return;
}
...
}
that handles 404 status for various of pages.
The stack of functions of this code is:
1) wp-blog-header.php:14, require()
2) function.php:775, wp()
3) class-wp.php:525, WP->main()
4) class-wp.php:491, handle_404()
So you have two ways to handle the situation:
1)
require('wp-blog-header.php');
function status_header( 200 );
2) more correct would be insert your own function here
if ( your_own_function() || ((is_tag() || is_category() || is_tax() || is_author() || is_post_type_archive() ) && $wp_query->get_queried_object()) ) {
that returns true when your custom page is requested

Wordpress custom post type hierarchy and menu highlighting (current_page_parent)

I have created a custom post type of 'portfolio' and page with a template that retrieves all posts matching that custom post type.
The problem is when I drill down into the actual post, the post seems to sit under 'blog' in the main menu highlighting (displays current_page_parent as a class)
The permalink url is correct: www.site.com/portfolio/post-slug
But the menu thinks the parent is 'blog'.
This is obviously a hierarchical issue but I don't know what to do to fix it.
It appears this is an issue with the core Wordpress code; the code that generates the menu classes adds current_page_parent to your Blog page everywhere except when viewing static page templates.
(This has been discussed in passing at http://core.trac.wordpress.org/ticket/13543).
You can however get around this with some custom code using the page_css_class filter. For example, add something along these lines to functions.php (not 100% tested):
function my_page_css_class($css_class, $page) {
if (get_post_type()=='portfolio' || is_page(57)) {
if ($page->ID == get_option('page_for_posts')) {
foreach ($css_class as $k=>$v) {
if ($v=='current_page_parent') unset($css_class[$k]);
}
}
if ($page->ID==57) {
$css_class[]='current_page_parent';
}
}
return $css_class;
}
add_filter('page_css_class','my_page_css_class',10,2);
Replacing 57 with the ID of your portfolios page, of course. That removes current_page_parent when printing the blog page and adds current_page_parent to your portfolios page, when either viewing a single portfolio or viewing the portfolios page itself.
Here is my optimized/extended version of previously suggested solutions, which is pretty much fully automated. No more extra CSS or menu attributes needed.
This version dynamically gets a list of custom post types and if the current post type is a custom post type, then it removes the 'current_page_parent' class from all menu items.
Furthermore it checks each menu item to see if it's for a page with a page template like "page-{custom_post_type_slug}.php", and if so, it'll add the 'current_page_parent' class.
The filter priority is 1, as some themes, replace the current_page_parent/etc. classes with a class like 'active' (eg. 'roots' does this), so this filter needs to execute first.
Lastly, it makes use of 3 static variables since this function is repeatedly called and these (obviously) remain the same through all calls.
function theme_current_type_nav_class($css_class, $item) {
static $custom_post_types, $post_type, $filter_func;
if (empty($custom_post_types))
$custom_post_types = get_post_types(array('_builtin' => false));
if (empty($post_type))
$post_type = get_post_type();
if ('page' == $item->object && in_array($post_type, $custom_post_types)) {
$css_class = array_filter($css_class, function($el) {
return $el !== "current_page_parent";
});
$template = get_page_template_slug($item->object_id);
if (!empty($template) && preg_match("/^page(-[^-]+)*-$post_type/", $template) === 1)
array_push($css_class, 'current_page_parent');
}
return $css_class;
}
add_filter('nav_menu_css_class', 'theme_current_type_nav_class', 1, 2);
PS. Just to point out one shortcoming in all non-CSS solutions I've seen so far, including my own:
Something not taken into account is highlighting the menu item parent/ancestor of an item linking to a page which displays posts of the current custom post type. Consider a custom post type "product" and a menu like:
Home Company News Contact
|
\--About Us
\--Products
"Products" is a page with a template "page-product.php" and shows an overview of posts of type 'product'. It is highlighted due to posted solution. However 'Company' as its parent/ancestor should also be highlighted, but isn't. Something to keep in mind.
WP ticket: http://core.trac.wordpress.org/ticket/16382
function fix_blog_menu_css_class( $classes, $item ) {
if ( is_tax( 'my-cat-tax' ) || is_singular( 'my-post-type' ) || is_post_type_archive( 'my-post-type' ) ) {
if ( $item->object_id == get_option('page_for_posts') ) {
$key = array_search( 'current_page_parent', $classes );
if ( false !== $key )
unset( $classes[ $key ] );
}
}
return $classes;
}
add_filter( 'nav_menu_css_class', 'fix_blog_menu_css_class', 10, 2 );
I did some more looking around on this and found another way of doing this.
add_filter('nav_menu_css_class', 'current_type_nav_class', 10, 2);
function current_type_nav_class($css_class, $item)
{
if (get_post_type() === 'portfolio') {
$current_value = 'current_page_parent';
$css_class = array_filter($css_class, function ($element) use ($current_value) {
return ($element != $current_value);
});
}
$post_type = get_query_var('post_type');
if ($item->attr_title !== '' && $item->attr_title === $post_type) {
array_push($css_class, 'current_page_parent');
};
return $css_class;
}
I got some help form this post and then modified it to also remove the "current_page_parent" class from the blog page.
https://wordpress.stackexchange.com/questions/3014/highlighting-wp-nav-menu-ancestor-class-w-o-children-in-nav-structure/3034#3034
Cordially
Vayu
As explained at https://core.trac.wordpress.org/ticket/16382, .current_page_parent matches "anything that isn't a page" for the sake of backwards compatibility (bear in mind that this was considered backwards 10 years ago...) so themes nowadays really shouldn't still be using it.
So the simplest solution, and the most efficient (since unlike previous answers it doesn't require running extra code on every page load), is to modify your theme's CSS to replace use of the .current_page_parent class selector with .current-menu-parent, which does the right thing. (NB underscores vs hyphens.)
If you are using a third-party theme and don't want to modify it directly, then you can overwrite its properties in your own stylesheet. For example, if your theme has:
.current_page_parent > a {
border-bottom: 4px solid blue;
}
then in your child theme's stylesheet you would do this to cancel out its effects, and apply them to the correct class:
.current_page_parent > a {
border-bottom: transparent !important; /* Cancel out incorrect styling */
}
.current-menu-parent > a {
border-bottom: 4px solid blue; /* Add styling correctly */
}
This is just an example - the correct way will depend on how your theme is styling these links.
Here is a solution that worked for me, without having to define my custom post type or menu id or page id in the code:
http://dtbaker.net/web-development/how-to-stop-wordpress-automatically-highlighting-the-blog-page-in-the-menu/
function dtbaker_wp_nav_menu_objects($sorted_menu_items, $args){
// this is the code from nav-menu-template.php that we want to stop running
// so we try our best to "reverse" this code wp code in this filter.
/* if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id )
$classes[] = 'current_page_parent'; */
// check if the current page is really a blog post.
//print_r($wp_query);exit;
global $wp_query;
if(!empty($wp_query->queried_object_id)){
$current_page = get_post($wp_query->queried_object_id);
if($current_page && $current_page->post_type=='post'){
//yes!
}else{
$current_page = false;
}
}else{
$current_page = false;
}
$home_page_id = (int) get_option( 'page_for_posts' );
foreach($sorted_menu_items as $id => $menu_item){
if ( ! empty( $home_page_id ) && 'post_type' == $menu_item->type && empty( $wp_query->is_page ) && $home_page_id == $menu_item->object_id ){
if(!$current_page){
foreach($sorted_menu_items[$id]->classes as $classid=>$classname){
if($classname=='current_page_parent'){
unset($sorted_menu_items[$id]->classes[$classid]);
}
}
}
}
}
return $sorted_menu_items;
}
add_filter('wp_nav_menu_objects','dtbaker_wp_nav_menu_objects',10,2);

Resources