Home > database >  How to execute a code only for the blog post page on my functions.php file
How to execute a code only for the blog post page on my functions.php file

Time:10-12

I'm trying to execute a simple code via my functions.php file (child theme) on my wordpress website.

The code should return "yes blog" when I open the blog post page, and return "no blog" for any other page. But it only returns "no blog" whatever the page. I tried different conditionnal tags :

  • is_home()
  • is_front_page() && is_home()
  • !is_front_page() && is_home()
  • is_page_template()
  • is_page('')

It always returns "no blog" I have a default front page and a default blog page on my website.

Here is my code:

   if(is_home()){
    echo '<script language="javascript">';
    echo 'alert("yes blog")';
    echo '</script>';
        } else {
        echo '<script language="javascript">';
        echo 'alert("no blog")';
        echo '</script>';
            }

CodePudding user response:

You could replace the if condition with something like:

if($_SERVER["REQUEST_URI"] == "/blog")

CodePudding user response:

The "blog" page is a page and has an ID, so you can write something like:

if (is_page(345)) {
   // content/code for page whose ID is 345
} else {
  // content/code for pages whose ID is not 345
}
  • Related