How to load different Bloginfo value for top level pages? - wordpress

I am no PHP programmer therefore I have been scratching my head for two hours on this issue as there seems to be many ways of doing it. None of which I managed to get working because of my poor PHP/Wordpress syntax and logic knowledge.
What is the best way to create and call a function that loads a different value for <?php bloginfo('name') ?> only on top level pages? i.e. top level menu headings.
I was wondering if there would be the possibility of doing something along these lines:
<?php
if ( is_page( 'about' ) || == $post->post_parent ) {
bloginfo = 'about';
} elseif ( is_page( 'work' ) || == $post->post_parent ) {
bloginfo = 'work';
} elseif ( is_page( 'contact' ) || == $post->post_parent ) {
bloginfo = 'contact';
} else {
bloginfo('name');
}
?>
Please treat it as pseudo code as I am still familiarizing myself with the wordpress and PHP codex/syntax, hence why I cannot get anything to work. But basically, what I need to do is use the main nav link names as the bloginfo name for every page in their section. Anything else is an exception which defaults back to the home bloginfo name.
Would it be easier to try to parse the nav link titles themselves into the function?
Please help if you can!
UPDATE
I have completely ditched the previous option and went the following route:
<?php if (is_page() || is_single( array( 62, 57, 51, 8 ) )) {
echo wp_title('');
}
else{
$category = get_the_category();
echo $category[0]->cat_name;
}?>
It is kind of botched but after reading lots of reference material on the wordpress site, I think I am beginning to understand the syntax. So, instead of targeting bloginfo() I am now targeting the page title. However I do not want the posts to have a huge title therefore I am making all pages other than top level "pages" pick up the first category instead, except for a handful of posts which are in fact custom pages, so need to display the wp_title value.
If anyone has any tips on turning this into a more flexible/efficient function please drop a post otherwise I will make this as resolved in a couple of hours.

the syntax would be
<?php
if ( is_page( 'about' ) || == $post->post_parent ) {
bloginfo('about');
} elseif ( is_page( 'work' ) || == $post->post_parent ) {
bloginfo('work');
} elseif ( is_page( 'contact' ) || == $post->post_parent ) {
bloginfo('contact');
} else {
bloginfo('name');
}
?>
but there are no such parameters like about,work,contact to the bloginfo function in wordpress.
For more information on bloginfo, please check the this url -> http://codex.wordpress.org/Function_Reference/bloginfo

You may try this to filter/change the name using bloginfo filter (just add this code snippet in your functions.php file)
function my_custom_blogname( $output, $show )
{
if( $show == 'name' )
{
if( is_page('about') ) $output = 'It\'s my about page';
if( is_page('work') ) $output = 'It\'s my work page';
if( is_page('contact') ) $output = 'It\'s my contact page';
}
return $output;
}
add_filter('bloginfo', 'my_custom_blogname', 10, 2);
Update : Also you can check if the page is parent/top lavel page using
if( !$post->post_parent ) // To check only if it's a parent/top lavel page
if( is_page('about') && !$post->post_parent ) $output = 'about'; // To check if it's a parent/top lavel page and about page

Related

Add custom product option to woocommerce orders list

I am using the "custom product options" plugin and created a datepicker for customers to choose a requested ship date. I need to create a column on my orders admin page for their selection so I do not have to go into the order to find the date. I have found a few different threads that are similar but they don't really apply to my exact situation. Would someone be able to provide some help on this? Apologies for a lack of detail that is getting me downvotes, but If I knew what I needed I wouldn't be here asking what to do. I am a novice, cut me a little slack. Datepicker can be seen here.
https://chrish148.sg-host.com/product/butterscotch-oatmeal-copy/
current layout looks like this.
current layout image
This is the code that I used for one of my WooCommerce sites to get order-colors:
add_filter( 'manage_edit-shop_order_columns','shopOrder',10 );
function shopOrder($columns)
{
$columns['custom_color_column'] = "Färger";
return $columns;
}
add_action( 'manage_shop_order_posts_custom_column' , 'populateShopOrder' );
function populateShopOrder( $column ) {
global $the_order, $post;
if( $column == 'custom_color_column' ) {
global $post, $the_order;
if ( empty( $the_order ) || $the_order->get_id() !== $post->ID ) {
$the_order = wc_get_order( $post->ID );
}
if ( empty( $the_order ) ) {
return;
}
$items = $the_order->get_items();
foreach ($items as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
if($value != null && $attribute['key'] == "pa_farg"){
echo "- " . $value;
if(count($items) > 1) echo "<br>";
}
else return;
}
}
}
}
However this might not be the exakt case for you. Since I do not know the exact plugin that you are using I can not know for sure.
However it should be close.
If you can figure out the attribute names then you could switch it from "fa_farg" to that, or you could link the plugin thet you are using and I will take a look at it.

