Advertisement:

Author Topic: Thanks, and also Meta Title And Description  (Read 163 times)

Rupert

  • Newbie
  • *
  • Posts: 15
Thanks, and also Meta Title And Description
« on: July 18, 2019, 09:07:48 pm »
First of all, I'd like to give a huge thanks to the members of this forum. After 16 years of using GeodesicSolutions software, I recently had to switch to Osclass. The two software programs are nothing alike and the change has been a tough one for me. The help I've received in this forum has been much appreciated.

And now for a question (using Bender Theme and SEO Pro plugin)......

In order to set my meta titles and descriptions for each category, here's what I've noticed:

- The <title> for each category is picked up from the SEO Pro plugin.

- The <description> for each category is picked up from the category description in the admin area. When I put a description in the SEO Pro plugin, it seems to be ignored whether or not there is a description in the category admin area.

- Thanks to this handy piece of code: <p><?php echo osc_category_description(); ?></p> (thanks, BritWeb!) placed in search.php, the category description from the admin area is also placed on the category' page.


So, here's my question/goal:

I'd like to have the category description from the admin area show up on the category's page. That's working great thanks to the small piece of code I referred to above.

However, I would also like to have a meta <description> that is independent of the category description from the admin area. Is that possible?

And, does anyone know why the description from the SEO Pro plugin isn't being used regardless of whether or not there's a category description in the admin area?

I'm being picky about the whole description thing because I had independent meta and page descriptions in the old Geo software I was using, and could get several pages to rank very high in the search engines. So, I'd like to imitate what I was doing before if that's possible. I have a feeling it might be if I could get the description from the SEO Pro plugin to work, but like I said earlier I can't get the SEO Pro plugin descriptions to show up anywhere.

Thank you.

PS

Since I've referred to the old Geo software I was using, here is how what I'm trying to do was accomplished in that software. The code isn't exact because they used "smarty templates," but I think it gives the right idea. I doubt it's useful......but just in case here it is.


For meta tags, this was in the <head>:

{if category is category 1}
<title>title for category 1</title>
<meta name="Description" content="description for category 1" />

{elseif category is category 2}
<title>title for category 2</title>
<meta name="Description" content="description for category 2" />

{elseif category is category 3}
<title>title for category 2<</title>
<meta name="Description" content="description for category 3" />

{else}
<title>hello world</title>
<meta name="description" content="a description>
{/if}



and this was in the <body>:

<p>{if category is category 1}
A description for category 1.

<p>{elseif category is category 2}
A description for category 2.

{elseif category is category 3}
A description for category 3.

{else}
{/if}</p>
« Last Edit: July 19, 2019, 12:11:42 am by Rupert »

BritWeb

  • Hero Member
  • *****
  • Posts: 770
  • If it ain't broke, don't fix it.
Re: Thanks, and also Meta Title And Description
« Reply #1 on: July 19, 2019, 01:06:28 am »
Hi

Open the attached text file -> copy the code and paste it just below the following lines of code on your 'head.php'

Code: [Select]
<meta name="description" content="<?php echo osc_esc_html(meta_description()); ?>" />
<?php ?>

Hope this is what you wanted to achieve!


Regards

Rupert

  • Newbie
  • *
  • Posts: 15
Re: Thanks, and also Meta Title And Description
« Reply #2 on: July 19, 2019, 01:37:38 am »
Hi,

Thanks for the reply. I copied the code you gave me into head.php, but nothing changed. Nothing bad happened, but everything stayed the same. The description from the category admin area is still shown in the <description> meta tag, and also on the category page. This is the same behavior as before, without the code I put into head.php.

I tried to look closely at the code...was there something I was supposed to modify for each category?

Thank you.

BritWeb

  • Hero Member
  • *****
  • Posts: 770
  • If it ain't broke, don't fix it.
Re: Thanks, and also Meta Title And Description
« Reply #3 on: July 19, 2019, 11:13:42 am »
Hi,

Thanks for the reply. I copied the code you gave me into head.php, but nothing changed. Nothing bad happened, but everything stayed the same. The description from the category admin area is still shown in the <description> meta tag, and also on the category page. This is the same behavior as before, without the code I put into head.php.

I tried to look closely at the code...was there something I was supposed to modify for each category?

Thank you.

Hi

I think the code works as you have specified, the category name becomes the title and the corresponding category description goes as meta description as shown below and you can check it by viewing ‘page source’ on your browser.

<title>category name</title>
<meta name="Description" content=" corresponding category description" />

By applying this code you will not see any change showing on the pages. Not sure what other way you wanted it to work.

Again, would it be advisable to have more than one title and meta description for your site?


Best of luck!



WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Thanks, and also Meta Title And Description
« Reply #4 on: July 19, 2019, 02:05:21 pm »
Hello!

Here's how I would do it.

If you want to use category name as meta title and category description as meta description, add this to your functions.php.

Code: [Select]
function wm_meta_title_category($title) {
    if (osc_is_search_page()) { // If we are on search page...
        $category = osc_search_category_id(); // Get current category.
        if (array_key_exists(0, $category)) { // If current category isn't empty...
            View::newInstance()->_exportVariableToView('category', Category::newInstance()->findByPrimaryKey($category[0]));
            $title = osc_category_name(osc_current_user_locale()); // Meta title is now category title.
         }
    }

    return $title; // If none of the criterias match, set default meta description.
}
osc_add_filter('meta_title_filter', 'wm_meta_title_category');

