Cray
Cray

Reputation: 5483

WordPress: get_post_type_archive_link doesn't work on certain pages

I'm using the function get_post_type_archive_link to get the link of a post type archive which I want to use in a shortcode function.

The code looks like this:

$account_help_link = get_post_type_archive_link('questions');

It works fine on almost every page but on some archives it doesn't work and outputs the current page/archive URL.

Is there any way to debug that? Or do I need to add some extra code to it? I've no idea where to look and why it's not working on these pages.

Upvotes: 0

Views: 1877

Answers (3)

FroPT
FroPT

Reputation: 85

Have you registered your new type of post (in your functions.php or another php file) with the property 'has_archive' set to true?

And have you created a "archive-question.php" file in your theme root?

Something like this:

<?php

function question_post_types()
{
  register_post_type('question', array(
    'public' => true,
    'has_archive' => true, /* this will tell WP to create a route */
    'rewrite' => array('slug' => 'questions'),
    'labels' => array(
      'name' => 'Questions',
    ),
    'menu_icon' => 'dashicons-admin-comments'
  ));
}

add_action('init', 'question_post_types');

Some help here

Editing this answer after reading your comment: "Yes to all"

Then, I would suggest you to try this inside your "single-question.php":

<a href="<?php echo get_post_type_archive_link('question'); ?>">Back to <?php echo get_post_type_archive_link('question'); ?></a>

Please let me know if this works for you.

Upvotes: 4

Rajan singh
Rajan singh

Reputation: 59

In my Case, I simply update Permalink Structure

Just Update Permalink Structure For page found issue

Creating custom Event type

Calling way

Upvotes: 1

xXxlazharxXx
xXxlazharxXx

Reputation: 443

Same problem with me when I wrote directly in the browser example: WordPress.Local/tasks, it works, but when I use get_post_type_archive_link like this to see what is the problem var_dump (get_post_type_archive_link('tasks ')) it return false

  • i have added ( 'has_archive' => 'tasks') in register_post_type
  • get_post_type_archive_link work fine with "post"

Upvotes: 1

Related Questions