Trying to load separate headers for home, page, post, and category - only category doesn't work

Since I have different Google Ad Manager codes that need to load in the of different content types, I created separate headers for each type and put this code snippet in index.php to load them accordingly:
if ( is_front_page() ) {
get_header('home') ;
}
elseif ( !is_front_page() AND is_page() ) {
get_header('page') ;
}
elseif ( is_single() ) {
get_header('article') ;
}
elseif ( is_category() ) {
get_header('category') ;
}
else {
get_header() ;
}
All of them work great - except for the category. It isn't loading. It just loads the default header.
What am I doing wrong?
Any help is much appreciated!
The short answer would be to try and use is_archive() instead of is_category(). If that doesn't work, you may need to dump your global $wp_query variable and see what query types are being returned on that particular url.
In that same vein, you may consider using is_singular() instead of is_single(), because is_single is limited to Posts.
One more point to consider is using && instead of AND (they're synonymous other than precedence. Take a look at this answer for a bit more in-depth on it)
Lastly, if you're only loading in a separate Google Ad Manager code, I'm not sure you need to maintain x number of header files. Have you considered dropping in the script codes into your functions.php file and loading them on the wp_enqueue_scripts or wp_head hooks?
instead of maintaining separate headers like this:
if( is_front_page() ){
get_header( 'home' );
} else if( is_page() ){
get_header( 'page' );
} else if( is_singular() ){
get_header( 'article' );
} else if( is_archive() ){
get_header( 'category' );
} else {
get_header();
}
You could drop those scripts straight in based on the same is_ functions using something like this:
add_action( 'wp_head', 'load_ad_manager_scripts' );
function load_ad_manager_scripts(){
if( is_front_page() ){
echo '<script>// Home Ad Manage Code</script>';
} else if( is_page() ){
echo '<script>// Page Ad Manage Code</script>';
} else if( is_singular() ){
echo '<script>// Single Manage Code</script>';
} else if( is_archive() ){
echo '<script>// Archive Manage Code</script>';
} else {
echo '<script>// Generic Manage Code</script>';
}
}

How to check WooCommerce thank you page

In Wordpressis_page() can check page by ID,name or by slug but how can we check WooCommerce thank you page whether its a part of checkout page.
Also We have a lot of WooCommerce conditional tags but cant find something solve my issue
for example I try
if(is_page('checkout')) {
//some thing for only checkout page
}else if(is_page('thankyou') && !is_page('checkout')){
//some thing for only thank you page but not on checkout page
}else{
//some thing for all other page
}
This sample code may work:
if ( is_checkout() && !empty( is_wc_endpoint_url('order-received') ) ) {
...
}
I think better to use endpoint like
if ( is_wc_endpoint_url( 'order-received' ) ) {
global $wp;
//Get Order ID
$current_order_id = intval( str_replace( 'checkout/order-received/', '', $wp->request ) );
echo $current_order_id;
}

WP - WooCo. - Compare - Button

I am using Wordpress for my online shop.
I have a problem. On the page with the product, I added a button for comparing the products. All is well but I want that after pressing the button, it becomes invisible.
<?php
$session_data = $be_product_compare->session->get_session_cookie();
if( is_array( $session_data ) && in_array( get_the_ID(), $session_data ) )
{ echo BE_Compare_Products::display_button( ''.get_the_ID() . '' );
}
else
{
echo ' disabled=disabled ';
}
?>
And I get
Uncaught Error: Call to a member function get_session_cookie() on null
in
Can someone help me finish with this Button?

Wordpress next_posts_link Linking to wrong URL

I have next_posts_link setup on the single.php, and it generates the following URL:
http://mywebsite.com/news/article1/page/2
However, this url would be redirected to
http://mywebsite.com/news/article1
Any way to get to the second page?
It seems that it's an issue with Wordpress permalinks. I currently use a custom permalink structure
/%category%/%postname%/
Setting the permalinks as default fixes this issue, but this project needs to have the custom permalinks.
You should be using next_post_link() which is meant to navigate between single posts. next_posts_link naviugate between pages
If you however need to navigate a paged post (using <--nextpage-->), then you should make use of wp_link_pages
EDIT
I have recently did the same exact thing on WPSE. As I explain in that post, the structure you need is not available for any permalink structure outside the default permalink structure.
Just a note before I paste that answer, I have done that for the /%postname%/ permalink structure. Just change all instances of /%postname%/ to the appropriate structure
POST FROM WPSE
As I said, this whole setup you are after is not possible natively with pretty permalinks. Your setup probably works with default permalink structure as both queries (the main query and your custom query) read these permalinks in the same way. When you switch to pretty permalinks, the two queries on the single page interpret the URL differently causing one or the other to fail when you try to paginate your custom query
Single pages was never meant to be paginated in this manner, specially using pretty permalinks. I have gone and played around with a couple of ideas, and the best way to accomplish this is
To write your own pagination functions that can read the page number from the URL
Write your own function that can append the page number to the URL, something like adding /2/ to the URL of single pages.
I must stress, if you are paginating single post with <!--nextpage-->, your post will also paginate together with your custom query. Also, if your permalink structure is not set to /%postname%/, the code will fail and display an error message through wp_die()
THE CODE
Here is the code that will get the next/previous page and also add the pagenumber to the URL.
function get_single_pagination_link( $pagenum = 1 ) {
global $wp_rewrite;
if( is_singular() && $wp_rewrite->permalink_structure == '/%postname%/') {
$pagenum = (int) $pagenum;
$post_id = get_queried_object_id();
$request = get_permalink( $post_id );
if ( $pagenum > 1 ) {
$request = trailingslashit( $request ) . user_trailingslashit( $pagenum );
}
return esc_url( $request );
}else{
wp_die( '<strong>The function get_single_pagination_link() requires that your permalinks are set to /%postname%/</strong>' );
}
}
You can use this function as follow to get the link for any page in the single page when paginating your custom query
get_single_pagination_link( 'pagenumber_of_previous_or_next_page' );
Again, as I said, there is no pagination function that will be able to paginate your custom query or read pagenumbers from the URL, so you have to write your own.
Here is my idea of creating links to the next and previous pages in your custom query. If you look closely, you will see how I have used the previous declared function get_single_pagination_link() to get the links to the next and previous pages
function get_next_single_page_link ( $label = null, $max_page = 0 ) {
global $wp_query;
if ( !$max_page ) {
$max_page = $wp_query->max_num_pages;
}
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
if( is_singular() ) {
$next_page = intval($paged) + 1;
if ( null === $label ) {
$label = __( 'Next Page »' );
}
if ( ( $next_page <= $max_page ) ) {
return '' . $label . '';
}
}
}
function get_previous_single_page_link( $label = null ) {
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
if( is_singular() ) {
$prev_page = intval($paged) - 1;
if ( null === $label ) {
$label = __( '« Previous Page' );
}
if ( ( $prev_page > 0 ) ) {
return '' . $label . '';
}
}
}
You can now use this two functions in your code to paginate your custom query. Both functions work exactly the same as get_next_posts_link() and get_previous_posts_link() and uses the same exact parameters, so you'll need to keep in mind that you need to pass the $max_page parameter for your custom query
Here is your custom query with these functions.
function get_related_author_posts() {
global $authordata, $post;
$paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
$args = array(
'posts_per_page' => 2,
'paged' => $paged
);
$authors_posts = new WP_Query( $args );
$output = '';
if( $authors_posts->have_posts() ) {
$output = '<ul>';
while( $authors_posts->have_posts() ) {
$authors_posts->the_post();
$output .= '<li>' . get_the_title() . '' . get_the_excerpt() . '</li>';
}
$output .= '</ul>';
$output .= get_previous_single_page_link();
$output .= get_next_single_page_link( null , $authors_posts->max_num_pages );
wp_reset_postdata();
}
return $output;
}
If you want your long post to be shown 2 or 3 pages . You just have to add <!--nextpage--> in your post . The content after it will be shown in the next page . Reference

Resources