function wm_meta_description_category($description) {
    if (osc_is_search_page()) { // If we are on search page...
        $category = osc_search_category_id(); // Get current category.
        if (array_key_exists(0, $category)) { // If current category isn't empty...
            View::newInstance()->_exportVariableToView('category', Category::newInstance()->findByPrimaryKey($category[0]));
            if(!empty(osc_category_description(osc_current_user_locale()))) { // If category description isn't empty...
                $description = osc_category_description(osc_current_user_locale()); // Meta description is now category description.
            } else { // If category description is empty...
                $description = osc_category_name(osc_current_user_locale()); // Meta description is now category title.
            }
         }
    }

    return $description; // If none of the criterias match, set default meta description.
}
osc_add_filter('meta_description_filter', 'wm_meta_description_category');

If you want to add custom meta title and meta description for specific categories directly in the file, add this to your functions.php.

Code: [Select]
function wm_meta_title_category($title) {
    if (osc_is_search_page()) { // If we are on search page...
        $category = osc_search_category_id(); // Get current category.
        if (array_key_exists(0, $category)) { // If current category isn't empty...
            switch($category) {
                case 1:
                    $title = 'Title for category with id 1';
                break;
                case 2:
                    $title = 'Title for category with id 2';
                break;
                case 15:
                    $title = 'Title for category with id 15';
                break;
            }
         }
    }

    return $title; // If none of the criterias match, set default meta description.
}
osc_add_filter('meta_title_filter', 'wm_meta_title_category');

function wm_meta_description_category($description) {
    if (osc_is_search_page()) { // If we are on search page...
        $category = osc_search_category_id(); // Get current category.
        if (array_key_exists(0, $category)) { // If current category isn't empty...
            switch($category) {
                case 1:
                    $description = 'Description for category with id 1';
                break;
                case 2:
                    $description = 'Description for category with id 2';
                break;
                case 15:
                    $description = 'Description for category with id 15';
                break;
            }
         }
    }

    return $description; // If none of the criterias match, set default meta description.
}
osc_add_filter('meta_description_filter', 'wm_meta_description_category');

Note for the second solution: just copy those 3 lines (case, title/description, break) and change ID and description for each one of your categories.

Perhaps it ain't the most sophisticated way of doing this, but otherwise, you would need a plugin with a database to store all custom titles and descriptions, etc.

Regards.

Rupert

  • Newbie
  • *
  • Posts: 15
Re: Thanks, and also Meta Title And Description
« Reply #5 on: July 24, 2019, 11:38:37 pm »
Thanks everyone for the suggestions. WEBmods, your first choice ("If you want to use category name as meta title and category description as meta description, add this to your functions.php.") works great, but isn't what I'm looking for.

Your second choice ("If you want to add custom meta title and meta description for specific categories directly in the file, add this to your functions.php.") is just what I'm looking for. However, I can't get it to work. I've tried several times thinking I was probably screwing it up - and I still could be - but I can't get it to work.

I'll be the first to admit I've been doing a lot of mods at a high rate of speed so the reason it isn't working could easily be my error. I'll revisit this solution again and try some more.

Thanks all for the help.
« Last Edit: July 25, 2019, 02:43:57 am by Rupert »

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Thanks, and also Meta Title And Description
« Reply #6 on: July 25, 2019, 12:12:35 am »
Read my post below.
« Last Edit: July 25, 2019, 12:16:59 am by WEBmods »

WEBmods

  • Hero Member
  • *****
  • Posts: 937
  • github.com/webmods-croatia/love-osclass/ | patrick
Re: Thanks, and also Meta Title And Description
« Reply #7 on: July 25, 2019, 12:16:39 am »
EDITED second solution. It should work this time. :)

If you want to add custom meta title and meta description for specific categories directly in the file, add this to your functions.php.

Code: [Select]
function wm_meta_title_category($title) {
    if (osc_is_search_page()) { // If we are on search page...
        $category = osc_search_category_id(); // Get current category.
        if (array_key_exists(0, $category)) { // If current category isn't empty...
            switch($category[0]) {
                case 1:
                    $title = 'Title for category with id 1';
                break;
                case 2:
                    $title = 'Title for category with id 2';
                break;
                case 15:
                    $title = 'Title for category with id 15';
                break;
            }
         }
    }

    return $title; // If none of the criterias match, set default meta description.
}
osc_add_filter('meta_title_filter', 'wm_meta_title_category');

function wm_meta_description_category($description) {
    if (osc_is_search_page()) { // If we are on search page...
        $category = osc_search_category_id(); // Get current category.
        if (array_key_exists(0, $category)) { // If current category isn't empty...
            switch($category[0]) {
                case 1:
                    $description = 'Description for category with id 1';
                break;
                case 2:
                    $description = 'Description for category with id 2';
                break;
                case 15:
                    $description = 'Description for category with id 15';
                break;
            }
         }
    }

    return $description; // If none of the criterias match, set default meta description.
}
osc_add_filter('meta_description_filter', 'wm_meta_description_category');

Note for the second solution: just copy those 3 lines (case, title/description, break) and change ID and description for each one of your categories.

Regards.