Wordpress Themes - WP Forum at BFA

Wordpress Themes - WP Forum at BFA (http://forum.bytesforall.com/index.php)
-   Header configuration & styling (http://forum.bytesforall.com/forumdisplay.php?f=15)
-   -   Change Title Tag based on parameter? (http://forum.bytesforall.com/showthread.php?t=23546)

Ob1knorrb Sep 8, 2016 08:00 PM

Change Title Tag based on parameter?
 
By default the title tag, which appears in the tabs in the browsers, gets set the Page Title and blog title, such as "Product_Page_test << ICEWEBRING". This is fine for most of my pages except for one.

In this one I change the content based on a parameter that is sent in (product_id), and I would like to change the Title Tag to be set to the second parameter (product_name)

Here is an example of this:

http://www.icewebring.com/ice-produc...ern%20Mirkwood

Before I moved my code to be within Wordpress, I was able to just include this in my php code:
echo "<title>$product_name</title>"

However, now that I've got the code within a Wordpress page, this no longer works, I think because I'm already out of the <head> area and into <body>.

I tried doing some Googling but didn't find a solution to this particular set of circumstances. The closest ones I found involved doing edits to header.php, which I'd like to avoid if possible.

Any suggestions on how to deal with this would be appreciated. Thanks!

juggledad Sep 9, 2016 05:50 AM

Why not just name the page the product name? i.e. instead of calling the page 'Product_Page_test' call it 'Southern Mirkwood'?

Did you think about creating each product as a post instead of a page? That way each of the items
HTML Code:

Product Line: MERP
Product Edition: M1
Product Name: Southern Mirkwood
Product Type: Campaign Setting
Author: Susan Tyler Hitchcock
Stock #: 2700
ISBN: 0-915795-09-4
Publisher: ICE
Page Count: 60
Release Date: 1983
Language: English

could be a category allowing easy searching

or you could use a plugin like woocommerce.

Ob1knorrb Sep 9, 2016 06:36 AM

The information about each book is in a database, there is a list page for each type of book (i.e. MERP, Rolemaster, Spacemaster) which reads the database for summary information. That page creates the links to the more detailed information pages for the individual books. It sends that page (now Product_Page) the product_id and product_title (was product_name).
Product_Page then uses product_id to load the rest of the information from the database and display the book. So it could be Southern Mirkwood, or it could be Northern Mirkwood, or any one of the other 600+ items in the database

http://www.icewebring.com/ice-produc...20Witch%20King

http://www.icewebring.com/ice-produc...tle=Arms%20Law

juggledad Sep 9, 2016 07:06 AM

You may have set yourself a bit of a chore. I think the cleanest way for you to do this (depending on how you access your database) would be to create a custom plugin that hooks into wp-title() and based on the actual page you are on, substitutes the product name for the page name.

This way you are not changing any WP code (a really bad idea) and not changing the theme code and - if you decide to - if you change themes, the plugin should still work.

Or you could create a child theme and add code to function.php

Ob1knorrb Sep 9, 2016 07:27 AM

Thanks, that's what I was afraid of :)
At this point it's more of a "wish list" item rather than a "must have" so I'm going to file it as a to do list item.
The advantages of pulling that code into a Wordpress page with the Atahualpa theme definitely out weigh those of keeping it outside of Wordpress but trying to pull in the same header and menu items.

Ob1knorrb Sep 9, 2016 12:34 PM

Is anyone aware of an existing plugin that might accomplish this or something similiar that I could look at as an example?
It looks like Yoast SEO might, but it also looks like it would be overkill for what I'm wanting to accomplish.

Ob1knorrb Sep 10, 2016 07:17 PM

It turned out to be less difficult than I thought it might. Tacked some extra code into the Parameter.php addon that I previously bastardized to get the variables working.
I did need to turn off “Use Bytes for All SEO options” in “Configure SEO” in the “Atahualpa Theme Options”.
Here is the code I ended up with for the plugin in case anyone is interested, haven't adjusted the original author's information yet

<?php
/* Plugin Name: Parameter
Plugin URI: http://webopius.com/
Description: A plugin to allow parameters to be passed in the URL and recognized by WordPress
Author: Adam Boyse
Version: 1.0
Author URI: http://www.webopius.com/
*/
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'product_id';
$qvars[] = 'product_title';
return $qvars;
}

/*
* Uses product_title as page title if it exists
*/
function wpdocs_filter_wp_title( $title, $sep ) {

global $wp_query;
if (isset($wp_query->query_vars['product_title']))
{
$title=$wp_query->query_vars['product_title'];
}
return $title;
}
add_filter( 'wp_title', 'wpdocs_filter_wp_title', 10, 2 );
?>


All times are GMT -6. The time now is 02:42 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.