Wordpress How to Check whether it is POST or PAGE - wordpress

How to check if an article is a post or a page in WordPress?

You can use the is_page() and is_single() functions.

You can also use get_post_type() function.
if (get_post_type() === 'post') {
// POST
}
if (get_post_type() === 'page') {
// PAGE
}

If you're looping through a collection of posts/pages (say, on a search results page), then is_single() and is_page() won't be of any use. In this situation, you could grab the global $post object (of type WP_Post) and examine the $post->post_type property. Possible values include 'post' and 'page'.

is_singular() returns true for a single post, page or attachment

You mean that is_single() will return true if it is a post ? (not a page), am I right,
I like that, I think you wrong, because I have a plugin show some text on only post, I'm using is_single() but It also show on pages.
Please advice.
Thanks

if you want y¡to know the page that list the posts , and you are using the posts page option in the configuration, You should use is_home().

It's for developer, if you are not a developer you can also check current page type. You have to just inspect particular page and see body tag. If theme is build with basic WordPress rules then body tag have classes related to page or single page. These classes may b included the post type, template name, file name, page id and many more.

Related

Change the default global edit link that lists my CustomPostType (edit.php?post_type=CPT), to use a plugin page link instead

I'm working on a plugin (with a https://wppb.me/ plugin boilerplate base, if relevant).
I defined my custom post type, with the editor options I want, that I can edit with the default /wp-admin/post.php?post={id}&action=edit
The listing of those CPT is done on my plugin pages only (no specific menu for the CPT) and I want to keep it that way.
Problem:
I'd like to change the base edit link of this post type when no ID is specified
( eg : /wp-admin/edit.php?post_type=CPT ) to be the URL of my plugin
( eg : /wp-admin/admin.php?page=myplugin )
This, mostly because in Gutenberg Editor Fullscreen, the top left wordpress logo links to the /wp-admin/edit.php?post_type=CPT that I don't wont to use nor show. I'd like this link to be a page of my plugin (here, it would be the homepage of my plugin)
I tried with a filter on get_edit_post_link when there is no ID provided, but it doesn't seem to be the correct way to fix my problem.
Any tips or help to get me in the right direction in my research are welcome !
I found one of the possible solutions. After entering the site, you must redirect the user.
function wp_custom_update_meta_cache_filter() {
global $pagenow, $typenow;
if ( $pagenow == 'edit.php' && $typenow == 'CPT' ) {
wp_safe_redirect();
die();
}
}
add_action( 'admin_init', 'wp_custom_update_meta_cache_filter' );
Based in (233 line):
https://github.com/dipolukarov/wordpress/blob/master/wp-content/plugins/woocommerce/admin/woocommerce-admin-init.php

How to determine the "post type" of a "page"

I've created a Wordpress template for a site and the site uses an events plugin. My template has a custom heading so in the header it displays different text according to the post-type.
I'm having difficulty with the plugin (The Events Calendar) however it isn't a plugin issue. I'm unable to determine if it is a "tribe_events" post type becuase the plugin generates a page so when I use the get_post_type() function, it returns that it is a page. How do you get the underlying post type of a page? Is it possible without having to write a direct MySQL or $wpdb query?
I know it's not the most efficient way to do this but I've resorted to using $wp_query->post and then using the available fields from the post.
global $wp_query;
$postObj = $wp_query->post;
Now I can use any of the available fields from $postObj
You can use global $post object to get post type.
global $post;
$post_type = $post->post_type;
var_dump($post); to check content of the object.

Customize search results for Custom Post Types

I am writing a Wordpress plug-in that creates several Custom Post Types (CPT). For they have their own custom fields, that need to be displayed in the search results, I need to customize the search results output.
Do I need to write my own theme for that or is there a hook (or other way) to solve this in my plug-in code?
You could hook into get_the_content and get_the_excerpt filters and test with is_search() to see if you should alter the returned value or not.
Not tested, but this is the idea:
add_filter( 'get_the_excerpt', 'my_search_excerpt' );
add_filter( 'get_the_content', 'my_search_excerpt' );
function my_search_excerpt( $content ) {
if ( is_search() ) {
$content = 'This is a search excerpt for ' . get_the_title();
// maybe add a read more link
// also, you can use global $post to access the current search result
}
return $content;
}
I see four possibilities:
Create a new (child) theme
Override the search template using filters (template_include)
Use client-side code to modify the appearance (CSS / JavaScript, poor workaround)
Hook the_content or the_excerpt
The easiest way might be to copy the search.php file of your installed theme and modify it to fulfill your needs. You can then hook it in using the first or second way. The first requires you to create a child theme, the second to create a plugin. The latter might be more complex so I would suggest to create a theme (take a look at template files of child themes for an explanation).

Seo yoast plugin, breadcrumbs

This is question on the seo yoast wordporess plugin.
I have a friend who doesn't know that much about code/wordpress/etc, they have an ecommerce site built using magento and a blog using wordpress which is styled to match the main site and they use yoast seo plugin for seo etc. They have asked me to try an get the breadcrumbs working for them in a different way to what i know, from what i can see they want to add a prefix to the breadcrumbs 'home' which links to main site then renaming the blog from home to blog, for example home(main site) > blog(blog) > post.
Now i did just that in the plugin settings but they said that the schema markup wouldn't be complete and said there’s a filter hook called ‘wpseo_breadcrumb_links’ which gives access to the array of URLs and anchor text used to build the breadcrumbs, now i cant find anything on google that explains this or how to start writing it, i do know that it needs to go in the functions file.
Would it be possible to get some help on this.
Thanks in advance and very much appreciated.
Justin
I'm not sure what Schema.org markup has to do with breadcrumbs! :) Schema is used on post/page level. So, they are either oversmarting themselves or I got you wrong.
I think this sample code may be helpful:
add_filter( 'wpseo_breadcrumb_output', 'custom_wpseo_breadcrumb_output' );
function custom_wpseo_breadcrumb_output( $output ){
if( is_product() ){
$from = '<span typeof="v:Breadcrumb">Products</span> »';
$to = '';
$output = str_replace( $from, $to, $output );
}
return $output;
}
It looks like wpseo_breadcrumb_output gives you access to the entire output instead of just the link portion. Please see this page for more details: http://wpquestions.com/question/showChrono/id/8603

How to map an arbitrary URL to a function in a Wordpress plugin

I'm trying to create a Wordpress plugin that redirects visitors of example.com/redirect/XXX to a different page based on the value of XXX. I think I know how to do the redirect logic, but I don't know how to make sure that my Wordpress plugin function will be called when a visitor goes to example.com/redirect. Right now I just get a 404. There are other solutions that involved changing the .htaccess file, but I want this to function as a standalone plugin. Thanks!
When I need this kind of things i just create a common page with template as the "plugin", a page with all the functions that I need.
For example, if i need a shopping cart, I just create a cart.php as:
<?php
/*
Template Name: Cart
*/
// functions here
?>
And I go to my wp-admin and create a page with Cart as its template.
Depending on what exactly you want to do which is quite vague ( when you say page you actually mean page ? post ? cpt ? and when you say plugin functions - what are they ? and do you use permalinks ?)
.. but under some conditions you could use wp conditionals .
example ( from codex )
is_singular( 'foo' )
// Returns true if the post_type is "foo". execute plugin hook
is_singular( array( 'foo', 'bar', 'baz' ) )
// Returns true if the post_type is "foo", "bar", or "baz".
// See also the Custom Post Types book.
or if you aim at filtering you can always hook to pre_get_post with is_main_query() or any other conditional //

Resources